Commit 156cde08 authored by tom's avatar tom

add sentry to api routes

parent 8e6a3dde
...@@ -4,6 +4,7 @@ NEXT_PUBLIC_SENTRY_DSN=xxx ...@@ -4,6 +4,7 @@ NEXT_PUBLIC_SENTRY_DSN=xxx
SENTRY_ORG=block-scout SENTRY_ORG=block-scout
SENTRY_PROJECT=new-ui SENTRY_PROJECT=new-ui
SENTRY_AUTH_TOKEN=xxx SENTRY_AUTH_TOKEN=xxx
SENTRY_IGNORE_API_RESOLUTION_ERROR=1
NEXT_PUBLIC_BLOCKSCOUT_VERSION=xxx NEXT_PUBLIC_BLOCKSCOUT_VERSION=xxx
NEXT_PUBLIC_FOOTER_GITHUB_LINK=https://github.com/blockscout/blockscout NEXT_PUBLIC_FOOTER_GITHUB_LINK=https://github.com/blockscout/blockscout
NEXT_PUBLIC_FOOTER_TWITTER_LINK=https://www.twitter.com/blockscoutcom NEXT_PUBLIC_FOOTER_TWITTER_LINK=https://www.twitter.com/blockscoutcom
......
import { withSentry } from '@sentry/nextjs';
import type { NextApiRequest, NextApiResponse } from 'next'; import type { NextApiRequest, NextApiResponse } from 'next';
import fetch from 'lib/api/fetch'; import fetch from 'lib/api/fetch';
...@@ -5,33 +6,39 @@ import getUrlWithNetwork from 'lib/api/getUrlWithNetwork'; ...@@ -5,33 +6,39 @@ import getUrlWithNetwork from 'lib/api/getUrlWithNetwork';
type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE'; type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE';
export default function handler(getUrl: (_req: NextApiRequest) => string, allowedMethods: Array<Methods>) { export default function createHandler(getUrl: (_req: NextApiRequest) => string, allowedMethods: Array<Methods>) {
return async(_req: NextApiRequest, res: NextApiResponse) => { const handler = async(_req: NextApiRequest, res: NextApiResponse) => {
if (_req.method && allowedMethods.includes(_req.method as Methods)) { if (!_req.method || !allowedMethods.includes(_req.method as Methods)) {
const isBodyDisallowed = _req.method === 'GET' || _req.method === 'HEAD'; res.setHeader('Allow', allowedMethods);
res.status(405).end(`Method ${ _req.method } Not Allowed`);
const url = getUrlWithNetwork(_req, `/api${ getUrl(_req) }`); return;
const response = await fetch(url, { }
method: _req.method,
body: isBodyDisallowed ? undefined : _req.body,
});
if (response.status !== 200) { const isBodyDisallowed = _req.method === 'GET' || _req.method === 'HEAD';
try {
const error = await response.json() as { errors: unknown };
res.status(500).json(error?.errors || {});
} catch (error) {
res.status(500).json({ statusText: response.statusText, status: response.status });
}
return; const url = getUrlWithNetwork(_req, `/api${ getUrl(_req) }`);
} const response = await fetch(url, {
method: _req.method,
body: isBodyDisallowed ? undefined : _req.body,
});
if (response.status === 200) {
const data = await response.json(); const data = await response.json();
res.status(200).json(data); res.status(200).json(data);
} else { return;
res.setHeader('Allow', allowedMethods);
res.status(405).end(`Method ${ _req.method } Not Allowed`);
} }
let responseError;
try {
const error = await response.json() as { errors: unknown };
responseError = error?.errors || {};
} catch (error) {
responseError = { statusText: response.statusText, status: response.status };
}
res.status(500).json(responseError);
}; };
return withSentry(handler);
} }
...@@ -5,7 +5,14 @@ const path = require('path'); ...@@ -5,7 +5,14 @@ const path = require('path');
const moduleExports = { const moduleExports = {
include: path.resolve(__dirname, 'icons'), include: path.resolve(__dirname, 'icons'),
reactStrictMode: true, reactStrictMode: true,
webpack(config) { webpack(config, { webpack }) {
config.plugins.push(
new webpack.DefinePlugin({
__SENTRY_DEBUG__: false,
__SENTRY_TRACING__: false,
}),
);
return config; return config;
}, },
async redirects() { async redirects() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment