refactor: Specific error status codes (#18847)
This commit is contained in:
1 parent
b3f946dd0b
commit
1fd4c3fd4c
4 files changed
+54
-8
No files matched your search
@@ -3,6 +3,8 @@ import Stripe from "stripe";
|
||||
import type { ZodIssue } from "zod";
|
||||
import { ZodError } from "zod";
|
||||
|
||||
import { ErrorCode } from "@calcom/lib/errorCodes";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { getHTTPStatusCodeFromError } from "@trpc/server/http";
|
||||
|
||||
@@ -70,7 +72,8 @@ export function getServerErrorFromUnknown(cause: unknown): HttpError {
|
||||
};
|
||||
}
|
||||
if (cause instanceof Error) {
|
||||
return getHttpError({ statusCode: 500, cause });
|
||||
const statusCode = getStatusCode(cause);
|
||||
return getHttpError({ statusCode, cause });
|
||||
}
|
||||
if (typeof cause === "string") {
|
||||
// @ts-expect-error https://github.com/tc39/proposal-error-cause
|
||||
@@ -83,6 +86,34 @@ export function getServerErrorFromUnknown(cause: unknown): HttpError {
|
||||
});
|
||||
}
|
||||
|
||||
function getStatusCode(cause: Error): number {
|
||||
switch (cause.message) {
|
||||
case ErrorCode.RequestBodyWithouEnd:
|
||||
case ErrorCode.MissingPaymentCredential:
|
||||
case ErrorCode.MissingPaymentAppId:
|
||||
case ErrorCode.AvailabilityNotFoundInSchedule:
|
||||
return 400;
|
||||
case ErrorCode.CancelledBookingsCannotBeRescheduled:
|
||||
return 403;
|
||||
case ErrorCode.NoAvailableUsersFound:
|
||||
case ErrorCode.HostsUnavailableForBooking:
|
||||
case ErrorCode.PaymentCreationFailure:
|
||||
case ErrorCode.ChargeCardFailure:
|
||||
case ErrorCode.AlreadySignedUpForBooking:
|
||||
case ErrorCode.BookingSeatsFull:
|
||||
case ErrorCode.NotEnoughAvailableSeats:
|
||||
return 409;
|
||||
case ErrorCode.EventTypeNotFound:
|
||||
case ErrorCode.BookingNotFound:
|
||||
return 404;
|
||||
case ErrorCode.UnableToSubscribeToThePlatform:
|
||||
case ErrorCode.UpdatingOauthClientError:
|
||||
case ErrorCode.CreatingOauthClientError:
|
||||
default:
|
||||
return 500;
|
||||
}
|
||||
}
|
||||
|
||||
function getHttpError<T extends Error>({ statusCode, cause }: { statusCode: number; cause: T }) {
|
||||
const redacted = redactError(cause);
|
||||
return new HttpError({ statusCode, message: redacted.message, cause: redacted });
|
||||
|
||||
Reference in new issue
Block a user