* revert: "fix: Request permissions to allow events to be created on shared Office365/Outlook calendars (#17760)" This reverts commit1680cba5a5. * plain.com card * detiled error handling for get customer * working email and id * hmac * Revert "fix: correct line-breaks in calendar event description (#18077)" This reverts commit06494a6999. * pr changes requet * remove pan for now * add-new-implementation-for-early-review * Pushing fix for createHmac stringify * added validation and user repository for email check * add apiRouteMiddleware which handles the error handling * HMAC_SECRET_KEY -> PLAIN_HMAC_SECRET_KEY * Use the right error * Convey right error to consumer * Fixup apiRouteMiddleware to handle handler * Don't export handler, only export POST * changed to app directory * working unkown user card --------- Co-authored-by: Keith Williams <keithwillcode@gmail.com> Co-authored-by: Omar López <zomars@me.com> Co-authored-by: Alex van Andel <me@alexvanandel.com>
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { captureException } from "@sentry/nextjs";
|
|
import { ApiError } from "next/dist/server/api-utils";
|
|
import type { NextRequest } from "next/server";
|
|
import { NextResponse } from "next/server";
|
|
|
|
import { getServerErrorFromUnknown } from "./getServerErrorFromUnknown";
|
|
|
|
export const apiRouteMiddleware =
|
|
(handler: (req: Request, res?: Response) => Promise<Response>) =>
|
|
async (req: NextRequest, res: NextResponse) => {
|
|
try {
|
|
return await handler(req, res);
|
|
} catch (error) {
|
|
if (error instanceof ApiError) {
|
|
return NextResponse.json({ message: error.message }, { status: error.statusCode });
|
|
} else {
|
|
const serverError = getServerErrorFromUnknown(error);
|
|
// we don't want to report Bad Request errors to Sentry / console
|
|
if (!(serverError.statusCode >= 400 && serverError.statusCode < 500)) {
|
|
console.error(error);
|
|
captureException(error);
|
|
}
|
|
return NextResponse.json(
|
|
{
|
|
message: serverError.message,
|
|
url: serverError.url,
|
|
method: serverError.method,
|
|
},
|
|
{
|
|
status: serverError.statusCode,
|
|
}
|
|
);
|
|
}
|
|
}
|
|
};
|