diff --git a/lib/app.tsx b/lib/app.tsx index 53e85e2a5..65feef3ab 100644 --- a/lib/app.tsx +++ b/lib/app.tsx @@ -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( diff --git a/lib/errors/index.test.ts b/lib/errors/index.test.ts index f76af6219..dc58186aa 100644 --- a/lib/errors/index.test.ts +++ b/lib/errors/index.test.ts @@ -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); }); }); diff --git a/lib/errors/index.tsx b/lib/errors/index.tsx index 2f16d0512..77a8f0831 100644 --- a/lib/errors/index.tsx +++ b/lib/errors/index.tsx @@ -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}`); diff --git a/lib/errors/not-found.ts b/lib/errors/not-found.ts index 2ba6e18b3..9cba16b31 100644 --- a/lib/errors/not-found.ts +++ b/lib/errors/not-found.ts @@ -1,3 +1,5 @@ -class NotFoundError extends Error {} +class NotFoundError extends Error { + name = 'NotFoundError'; +} export default NotFoundError; diff --git a/lib/errors/reject.ts b/lib/errors/reject.ts index 629648226..b6b91fe49 100644 --- a/lib/errors/reject.ts +++ b/lib/errors/reject.ts @@ -1,3 +1,5 @@ -class RejectError extends Error {} +class RejectError extends Error { + name = 'RejectError'; +} export default RejectError; diff --git a/lib/errors/request-in-progress.ts b/lib/errors/request-in-progress.ts index 99118977e..73ae4b570 100644 --- a/lib/errors/request-in-progress.ts +++ b/lib/errors/request-in-progress.ts @@ -1,3 +1,5 @@ -class RequestInProgressError extends Error {} +class RequestInProgressError extends Error { + name = 'RequestInProgressError'; +} export default RequestInProgressError;