feat: better error message

This commit is contained in:
DIYgod 2024-04-07 13:07:09 +08:00
parent cb69f5032c
commit 2a6d5ac9f9
No known key found for this signature in database
6 changed files with 35 additions and 27 deletions

View File

@ -13,6 +13,7 @@ import header from '@/middleware/header';
import antiHotlink from '@/middleware/anti-hotlink';
import parameter from '@/middleware/parameter';
import { jsxRenderer } from 'hono/jsx-renderer';
import { trimTrailingSlash } from 'hono/trailing-slash';
import logger from '@/utils/logger';
@ -26,6 +27,7 @@ process.on('uncaughtException', (e) => {
const app = new Hono();
app.use(trimTrailingSlash());
app.use(compress());
app.use(

View File

@ -22,7 +22,7 @@ describe('httperror', () => {
it(`httperror`, async () => {
const response = await request.get('/test/httperror');
expect(response.status).toBe(503);
expect(response.text).toMatch('404 Not Found: target website might be blocking our access, you can host your own RSSHub instance for a better usability.');
expect(response.text).toMatch('FetchError: [GET] "https://httpbingo.org/status/404": 404 Not Found');
}, 20000);
});
@ -31,7 +31,7 @@ describe('RequestInProgressError', () => {
const responses = await Promise.all([request.get('/test/slow'), request.get('/test/slow')]);
expect(new Set(responses.map((r) => r.status))).toEqual(new Set([200, 503]));
expect(new Set(responses.map((r) => r.headers['cache-control']))).toEqual(new Set([`public, max-age=${config.cache.routeExpire}`, `public, max-age=${config.requestTimeout / 1000}`]));
expect(responses.filter((r) => r.text.includes('This path is currently fetching, please come back later!'))).toHaveLength(1);
expect(responses.filter((r) => r.text.includes('RequestInProgressError: This path is currently fetching, please come back later!'))).toHaveLength(1);
});
});

View File

@ -5,8 +5,6 @@ import Sentry from '@sentry/node';
import logger from '@/utils/logger';
import Error from '@/views/error';
import RequestInProgressError from './request-in-progress';
import RejectError from './reject';
import NotFoundError from './not-found';
export const errorHandler: ErrorHandler = (error, ctx) => {
@ -38,27 +36,29 @@ export const errorHandler: ErrorHandler = (error, ctx) => {
});
}
let message = '';
if (error.name && (error.name === 'HTTPError' || error.name === 'RequestError' || error.name === 'FetchError')) {
ctx.status(503);
message = `${error.message}: target website might be blocking our access, you can host your own RSSHub instance for a better usability.`;
} else if (error instanceof RequestInProgressError) {
ctx.header('Cache-Control', `public, max-age=${config.requestTimeout / 1000}`);
ctx.status(503);
message = error.message;
} else if (error instanceof RejectError) {
ctx.status(403);
message = error.message;
} else if (error instanceof NotFoundError) {
ctx.status(404);
message = 'wrong path';
if (ctx.req.path.endsWith('/')) {
message += ', you can try removing the trailing slash in the path';
}
} else {
ctx.status(503);
message = process.env.NODE_ENV === 'production' ? error.message : error.stack || error.message;
let errorMessage = process.env.NODE_ENV === 'production' ? error.message : error.stack || error.message;
switch (error.constructor.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;
}
const message = `${error.name}: ${errorMessage}`;
logger.error(`Error in ${requestPath}: ${message}`);

View File

@ -1,3 +1,5 @@
class NotFoundError extends Error {}
class NotFoundError extends Error {
name = 'NotFoundError';
}
export default NotFoundError;

View File

@ -1,3 +1,5 @@
class RejectError extends Error {}
class RejectError extends Error {
name = 'RejectError';
}
export default RejectError;

View File

@ -1,3 +1,5 @@
class RequestInProgressError extends Error {}
class RequestInProgressError extends Error {
name = 'RequestInProgressError';
}
export default RequestInProgressError;