RSSHub/lib/errors/index.tsx

91 lines
3.0 KiB
TypeScript

import Honeybadger from '@honeybadger-io/js';
import type { ErrorHandler, NotFoundHandler } from 'hono';
import { routePath } from 'hono/route';
import { config } from '@/config';
import { getDebugInfo, setDebugInfo } from '@/utils/debug-info';
import logger from '@/utils/logger';
import { requestMetric } from '@/utils/otel';
import Error from '@/views/error';
import NotFoundError from './types/not-found';
const Sentry = config.sentry.dsn ? await import('@sentry/node') : undefined;
export const errorHandler: ErrorHandler = (error, ctx) => {
const requestPath = ctx.req.path;
const matchedRoute = routePath(ctx);
const hasMatchedRoute = matchedRoute !== '/*';
const debug = getDebugInfo();
try {
if (ctx.res.headers.get('RSSHub-Cache-Status')) {
debug.hitCache++;
}
} catch {
// ignore
}
debug.error++;
const errorPathCount = debug.errorPaths[requestPath];
if (!errorPathCount) {
debug.errorPaths[requestPath] = 0;
}
debug.errorPaths[requestPath]++;
const errorRouteCount = debug.errorRoutes[matchedRoute];
if (!errorRouteCount && hasMatchedRoute) {
debug.errorRoutes[matchedRoute] = 0;
}
hasMatchedRoute && debug.errorRoutes[matchedRoute]++;
setDebugInfo(debug);
if (config.honeybadger.apiKey) {
Honeybadger.notify(error, {
context: { name: requestPath.split('/', 2)[1] },
});
}
if (Sentry) {
Sentry.withScope((scope) => {
scope.setTag('name', requestPath.split('/', 2)[1]);
Sentry.captureException(error);
});
}
let errorMessage = (process.env.NODE_ENV || process.env.VERCEL_ENV) === 'production' || !error.stack ? `${error.name}: ${error.message}` : error.stack;
switch (error.name) {
case 'HTTPError':
case 'RequestError':
case 'FetchError':
ctx.status(503);
break;
case 'RequestInProgressError':
ctx.header('Cache-Control', `public, max-age=${config.requestTimeout / 1000}`);
ctx.status(503);
break;
case 'RejectError':
ctx.status(403);
break;
case 'NotFoundError':
ctx.status(404);
errorMessage += 'The route does not exist or has been deleted.';
break;
default:
ctx.status(503);
break;
}
logger.error(`Error in ${requestPath}: ${errorMessage}`);
requestMetric.error({ path: matchedRoute, method: ctx.req.method, status: ctx.res.status });
return config.isPackage || ctx.req.query('format') === 'json'
? ctx.json({
error: {
message: error.message ?? error,
},
})
: ctx.html(<Error requestPath={requestPath} message={errorMessage} errorRoute={hasMatchedRoute ? matchedRoute : requestPath} nodeVersion={process.version} />);
};
export const notFoundHandler: NotFoundHandler = (ctx) => errorHandler(new NotFoundError(), ctx);