2022-03-29 00:25:24 +00:00
|
|
|
import * as Sentry from "@sentry/nextjs";
|
2023-02-16 21:01:40 +00:00
|
|
|
import type { NextMiddleware } from "next-api-middleware";
|
2022-03-29 00:25:24 +00:00
|
|
|
|
2023-08-03 15:22:38 +00:00
|
|
|
import { redactError } from "@calcom/lib/redactError";
|
|
|
|
|
2022-03-29 00:25:24 +00:00
|
|
|
export const captureErrors: NextMiddleware = async (_req, res, next) => {
|
|
|
|
try {
|
|
|
|
// Catch any errors that are thrown in remaining
|
|
|
|
// middleware and the API route handler
|
|
|
|
await next();
|
2022-04-08 16:08:26 +00:00
|
|
|
} catch (error) {
|
|
|
|
Sentry.captureException(error);
|
2023-08-03 15:22:38 +00:00
|
|
|
const redactedError = redactError(error);
|
|
|
|
if (redactedError instanceof Error) {
|
|
|
|
res.status(400).json({ message: redactedError.message, error: redactedError });
|
|
|
|
return;
|
|
|
|
}
|
2022-04-08 16:08:26 +00:00
|
|
|
res.status(400).json({ message: "Something went wrong", error });
|
2022-03-29 00:25:24 +00:00
|
|
|
}
|
|
|
|
};
|