OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix: logger issues after fastify upgrade
Carl-Gerhard Lindesvärdbc5fdf1f503e
diff --git a/apps/api/src/app.ts b/apps/api/src/app.tsindex dc440bcc2..a42ccbfac 100644--- a/apps/api/src/app.ts+++ b/apps/api/src/app.ts@@ -57,7 +57,7 @@ import oauthRouter from './routes/oauth-callback.router';import profileRouter from './routes/profile.router';import trackRouter from './routes/track.router';import webhookRouter from './routes/webhook.router';-import { HttpError } from './utils/errors';+import { HttpError, normalizeError } from './utils/errors';import { logger } from './utils/logger';declare module 'fastify' {@@ -370,7 +370,9 @@ export async function buildApp(const SKIP_LOG_ERRORS = ['UNAUTHORIZED', 'FST_ERR_CTP_INVALID_MEDIA_TYPE'];fastify.setErrorHandler((error, request, reply) => {- if (error.statusCode === 429) {+ const { status, code, message, errorName } = normalizeError(error);++ if (status === 429) {return reply.status(429).send({status: 429,error: 'Too Many Requests',@@ -378,35 +380,16 @@ export async function buildApp(});}- if (error instanceof HttpError) {- if (!SKIP_LOG_ERRORS.includes(error.code)) {- // 4xx are client-side problems (bad payloads, missing fields, etc.) —- // log as warn so they don't drown out real server errors.- if (error.status >= 500) {- request.log.error({ err: error }, 'internal server error');- } else {- request.log.warn({ err: error }, 'internal server error');- }- }- if (process.env.NODE_ENV === 'production' && error.status === 500) {- return reply.status(500).send('Internal server error');- }- return reply.status(error.status).send({- status: error.status,- error: error.error,- message: error.message,- });- }-- const status = error?.statusCode ?? 500;-- if (!SKIP_LOG_ERRORS.includes(error.code)) {- // Same rationale: client errors (incl. FST_ERR_VALIDATION) are warnings,- // not errors. They are caused by callers, not by us.+ const skipLog = code !== undefined && SKIP_LOG_ERRORS.includes(code);+ if (!skipLog) {+ // 4xx are client-side problems (bad payloads, missing fields, etc.) —+ // log as warn so they don't drown out real server errors.+ const label =+ error instanceof HttpError ? 'internal server error' : 'request error';if (status >= 500) {- request.log.error({ err: error }, 'request error');+ request.log.error({ err: error }, label);} else {- request.log.warn({ err: error }, 'request error');+ request.log.warn({ err: error }, label);}}@@ -414,7 +397,14 @@ export async function buildApp(return reply.status(500).send('Internal server error');}- return reply.status(status).send({ status, error, message: error.message });+ return reply.status(status).send({+ status,+ // HttpError carries an explicit `error` payload (the underlying cause);+ // for everything else we surface the error name so the body is always+ // a stable JSON shape, regardless of what was thrown.+ error: error instanceof HttpError ? error.error : errorName,+ message,+ });});return fastify;diff --git a/apps/api/src/controllers/oauth-callback.controller.tsx b/apps/api/src/controllers/oauth-callback.controller.tsxindex e0e60ae57..f626e6491 100644--- a/apps/api/src/controllers/oauth-callback.controller.tsx+++ b/apps/api/src/controllers/oauth-callback.controller.tsx@@ -130,11 +130,11 @@ async function handleNewUser({try {await connectUserToOrganization({ user, inviteId });} catch (error) {- reply.log.error('error connecting user to organization', {+ reply.log.error({error,inviteId,user,- });+ }, 'error connecting user to organization');}}diff --git a/apps/api/src/utils/errors.ts b/apps/api/src/utils/errors.tsindex 193a9611c..b6bb8e41e 100644--- a/apps/api/src/utils/errors.ts+++ b/apps/api/src/utils/errors.ts@@ -4,7 +4,7 @@ export class LogError extends Error {constructor(message: string,payload?: Record<string, unknown>,- options?: ErrorOptions,+ options?: ErrorOptions) {super(message, options);this.name = 'LogError';@@ -26,7 +26,7 @@ export class HttpError extends Error {fingerprint?: string;extra?: Record<string, unknown>;error?: Error | unknown;- },+ }) {super(message);this.name = 'HttpError';@@ -37,3 +37,56 @@ export class HttpError extends Error {Object.setPrototypeOf(this, new.target.prototype);}}++export interface NormalizedError {+ status: number;+ code: string | undefined;+ message: string;+ errorName: string;+}++export function normalizeError(error: unknown): NormalizedError {+ if (error instanceof Error) {+ const meta = error as Error & {+ statusCode?: unknown;+ status?: unknown;+ code?: unknown;+ };+ const status =+ typeof meta.statusCode === 'number'+ ? meta.statusCode+ : typeof meta.status === 'number'+ ? meta.status+ : 500;+ return {+ status,+ code: typeof meta.code === 'string' ? meta.code : undefined,+ message: error.message || 'Internal server error',+ errorName: error.name || 'Error',+ };+ }++ // Plain object thrown (e.g. `throw { statusCode: 400, message: '...' }`).+ if (typeof error === 'object' && error !== null) {+ const e = error as Record<string, unknown>;+ return {+ status: typeof e.statusCode === 'number' ? e.statusCode : 500,+ code: typeof e.code === 'string' ? e.code : undefined,+ message:+ typeof e.message === 'string' ? e.message : 'Internal server error',+ errorName: typeof e.name === 'string' ? e.name : 'Error',+ };+ }++ if (typeof error === 'string') {+ return { status: 500, code: undefined, message: error, errorName: 'Error' };+ }++ // null, undefined, number, boolean, symbol, bigint+ return {+ status: 500,+ code: undefined,+ message: 'Internal server error',+ errorName: 'Error',+ };+}