2023-02-28 21:15:50 +00:00
|
|
|
import { PrismaClientKnownRequestError, NotFoundError } from "@prisma/client/runtime/library";
|
2022-06-10 18:38:46 +00:00
|
|
|
import Stripe from "stripe";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { ZodIssue } from "zod";
|
|
|
|
import { ZodError } from "zod";
|
2022-06-10 18:38:46 +00:00
|
|
|
|
|
|
|
import { HttpError } from "../http-error";
|
2023-08-03 15:22:38 +00:00
|
|
|
import { redactError } from "../redactError";
|
2022-06-10 18:38:46 +00:00
|
|
|
|
2022-06-14 20:07:39 +00:00
|
|
|
function hasName(cause: unknown): cause is { name: string } {
|
|
|
|
return !!cause && typeof cause === "object" && "name" in cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isZodError(cause: unknown): cause is ZodError {
|
|
|
|
return cause instanceof ZodError || (hasName(cause) && cause.name === "ZodError");
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseZodErrorIssues(issues: ZodIssue[]): string {
|
|
|
|
return issues
|
|
|
|
.map((i) =>
|
|
|
|
i.code === "invalid_union"
|
2022-06-14 20:43:50 +00:00
|
|
|
? i.unionErrors.map((ue) => parseZodErrorIssues(ue.issues)).join("; ")
|
|
|
|
: i.code === "unrecognized_keys"
|
|
|
|
? i.message
|
2022-10-14 22:45:02 +00:00
|
|
|
: `${i.path.length ? `${i.code} in '${i.path}': ` : ""}${i.message}`
|
2022-06-14 20:07:39 +00:00
|
|
|
)
|
2022-06-14 20:43:50 +00:00
|
|
|
.join("; ");
|
2022-06-14 20:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 13:34:17 +00:00
|
|
|
export function getServerErrorFromUnknown(cause: unknown): HttpError {
|
2022-06-14 20:07:39 +00:00
|
|
|
if (isZodError(cause)) {
|
|
|
|
console.log("cause", cause);
|
|
|
|
return new HttpError({
|
|
|
|
statusCode: 400,
|
|
|
|
message: parseZodErrorIssues(cause.issues),
|
|
|
|
cause,
|
|
|
|
});
|
|
|
|
}
|
2022-08-30 00:05:35 +00:00
|
|
|
if (cause instanceof SyntaxError) {
|
|
|
|
return new HttpError({
|
|
|
|
statusCode: 500,
|
|
|
|
message: "Unexpected error, please reach out for our customer support.",
|
|
|
|
});
|
|
|
|
}
|
2023-08-03 15:22:38 +00:00
|
|
|
|
2022-06-13 16:31:00 +00:00
|
|
|
if (cause instanceof PrismaClientKnownRequestError) {
|
2023-08-03 15:22:38 +00:00
|
|
|
return getHttpError({ statusCode: 400, cause });
|
2022-06-10 18:38:46 +00:00
|
|
|
}
|
2022-08-31 19:44:47 +00:00
|
|
|
if (cause instanceof NotFoundError) {
|
2023-08-03 15:22:38 +00:00
|
|
|
return getHttpError({ statusCode: 404, cause });
|
2022-08-31 19:44:47 +00:00
|
|
|
}
|
2022-06-14 20:07:39 +00:00
|
|
|
if (cause instanceof Stripe.errors.StripeInvalidRequestError) {
|
2023-08-03 15:22:38 +00:00
|
|
|
return getHttpError({ statusCode: 400, cause });
|
2022-06-10 18:38:46 +00:00
|
|
|
}
|
|
|
|
if (cause instanceof HttpError) {
|
2023-08-03 15:22:38 +00:00
|
|
|
const redactedCause = redactError(cause);
|
|
|
|
return {
|
|
|
|
...redactedCause,
|
|
|
|
cause: cause.cause,
|
|
|
|
url: cause.url,
|
|
|
|
statusCode: cause.statusCode,
|
|
|
|
method: cause.method,
|
|
|
|
};
|
2022-06-10 18:38:46 +00:00
|
|
|
}
|
2022-06-14 20:07:39 +00:00
|
|
|
if (cause instanceof Error) {
|
2023-08-03 15:22:38 +00:00
|
|
|
return getHttpError({ statusCode: 500, cause });
|
2022-06-10 18:38:46 +00:00
|
|
|
}
|
|
|
|
if (typeof cause === "string") {
|
|
|
|
// @ts-expect-error https://github.com/tc39/proposal-error-cause
|
|
|
|
return new Error(cause, { cause });
|
|
|
|
}
|
|
|
|
|
2022-08-30 00:05:35 +00:00
|
|
|
return new HttpError({
|
|
|
|
statusCode: 500,
|
|
|
|
message: `Unhandled error of type '${typeof cause}'. Please reach out for our customer support.`,
|
|
|
|
});
|
2022-06-10 18:38:46 +00:00
|
|
|
}
|
2023-08-03 15:22:38 +00:00
|
|
|
|
|
|
|
function getHttpError<T extends Error>({ statusCode, cause }: { statusCode: number; cause: T }) {
|
|
|
|
const redacted = redactError(cause);
|
|
|
|
return new HttpError({ statusCode, message: redacted.message, cause: redacted });
|
|
|
|
}
|