From 0311c28c84212652cf5e6a4c732c93cafc5a2dd5 Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Wed, 28 Apr 2021 10:41:17 +0200 Subject: [PATCH] fix: Correct imports --- .../nextjs/src/utils/api-wrapping-loader.ts | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/nextjs/src/utils/api-wrapping-loader.ts b/packages/nextjs/src/utils/api-wrapping-loader.ts index 913f89bef6c3..db291328afc6 100644 --- a/packages/nextjs/src/utils/api-wrapping-loader.ts +++ b/packages/nextjs/src/utils/api-wrapping-loader.ts @@ -80,13 +80,24 @@ function getOptions(loaders: Loader[]): LoaderOptions | undefined { /** Wrap the given request handler for error-catching purposes */ function makeWrappedRequestHandler(origHandler: RequestHandler): WrappedRequestHandler { // TODO are there any overloads we need to worry about? - return async (req: NextApiRequest, res: NextApiResponse): Promise => { - try { - return await origHandler(req, res); - } catch (err) { - Sentry.captureException(err); - await Sentry.flush(2000); + return (req: NextApiRequest, res: NextApiResponse): Promise => { + return origHandler(req, res).catch(err => { + if (Sentry !== undefined) { + Sentry.withScope(scope => { + scope.addEventProcessor(event => Sentry.Handlers.parseRequest(event, req)); + Sentry.captureException(err); + // eslint-disable-next-line no-console + }); + Sentry.flush(2000).catch(() => { + // Never throws + }); + } else { + // eslint-disable-next-line no-console + console.error( + "[Sentry] Please make sure to `import * as Sentry from '@sentry/nextjs';` in your file in order to capture errors", + ); + } throw err; - } + }); }; }