From 94cf0742afa8bf18510ad9ded9b76dcd2ad52c90 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 6 Mar 2024 02:33:28 +0800 Subject: [PATCH] feat: new error page --- lib/errors/index.test.ts | 8 +-- lib/errors/{index.ts => index.tsx} | 81 +++++++++------------ lib/views/error.art | 65 ----------------- lib/views/error.tsx | 112 +++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+), 116 deletions(-) rename lib/errors/{index.ts => index.tsx} (53%) delete mode 100644 lib/views/error.art create mode 100644 lib/views/error.tsx diff --git a/lib/errors/index.test.ts b/lib/errors/index.test.ts index b493ab740..a89a84af6 100644 --- a/lib/errors/index.test.ts +++ b/lib/errors/index.test.ts @@ -13,7 +13,7 @@ afterAll(() => { describe('error', () => { it(`error`, async () => { const response = await request.get('/test/error'); - expect(response.status).toBe(404); + expect(response.status).toBe(503); expect(response.text).toMatch(/Error: Error test/); }); }); @@ -21,10 +21,8 @@ describe('error', () => { describe('httperror', () => { it(`httperror`, async () => { const response = await request.get('/test/httperror'); - expect(response.status).toBe(404); - expect(response.text).toMatch( - /Response code 404 \(Not Found\): target website might be blocking our access, you can host your own RSSHub instance<\/a> for a better usability\./ - ); + expect(response.status).toBe(503); + expect(response.text).toMatch('Response code 404 (Not Found): target website might be blocking our access, you can host your own RSSHub instance for a better usability.'); }, 20000); }); diff --git a/lib/errors/index.ts b/lib/errors/index.tsx similarity index 53% rename from lib/errors/index.ts rename to lib/errors/index.tsx index dd69b50ff..f2ff4bde7 100644 --- a/lib/errors/index.ts +++ b/lib/errors/index.tsx @@ -3,27 +3,16 @@ import { getDebugInfo, setDebugInfo } from '@/utils/debug-info'; import { config } from '@/config'; import Sentry from '@sentry/node'; import logger from '@/utils/logger'; -import art from 'art-template'; -import * as path from 'node:path'; -import { gitHash } from '@/utils/git-hash'; +import Error from '@/views/error'; import RequestInProgressError from './request-in-progress'; import RejectError from './reject'; import NotFoundError from './not-found'; -import { getCurrentPath } from '@/utils/helpers'; -const __dirname = getCurrentPath(import.meta.url); - export const errorHandler: ErrorHandler = (error, ctx) => { const requestPath = ctx.req.path; const matchedRoute = ctx.req.routePath; const hasMatchedRoute = matchedRoute !== '/*'; - let message = ''; - if (error.name && (error.name === 'HTTPError' || error.name === 'RequestError')) { - 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 Error) { - message = process.env.NODE_ENV === 'production' ? error.message : error.stack || error.message; - } const debug = getDebugInfo(); if (ctx.res.headers.get('RSSHub-Cache-Status')) { @@ -49,42 +38,42 @@ export const errorHandler: ErrorHandler = (error, ctx) => { }); } + let message = ''; + if (error.name && (error.name === 'HTTPError' || error.name === 'RequestError')) { + 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; + } + logger.error(`Error in ${requestPath}: ${message}`); - if (config.isPackage) { - return ctx.json({ - error: { - message: error.message ?? error, - }, - }); - } 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(404); - } - - return ctx.html( - art(path.resolve(__dirname, '../views/error.art'), { - requestPath, - message, - errorRoute: hasMatchedRoute ? matchedRoute : requestPath, - nodeVersion: process.version, - gitHash, - }) - ); - } + return config.isPackage ? ctx.json({ + error: { + message: error.message ?? error, + }, + }) : ctx.html(( + + )); }; export const notFoundHandler: NotFoundHandler = (ctx) => errorHandler(new NotFoundError(), ctx); diff --git a/lib/views/error.art b/lib/views/error.art deleted file mode 100644 index 3a98d2d86..000000000 --- a/lib/views/error.art +++ /dev/null @@ -1,65 +0,0 @@ - - - - - Error in RSSHub! - - - - -
-

- RSSHub -

- -

Looks like something went wrong

- -
Path requested: {{@ requestPath }}
-
Error message: {{@ message }}
-
-Helpful Information to provide when opening issue:
-Route: {{@ errorRoute }}
-Node version: {{@ nodeVersion }}
-Git Hash: {{@ gitHash }}
-        
-
-If you believe this is an error caused by RSSHub, please report on github
-如果您认为 RSSHub 导致了该错误,请在 GitHub 按照模版,复制本页面信息进行 汇报
-        
- -
-

For online documentation and support please refer to - docs.rsshub.app.

-

在线文档与支持,请访问 docs.rsshub.app。

-
-
- - - diff --git a/lib/views/error.tsx b/lib/views/error.tsx new file mode 100644 index 000000000..be2f75b1b --- /dev/null +++ b/lib/views/error.tsx @@ -0,0 +1,112 @@ +import type { FC } from 'hono/jsx'; + +import { gitHash, gitDate } from '@/utils/git-hash'; + +const Layout: FC = (props) => ( + + + Welcome to RSSHub! + + + + {props.children} + +); + +const Index: FC<{ + requestPath: string; + message: string; + errorRoute: string; + nodeVersion: string; +}> = ({ + requestPath, + message, + errorRoute, + nodeVersion, +}) => ( + +
+
+ RSSHub +

+ Looks like something went wrong +

+
+
+

Helpful Information

+

Error Message:

+ {message} +

Route: {errorRoute}

+

Full Route: {requestPath}

+

Node Version: {nodeVersion}

+

Git Hash: {gitHash}

+

Git Date: {gitDate?.toUTCString()}

+
+
+

Report

+

After carefully reading the document, if you think this is a bug of RSSHub, please submit an issue on GitHub.

+

在仔细阅读文档后,如果你认为这是 RSSHub 的 bug,请在 GitHub 提交 issue。

+
+
+

Community

+

You can also join our Telegram group, or follow our Telegram channel and Twitter to get community support and news.

+

你也可以加入我们的 Telegram 群组,或关注我们的 Telegram 频道和 Twitter 获取社区支持和新闻。

+
+
+
+
+

+ + github + + + telegram group + + + telegram channel + + + github + +

+

+ Please consider{' '} + + sponsoring + {' '} + to help keep this open source project alive. +

+

+ Made with ❤️ by{' '} + + DIYgod + {' '} + and{' '} + + Contributors + {' '} + under MIT License. +

+
+
+); + +export default Index;