Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Do not import Stripe lib in getServerErrorFromUnknown #19578

Merged
merged 9 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions packages/app-store/_utils/stripe.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { z } from "zod";

// Copied from node_modules/stripe/types/Errors.d.ts because stripe lib doesn't expose these types
interface StripeInvalidRequestError extends Error {
readonly type: "StripeInvalidRequestError";
readonly rawType: "invalid_request_error";
/**
* A human-readable message giving more details about the error. For card errors, these messages can
* be shown to your users.
*/
readonly message: string;
/**
* For card errors, a short string describing the kind of card error that occurred.
*
* @docs https://stripe.com/docs/error-codes
*/
readonly code?: string;

/**
* A URL to more information about the error code reported.
*
* @docs https://stripe.com/docs/error-codes
*/
readonly doc_url?: string;

/**
* Typically a 4xx or 5xx.
*/
readonly statusCode?: number;

readonly raw: unknown;

readonly headers: {
[key: string]: string;
};

readonly requestId: string;

/**
* The parameter the error relates to if the error is parameter-specific. You can use this to display a
* message near the correct form field, for example.
*/
readonly param?: string;
}

const errorSchema = z.object({
name: z.string(),
message: z.string(),
stack: z.string().optional(),
});

export const stripeInvalidRequestErrorSchema = errorSchema.extend({
type: z.literal("StripeInvalidRequestError"),
rawType: z.literal("invalid_request_error"),
code: z.string().optional(),
doc_url: z.string().optional(),
statusCode: z.number().optional(),
raw: z.unknown(),
headers: z.record(z.string()),
requestId: z.string(),
param: z.string().optional(),
});
10 changes: 6 additions & 4 deletions packages/lib/server/getServerErrorFromUnknown.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Prisma } from "@prisma/client";
import Stripe from "stripe";
import type { ZodIssue } from "zod";
import { ZodError } from "zod";

import { stripeInvalidRequestErrorSchema } from "@calcom/app-store/_utils/stripe.types";
import { ErrorCode } from "@calcom/lib/errorCodes";

import { TRPCError } from "@trpc/server";
Expand Down Expand Up @@ -36,6 +36,11 @@ function parseZodErrorIssues(issues: ZodIssue[]): string {
}

export function getServerErrorFromUnknown(cause: unknown): HttpError {
const parsedStripeError = stripeInvalidRequestErrorSchema.safeParse(cause);
if (parsedStripeError.success) {
return getHttpError({ statusCode: 400, cause: parsedStripeError.data });
}

if (cause instanceof TRPCError) {
const statusCode = getHTTPStatusCodeFromError(cause);
return new HttpError({ statusCode, message: cause.message });
Expand All @@ -56,9 +61,6 @@ export function getServerErrorFromUnknown(cause: unknown): HttpError {
if (isPrismaError(cause)) {
return getServerErrorFromPrismaError(cause);
}
if (cause instanceof Stripe.errors.StripeInvalidRequestError) {
return getHttpError({ statusCode: 400, cause });
}
if (cause instanceof HttpError) {
const redactedCause = redactError(cause);
return {
Expand Down
Loading