forked from kdy1/trpc-openapi
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrors.ts
More file actions
21 lines (19 loc) · 717 Bytes
/
errors.ts
File metadata and controls
21 lines (19 loc) · 717 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { TRPCError } from '@trpc/server';
import { TRPC_ERROR_CODES_BY_KEY } from '@trpc/server/rpc';
export const getErrorFromUnknown = (error: unknown): TRPCError => {
if (error instanceof Error && error.name === 'TRPCError') {
return error as TRPCError;
}
const code = (error as any).code as keyof typeof TRPC_ERROR_CODES_BY_KEY;
const errorToString =
typeof (error as any).toString === 'function' ? (error as any).toString() : undefined;
return new TRPCError({
code: TRPC_ERROR_CODES_BY_KEY[code] ? code : 'INTERNAL_SERVER_ERROR',
message:
error instanceof Error
? error.message
: errorToString
? errorToString
: 'Unknown error occurred',
});
};