Skip to content

Commit e6ba899

Browse files
hbjORbjdevin-ai-integration[bot]cubic-dev-ai[bot]
authored
refactor: consolidate error handlers to use getServerErrorFromUnknown (calcom#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: benny@cal.com <sldisek783@gmail.com> * 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>
1 parent 7242048 commit e6ba899

12 files changed

Lines changed: 61 additions & 38 deletions

File tree

apps/web/pages/api/integrations/subscriptions/webhook.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type Stripe from "stripe";
44

55
import stripe from "@calcom/features/ee/payments/server/stripe";
66
import { IS_PRODUCTION } from "@calcom/lib/constants";
7-
import { getErrorFromUnknown } from "@calcom/lib/errors";
87
import { HttpError as HttpCode } from "@calcom/lib/http-error";
8+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
99
import { prisma } from "@calcom/prisma";
1010

1111
export const config = {
@@ -108,13 +108,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
108108
});
109109
}
110110
} catch (_err) {
111-
const err = getErrorFromUnknown(_err);
111+
const err = getServerErrorFromUnknown(_err);
112112
if (!err.message.includes("No credential found with subscription ID")) {
113113
console.error(`Webhook Error: ${err.message}`);
114114
}
115-
res.status(err.statusCode ?? 500).send({
115+
res.status(err.statusCode).send({
116116
message: err.message,
117-
stack: IS_PRODUCTION ? undefined : err.stack,
117+
stack: IS_PRODUCTION ? undefined : err.cause?.stack,
118118
});
119119
return;
120120
}

packages/app-store/alby/api/webhook.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { handlePaymentSuccess } from "@calcom/app-store/_utils/payments/handlePa
66
import { albyCredentialKeysSchema } from "@calcom/app-store/alby/lib";
77
import parseInvoice from "@calcom/app-store/alby/lib/parseInvoice";
88
import { IS_PRODUCTION } from "@calcom/lib/constants";
9-
import { getErrorFromUnknown } from "@calcom/lib/errors";
109
import { HttpError as HttpCode } from "@calcom/lib/http-error";
10+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
1111
import prisma from "@calcom/prisma";
1212

1313
export const config = {
@@ -90,11 +90,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
9090

9191
return await handlePaymentSuccess(payment.id, payment.bookingId);
9292
} catch (_err) {
93-
const err = getErrorFromUnknown(_err);
93+
const err = getServerErrorFromUnknown(_err);
9494
console.error(`Webhook Error: ${err.message}`);
95-
return res.status(err.statusCode || 500).send({
95+
return res.status(err.statusCode).send({
9696
message: err.message,
97-
stack: IS_PRODUCTION ? undefined : err.stack,
97+
stack: IS_PRODUCTION ? undefined : err.cause?.stack,
9898
});
9999
}
100100
}

packages/app-store/btcpayserver/api/webhook.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { z } from "zod";
55

66
import { handlePaymentSuccess } from "@calcom/app-store/_utils/payments/handlePaymentSuccess";
77
import { IS_PRODUCTION } from "@calcom/lib/constants";
8-
import { getErrorFromUnknown } from "@calcom/lib/errors";
98
import { HttpError as HttpCode } from "@calcom/lib/http-error";
9+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
1010
import { PrismaBookingPaymentRepository as BookingPaymentRepository } from "@calcom/lib/server/repository/PrismaBookingPaymentRepository";
1111

1212
import appConfig from "../config.json";
@@ -90,11 +90,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
9090
await handlePaymentSuccess(payment.id, payment.bookingId);
9191
return res.status(200).json({ success: true });
9292
} catch (_err) {
93-
const err = getErrorFromUnknown(_err);
94-
const statusCode = err instanceof HttpCode ? err.statusCode : 500;
95-
return res.status(statusCode).send({
93+
const err = getServerErrorFromUnknown(_err);
94+
return res.status(err.statusCode).send({
9695
message: err.message,
97-
stack: IS_PRODUCTION ? undefined : err.stack,
96+
stack: IS_PRODUCTION ? undefined : err.cause?.stack,
9897
});
9998
}
10099
}

packages/app-store/hitpay/api/webhook.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type z from "zod";
44

55
import { handlePaymentSuccess } from "@calcom/app-store/_utils/payments/handlePaymentSuccess";
66
import { IS_PRODUCTION } from "@calcom/lib/constants";
7-
import { getErrorFromUnknown } from "@calcom/lib/errors";
87
import { HttpError as HttpCode } from "@calcom/lib/http-error";
8+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
99
import prisma from "@calcom/prisma";
1010

1111
import type { hitpayCredentialKeysSchema } from "../lib/hitpayCredentialKeysSchema";
@@ -111,11 +111,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
111111
}
112112
return await handlePaymentSuccess(payment.id, payment.bookingId);
113113
} catch (_err) {
114-
const err = getErrorFromUnknown(_err);
114+
const err = getServerErrorFromUnknown(_err);
115115
console.error(`Webhook Error: ${err.message}`);
116116
return res.status(200).send({
117117
message: err.message,
118-
stack: IS_PRODUCTION ? undefined : err.stack,
118+
stack: IS_PRODUCTION ? undefined : err.cause?.stack,
119119
});
120120
}
121121
}

packages/app-store/paypal/api/webhook.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { handlePaymentSuccess } from "@calcom/app-store/_utils/payments/handlePa
66
import { paypalCredentialKeysSchema } from "@calcom/app-store/paypal/lib";
77
import Paypal from "@calcom/app-store/paypal/lib/Paypal";
88
import { IS_PRODUCTION } from "@calcom/lib/constants";
9-
import { getErrorFromUnknown } from "@calcom/lib/errors";
109
import { HttpError as HttpCode } from "@calcom/lib/http-error";
10+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
1111
import prisma from "@calcom/prisma";
1212

1313
export const config = {
@@ -92,11 +92,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
9292
return await handlePaypalPaymentSuccess(parsedPayload, bodyAsString, parseHeaders.data);
9393
}
9494
} catch (_err) {
95-
const err = getErrorFromUnknown(_err);
95+
const err = getServerErrorFromUnknown(_err);
9696
console.error(`Webhook Error: ${err.message}`);
9797
res.status(200).send({
9898
message: err.message,
99-
stack: IS_PRODUCTION ? undefined : err.stack,
99+
stack: IS_PRODUCTION ? undefined : err.cause?.stack,
100100
});
101101
return;
102102
}

packages/app-store/stripepayment/lib/PaymentService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import z from "zod";
55
import { sendAwaitingPaymentEmailAndSMS } from "@calcom/emails/email-manager";
66
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
77
import { ErrorCode } from "@calcom/lib/errorCodes";
8-
import { getErrorFromUnknown } from "@calcom/lib/errors";
98
import { ErrorWithCode } from "@calcom/lib/errors";
109
import logger from "@calcom/lib/logger";
10+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
1111
import { safeStringify } from "@calcom/lib/safeStringify";
1212
import prisma from "@calcom/prisma";
1313
import type { Booking, Payment, PaymentOption, Prisma } from "@calcom/prisma/client";
@@ -368,7 +368,7 @@ export class PaymentService implements IAbstractPaymentService {
368368
});
369369
return updatedPayment;
370370
} catch (e) {
371-
const err = getErrorFromUnknown(e);
371+
const err = getServerErrorFromUnknown(e);
372372
throw err;
373373
}
374374
}

packages/app-store/vital/api/webhook.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import queue from "queue";
33

44
import dayjs from "@calcom/dayjs";
55
import { IS_PRODUCTION } from "@calcom/lib/constants";
6-
import { getErrorFromUnknown } from "@calcom/lib/errors";
76
import { HttpError as HttpCode } from "@calcom/lib/http-error";
87
import logger from "@calcom/lib/logger";
8+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
99
import prisma from "@calcom/prisma";
1010
import type { Prisma } from "@calcom/prisma/client";
1111
import { BookingStatus } from "@calcom/prisma/enums";
@@ -100,7 +100,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
100100
return;
101101
}
102102

103-
if (!event.data.hasOwnProperty(parameterFilter)) {
103+
if (!Object.prototype.hasOwnProperty.call(event.data, parameterFilter)) {
104104
res.status(500).json({ message: "Selected param not available" });
105105
return;
106106
}
@@ -141,7 +141,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
141141
);
142142
}
143143
await q.start();
144-
} catch (error) {
144+
} catch {
145145
throw new Error("Failed to reschedule bookings");
146146
}
147147
}
@@ -155,11 +155,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
155155
}
156156
return res.status(200).json({ body: req.body });
157157
} catch (_err) {
158-
const err = getErrorFromUnknown(_err);
158+
const err = getServerErrorFromUnknown(_err);
159159
console.error(`Webhook Error: ${err.message}`);
160-
res.status(err.statusCode ?? 500).send({
160+
res.status(err.statusCode).send({
161161
message: err.message,
162-
stack: IS_PRODUCTION ? undefined : err.stack,
162+
stack: IS_PRODUCTION ? undefined : err.cause?.stack,
163163
});
164164
return;
165165
}

packages/emails/templates/_base-email.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { z } from "zod";
33

44
import dayjs from "@calcom/dayjs";
55
import { FeaturesRepository } from "@calcom/features/flags/features.repository";
6-
import { getErrorFromUnknown } from "@calcom/lib/errors";
76
import isSmsCalEmail from "@calcom/lib/isSmsCalEmail";
87
import { serverConfig } from "@calcom/lib/serverConfig";
8+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
99
import { setTestEmail } from "@calcom/lib/testEmails";
1010
import { prisma } from "@calcom/prisma";
1111

@@ -77,7 +77,7 @@ export default class BaseEmail {
7777
payloadWithUnEscapedSubject,
7878
(_err, info) => {
7979
if (_err) {
80-
const err = getErrorFromUnknown(_err);
80+
const err = getServerErrorFromUnknown(_err);
8181
this.printNodeMailerError(err);
8282
reject(err);
8383
} else {

packages/features/bookings/lib/service/RegularBookingService.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ import { groupHostsByGroupId } from "@calcom/lib/bookings/hostGroupUtils";
6161
import { shouldIgnoreContactOwner } from "@calcom/lib/bookings/routing/utils";
6262
import { DEFAULT_GROUP_ID } from "@calcom/lib/constants";
6363
import { ErrorCode } from "@calcom/lib/errorCodes";
64-
import { getErrorFromUnknown, ErrorWithCode } from "@calcom/lib/errors";
64+
import { ErrorWithCode } from "@calcom/lib/errors";
6565
import { extractBaseEmail } from "@calcom/lib/extract-base-email";
6666
import getOrgIdFromMemberOrTeamId from "@calcom/lib/getOrgIdFromMemberOrTeamId";
6767
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
6868
import { HttpError } from "@calcom/lib/http-error";
6969
import { criticalLogger } from "@calcom/lib/logger.server";
7070
import { getPiiFreeCalendarEvent, getPiiFreeEventType } from "@calcom/lib/piiFreeData";
7171
import { safeStringify } from "@calcom/lib/safeStringify";
72+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
7273
import { getTranslation } from "@calcom/lib/server/i18n";
7374
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
7475
import { distributedTracing } from "@calcom/lib/tracing/factory";
@@ -1844,9 +1845,9 @@ async function handler(
18441845
};
18451846
}
18461847
} catch (_err) {
1847-
const err = getErrorFromUnknown(_err);
1848+
const err = getServerErrorFromUnknown(_err);
18481849
tracingLogger.error(`Booking ${eventTypeId} failed`, "Error when saving booking to db", err.message);
1849-
if (err.code === "P2002") {
1850+
if (err.cause && typeof err.cause === "object" && "code" in err.cause && err.cause.code === "P2002") {
18501851
throw new HttpError({
18511852
statusCode: 409,
18521853
message: ErrorCode.BookingConflict,

packages/features/ee/payments/api/webhook.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import stripe from "@calcom/features/ee/payments/server/stripe";
1414
import { getPlatformParams } from "@calcom/features/platform-oauth-client/get-platform-params";
1515
import { PlatformOAuthClientRepository } from "@calcom/features/platform-oauth-client/platform-oauth-client.repository";
1616
import { IS_PRODUCTION } from "@calcom/lib/constants";
17-
import { getErrorFromUnknown } from "@calcom/lib/errors";
1817
import { HttpError as HttpCode } from "@calcom/lib/http-error";
1918
import logger from "@calcom/lib/logger";
2019
import { safeStringify } from "@calcom/lib/safeStringify";
20+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
2121
import { prisma } from "@calcom/prisma";
2222
import type { Prisma } from "@calcom/prisma/client";
2323
import { BookingStatus } from "@calcom/prisma/enums";
@@ -180,11 +180,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
180180
});
181181
}
182182
} catch (_err) {
183-
const err = getErrorFromUnknown(_err);
183+
const err = getServerErrorFromUnknown(_err);
184184
console.error(`Webhook Error: ${err.message}`);
185-
res.status(err.statusCode ?? 500).send({
185+
res.status(err.statusCode).send({
186186
message: err.message,
187-
stack: IS_PRODUCTION ? undefined : err.stack,
187+
stack: IS_PRODUCTION ? undefined : err.cause?.stack,
188188
});
189189
return;
190190
}

0 commit comments

Comments
 (0)