Feature/wrap logging * feat: change all logging to go through our own logger function so that we can control log levels * move packages/trpc to using our own logger * merge Approved-by: Linus Flood
63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
|
|
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
|
|
|
import { appRouter } from "./routers/appRouter"
|
|
import { createCallerFactory } from "."
|
|
|
|
import type { Context } from "./context"
|
|
|
|
const createCaller = createCallerFactory(appRouter)
|
|
|
|
export type CreateContextFn = () => Promise<Context>
|
|
let createTrpcContext: CreateContextFn | null = null
|
|
|
|
export function configureServerClient(createContext: () => Promise<Context>) {
|
|
createTrpcContext = createContext
|
|
}
|
|
|
|
type OnError = Required<Parameters<typeof createCaller>>[1]["onError"]
|
|
type ServerClientOptions = {
|
|
onError?: OnError
|
|
}
|
|
export function appServerClient(
|
|
context: Context,
|
|
options: ServerClientOptions = {}
|
|
) {
|
|
const serverClientLogger = createLogger("serverClient")
|
|
return createCaller(context, {
|
|
onError: (args) => {
|
|
const { ctx, error, input, path, type } = args
|
|
serverClientLogger.error(`error for ${type}: ${path}`, error)
|
|
if (input) {
|
|
serverClientLogger.error(`received input:`, input)
|
|
}
|
|
|
|
options.onError?.(args)
|
|
|
|
Sentry.captureException(error, {
|
|
extra: {
|
|
input,
|
|
path,
|
|
type,
|
|
url: ctx?.url,
|
|
lang: ctx?.lang,
|
|
pathname: ctx?.pathname,
|
|
contentType: ctx?.contentType,
|
|
uid: ctx?.uid,
|
|
},
|
|
})
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function packageServerClient() {
|
|
if (!createTrpcContext) {
|
|
throw new Error(
|
|
"createTrpcContext is not defined. Did you forget to call configureServerClient?"
|
|
)
|
|
}
|
|
|
|
return appServerClient(await createTrpcContext())
|
|
}
|