MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / normalizeError

Function normalizeError

apps/api/src/utils/errors.ts:48–92  ·  view source on GitHub ↗
(error: unknown)

Source from the content-addressed store, hash-verified

46}
47
48export function normalizeError(error: unknown): NormalizedError {
49 if (error instanceof Error) {
50 const meta = error as Error & {
51 statusCode?: unknown;
52 status?: unknown;
53 code?: unknown;
54 };
55 const status =
56 typeof meta.statusCode === 'number'
57 ? meta.statusCode
58 : typeof meta.status === 'number'
59 ? meta.status
60 : 500;
61 return {
62 status,
63 code: typeof meta.code === 'string' ? meta.code : undefined,
64 message: error.message || 'Internal server error',
65 errorName: error.name || 'Error',
66 };
67 }
68
69 // Plain object thrown (e.g. `throw { statusCode: 400, message: '...' }`).
70 if (typeof error === 'object' && error !== null) {
71 const e = error as Record<string, unknown>;
72 return {
73 status: typeof e.statusCode === 'number' ? e.statusCode : 500,
74 code: typeof e.code === 'string' ? e.code : undefined,
75 message:
76 typeof e.message === 'string' ? e.message : 'Internal server error',
77 errorName: typeof e.name === 'string' ? e.name : 'Error',
78 };
79 }
80
81 if (typeof error === 'string') {
82 return { status: 500, code: undefined, message: error, errorName: 'Error' };
83 }
84
85 // null, undefined, number, boolean, symbol, bigint
86 return {
87 status: 500,
88 code: undefined,
89 message: 'Internal server error',
90 errorName: 'Error',
91 };
92}

Callers 1

buildAppFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected