chore: add CalendarService Exception Filter api v2 (#20021)

This commit is contained in:
Morgan
2025-03-12 16:24:50 +00:00
committed by GitHub
parent 49f75f5070
commit 8de7876f72
5 changed files with 78 additions and 10 deletions
+1 -1
View File
@@ -38,7 +38,7 @@
"@axiomhq/winston": "^1.2.0",
"@calcom/platform-constants": "*",
"@calcom/platform-enums": "*",
"@calcom/platform-libraries": "npm:@calcom/platform-libraries@0.0.149",
"@calcom/platform-libraries": "npm:@calcom/platform-libraries@0.0.150",
"@calcom/platform-libraries-0.0.2": "npm:@calcom/platform-libraries@0.0.2",
"@calcom/platform-types": "*",
"@calcom/platform-utils": "*",
+2
View File
@@ -21,6 +21,7 @@ import {
X_CAL_PLATFORM_EMBED,
} from "@calcom/platform-constants";
import { CalendarServiceExceptionFilter } from "./filters/calendar-service-exception.filter";
import { TRPCExceptionFilter } from "./filters/trpc-exception.filter";
export const bootstrap = (app: NestExpressApplication): NestExpressApplication => {
@@ -76,6 +77,7 @@ export const bootstrap = (app: NestExpressApplication): NestExpressApplication =
app.useGlobalFilters(new ZodExceptionFilter());
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalFilters(new TRPCExceptionFilter());
app.useGlobalFilters(new CalendarServiceExceptionFilter());
app.use(cookieParser());
@@ -0,0 +1,57 @@
import { filterReqHeaders } from "@/lib/filterReqHeaders";
import { ArgumentsHost, Catch, ExceptionFilter, Logger } from "@nestjs/common";
import { Request } from "express";
import { ERROR_STATUS } from "@calcom/platform-constants";
import {
CalendarAppError,
CalendarAppDelegationCredentialInvalidGrantError,
CalendarAppDelegationCredentialError,
CalendarAppDelegationCredentialConfigurationError,
CalendarAppDelegationCredentialClientIdNotAuthorizedError,
CalendarAppDelegationCredentialNotSetupError,
} from "@calcom/platform-libraries/app-store";
import { Response } from "@calcom/platform-types";
type CalendarError =
| CalendarAppError
| CalendarAppDelegationCredentialInvalidGrantError
| CalendarAppDelegationCredentialError
| CalendarAppDelegationCredentialConfigurationError
| CalendarAppDelegationCredentialNotSetupError
| CalendarAppDelegationCredentialClientIdNotAuthorizedError;
@Catch(
CalendarAppError,
CalendarAppDelegationCredentialInvalidGrantError,
CalendarAppDelegationCredentialError,
CalendarAppDelegationCredentialConfigurationError,
CalendarAppDelegationCredentialClientIdNotAuthorizedError,
CalendarAppDelegationCredentialNotSetupError
)
export class CalendarServiceExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger("CalendarServiceExceptionFilter");
catch(exception: CalendarError, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const requestId = request.headers["X-Request-Id"];
this.logger.error(`Calendar Service Exception Filter: ${exception?.message}`, {
exception,
body: request.body,
headers: filterReqHeaders(request.headers),
url: request.url,
method: request.method,
requestId,
});
response.status(400).json({
status: ERROR_STATUS,
timestamp: new Date().toISOString(),
path: request.url,
error: { code: exception.name, message: exception.message },
});
}
}
+10 -9
View File
@@ -2,19 +2,20 @@ import { IncomingHttpHeaders } from "http";
export function filterReqHeaders(headers: IncomingHttpHeaders): Partial<IncomingHttpHeaders> {
return {
"Content-Type": headers["content-type"],
"User-Agent": headers["user-agent"],
"Content-Type": headers["Content-Type"] ?? headers["content-type"],
Authorization: headers["Authorization"] ?? headers["authorization"],
"X-Cal-Client-Id": headers["X-Cal-Client-Id"] ?? headers["x-cal-client-id"],
"Cal-Api-Version": headers["Cal-Api-Version"] ?? headers["cal-api-version"],
"X-Request-Id": headers["X-Request-Id"] ?? headers["x-request-id"],
// IP-related headers
"User-Agent": headers["User-agent"] ?? headers["user-agent"],
"X-Forwarded-For": headers["x-forwarded-for"],
"X-Forwarded-Host": headers["x-forwarded-host"],
"CF-Connecting-IP": headers["cf-connecting-ip"],
"CloudFront-Viewer-Address": headers["cloudfront-viewer-address"],
// Vercel headers
"x-vercel-id": headers["x-vercel-id"],
"x-vercel-deployment-url": headers["x-vercel-deployment-url"],
"x-vercel-country": headers["x-vercel-country"],
"x-vercel-region": headers["x-vercel-region"],
Host: headers["host"],
"X-Vercel-Id": headers["x-vercel-id"],
"X-Vercel-Deployment-Url": headers["x-vercel-deployment-url"],
"X-Vercel-Country": headers["x-vercel-country"],
"X-Vercel-Region": headers["x-vercel-region"],
Host: headers["Host"] ?? headers["host"],
};
}
+8
View File
@@ -32,3 +32,11 @@ export { addDelegationCredential };
export { enrichUserWithDelegationConferencingCredentialsWithoutOrgId } from "@calcom/lib/delegationCredential/server";
export { toggleDelegationCredentialEnabled } from "@calcom/trpc/server/routers/viewer/delegationCredential/toggleEnabled.handler";
export {
CalendarAppError,
CalendarAppDelegationCredentialInvalidGrantError,
CalendarAppDelegationCredentialError,
CalendarAppDelegationCredentialConfigurationError,
CalendarAppDelegationCredentialClientIdNotAuthorizedError,
CalendarAppDelegationCredentialNotSetupError,
} from "@calcom/lib/CalendarAppError";