feat: new error page

This commit is contained in:
DIYgod
2024-03-06 02:33:28 +08:00
parent 641a8e62fb
commit 94cf0742af
4 changed files with 150 additions and 116 deletions
+3 -5
View File
@@ -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 <a href="https:\/\/docs\.rsshub\.app\/install\/">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);
});
+35 -46
View File
@@ -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 <a href="https://docs.rsshub.app/install/">host your own RSSHub instance</a> 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((
<Error
requestPath={requestPath}
message={message}
errorRoute={hasMatchedRoute ? matchedRoute : requestPath}
nodeVersion={process.version}
/>
));
};
export const notFoundHandler: NotFoundHandler = (ctx) => errorHandler(new NotFoundError(), ctx);
-65
View File
@@ -1,65 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Error in RSSHub!</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-size: 16px;
color: #2c3e50;
}
.content {
width: 800px;
margin: 100px auto 0;
text-align: center;
}
.message {
white-space: pre-wrap;
text-align: left;
}
a {
color: #F5712C;
}
img {
filter: grayscale(1);
}
</style>
</head>
<body>
<div class="content">
<p>
<img src="/logo.png" alt="RSSHub" width="120" loading="lazy" />
</p>
<h1>Looks like something went wrong</h1>
<pre class="message">Path requested: {{@ requestPath }}</pre>
<pre class="message">Error message: {{@ message }}</pre>
<pre class="message">
Helpful Information to provide when opening issue:
Route: {{@ errorRoute }}
Node version: {{@ nodeVersion }}
Git Hash: {{@ gitHash }}
</pre>
<pre class="message">
If you believe this is an error caused by RSSHub, please <a href="https://github.com/DIYgod/RSSHub/issues/new?assignees=&labels=RSS+bug&template=bug_report_en.yml" target="_blank">report on github</a>
如果您认为 RSSHub 导致了该错误,请在 GitHub 按照模版,复制本页面信息进行 <a href="https://github.com/DIYgod/RSSHub/issues/new?assignees=&labels=RSS+bug&template=bug_report_zh.yml" target="_blank">汇报</a>
</pre>
<div style="margin: 50px 0">
<p>For online documentation and support please refer to
<a href="https://docs.rsshub.app/" target="_blank">docs.rsshub.app</a>.</p>
<p>在线文档与支持,请访问 <a href="https://docs.rsshub.app/" target="_blank">docs.rsshub.app</a>。</p>
</div>
</div>
</body>
</html>
+112
View File
@@ -0,0 +1,112 @@
import type { FC } from 'hono/jsx';
import { gitHash, gitDate } from '@/utils/git-hash';
const Layout: FC = (props) => (
<html>
<head>
<title>Welcome to RSSHub!</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
{`
details::-webkit-scrollbar {
width: 0.25rem;
}
details::-webkit-scrollbar-thumb {
border-radius: 0.125rem;
background-color: #e4e4e7;
}
details::-webkit-scrollbar-thumb:hover {
background-color: #a1a1aa;
}`}
</style>
</head>
<body className="antialiased text-zinc-700">{props.children}</body>
</html>
);
const Index: FC<{
requestPath: string;
message: string;
errorRoute: string;
nodeVersion: string;
}> = ({
requestPath,
message,
errorRoute,
nodeVersion,
}) => (
<Layout>
<div
className="pointer-events-none absolute w-full h-screen"
style={{
backgroundImage: `url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHZpZXdCb3g9JzAgMCAzMiAzMicgd2lkdGg9JzMyJyBoZWlnaHQ9JzMyJyBmaWxsPSdub25lJyBzdHJva2U9J3JnYigxNSAyMyA0MiAvIDAuMDQpJz48cGF0aCBkPSdNMCAuNUgzMS41VjMyJy8+PC9zdmc+')`,
maskImage: 'linear-gradient(transparent, black, transparent)',
}}
></div>
<div className="w-full h-screen flex items-center justify-center flex-col space-y-4">
<img className="grayscale" src="/logo.png" alt="RSSHub" width="100" loading="lazy" />
<h1 className="text-4xl font-bold">
Looks like something went wrong
</h1>
<div className="text-left w-[800px] space-y-6 !mt-10">
<div className="space-y-2">
<p className="mb-2 font-bold">Helpful Information</p>
<p>Error Message:</p>
<code className="mt-2 block max-h-28 overflow-auto bg-zinc-100 align-bottom w-fit details">{message}</code>
<p>Route: <code className="ml-2 bg-zinc-100">{errorRoute}</code></p>
<p>Full Route: <code className="ml-2 bg-zinc-100">{requestPath}</code></p>
<p>Node Version: <code className="ml-2 bg-zinc-100">{nodeVersion}</code></p>
<p>Git Hash: <code className="ml-2 bg-zinc-100">{gitHash}</code></p>
<p>Git Date: <code className="ml-2 bg-zinc-100">{gitDate?.toUTCString()}</code></p>
</div>
<div>
<p className="mb-2 font-bold">Report</p>
<p>After carefully reading the <a className="text-[#F5712C]" href="https://docs.rsshub.app/" target="_blank">document</a>, if you think this is a bug of RSSHub, please <a className="text-[#F5712C]" href="https://github.com/DIYgod/RSSHub/issues/new?assignees=&labels=RSS+bug&template=bug_report_en.yml" target="_blank">submit an issue</a> on GitHub.</p>
<p>在仔细阅读<a className="text-[#F5712C]" href="https://docs.rsshub.app/zh/" target="_blank">文档</a>后,如果你认为这是 RSSHub 的 bug,请在 GitHub <a className="text-[#F5712C]" href="https://github.com/DIYgod/RSSHub/issues/new?assignees=&labels=RSS+bug&template=bug_report_zh.yml" target="_blank">提交 issue</a>。</p>
</div>
<div>
<p className="mb-2 font-bold">Community</p>
<p>You can also join our <a className="text-[#F5712C]" target="_blank" href="https://t.me/rsshub">Telegram group</a>, or follow our <a className="text-[#F5712C]" target="_blank" href="https://t.me/awesomeRSSHub">Telegram channel</a> and <a target="_blank" href="https://twitter.com/intent/follow?screen_name=_RSSHub" className="text-[#F5712C]">Twitter</a> to get community support and news.</p>
<p>你也可以加入我们的 <a className="text-[#F5712C]" target="_blank" href="https://t.me/rsshub">Telegram 群组</a>,或关注我们的 <a className="text-[#F5712C]" target="_blank" href="https://t.me/awesomeRSSHub">Telegram 频道</a>和 <a target="_blank" href="https://twitter.com/intent/follow?screen_name=_RSSHub" className="text-[#F5712C]">Twitter</a> 获取社区支持和新闻。</p>
</div>
</div>
</div>
<div className="absolute bottom-10 text-center w-full text-sm font-medium space-y-2">
<p className="space-x-4">
<a target="_blank" href="https://github.com/DIYgod/RSSHub">
<img className="inline" src="https://icons.ly/github/_/fff" alt="github" width="20" height="20" />
</a>
<a target="_blank" href="https://t.me/rsshub">
<img className="inline" src="https://icons.ly/telegram" alt="telegram group" width="20" height="20" />
</a>
<a target="_blank" href="https://t.me/awesomeRSSHub">
<img className="inline" src="https://icons.ly/telegram" alt="telegram channel" width="20" height="20" />
</a>
<a target="_blank" href="https://twitter.com/intent/follow?screen_name=_RSSHub" className="text-[#F5712C]">
<img className="inline" src="https://icons.ly/twitter" alt="github" width="20" height="20" />
</a>
</p>
<p className="!mt-6">
Please consider{' '}
<a target="_blank" href="https://docs.rsshub.app/support" className="text-[#F5712C]">
sponsoring
</a>{' '}
to help keep this open source project alive.
</p>
<p>
Made with ❤️ by{' '}
<a target="_blank" href="https://diygod.cc" className="text-[#F5712C]">
DIYgod
</a>{' '}
and{' '}
<a target="_blank" href="https://github.com/DIYgod/RSSHub/graphs/contributors" className="text-[#F5712C]">
Contributors
</a>{' '}
under MIT License.
</p>
</div>
</Layout>
);
export default Index;