refactor: consolidate error handlers to use getServerErrorFromUnknown (#25114)
* refactor: consolidate error handlers to use getServerErrorFromUnknown - Migrate server-only code to use getServerErrorFromUnknown for better error handling - Add JSDoc documentation to both getErrorFromUnknown and getServerErrorFromUnknown - Update webhook handlers, payment services, email service, and booking service - Keep getErrorFromUnknown for client-side and isomorphic code - Improve error message extraction by using err.cause?.stack instead of err.stack - Fix ESLint warnings: replace 'any' with 'unknown' types, fix hasOwnProperty usage Co-Authored-By: [email protected] <[email protected]> * Update packages/app-store/paypal/api/webhook.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * refactor --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
12 files changed
+61
-38
No files matched your search
+19
-2
@@ -13,17 +13,34 @@ export class ErrorWithCode extends Error {
|
||||
get(_, prop: string) {
|
||||
if (prop in ErrorCode) {
|
||||
const code = ErrorCode[prop as keyof typeof ErrorCode];
|
||||
return (message?: string, data?: Record<string, any>) => new ErrorWithCode(code, message, data);
|
||||
return (message?: string, data?: Record<string, unknown>) => new ErrorWithCode(code, message, data);
|
||||
}
|
||||
throw new Error(`Unknown error code: ${prop}`);
|
||||
},
|
||||
}) as unknown as Record<
|
||||
keyof typeof ErrorCode,
|
||||
(message?: string, data?: Record<string, any>) => ErrorWithCode
|
||||
(message?: string, data?: Record<string, unknown>) => ErrorWithCode
|
||||
>;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts unknown error types to Error objects.
|
||||
*
|
||||
* @deprecated For server-side code, use `getServerErrorFromUnknown` from `@calcom/lib/server/getServerErrorFromUnknown` instead.
|
||||
* This function should only be used in client-side or isomorphic code (React components, shared utilities).
|
||||
*
|
||||
* Use this function when:
|
||||
* - You're in a React component that runs on the client
|
||||
* - You're in shared/isomorphic code that cannot import server-only dependencies
|
||||
* - You only need a basic Error object without HTTP status code mapping
|
||||
*
|
||||
* For server-side error handling (API routes, tRPC handlers, webhooks), use `getServerErrorFromUnknown`
|
||||
* which provides proper HTTP status code mapping, error redaction, and handles Zod/Prisma/Stripe errors.
|
||||
*
|
||||
* @param cause - The unknown error to convert
|
||||
* @returns An Error object with optional statusCode and code properties
|
||||
*/
|
||||
export function getErrorFromUnknown(cause: unknown): Error & { statusCode?: number; code?: string } {
|
||||
if (cause instanceof Error) {
|
||||
return cause;
|
||||
|
||||
@@ -33,6 +33,12 @@ function parseZodErrorIssues(issues: ZodIssue[]): string {
|
||||
.join("; ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts unknown error types to HttpError with proper status code mapping and error redaction.
|
||||
* SERVER-ONLY: This function imports Prisma and Stripe schemas and should only be used in server-side code.
|
||||
* Use in API routes, tRPC handlers, webhooks, and server-side services.
|
||||
* For client-side code, use getErrorFromUnknown from @calcom/lib/errors instead.
|
||||
*/
|
||||
export function getServerErrorFromUnknown(cause: unknown): HttpError {
|
||||
if (isZodError(cause)) {
|
||||
return new HttpError({
|
||||
|
||||
Reference in new issue
Block a user