feat: error page

This commit is contained in:
DIYgod
2019-09-04 16:00:00 +08:00
parent dc37a779e4
commit 24cab914d8
7 changed files with 63 additions and 11 deletions

View File

@@ -1,6 +1,8 @@
const logger = require('@/utils/logger');
const config = require('@/config');
const Sentry = require('@sentry/node');
const art = require('art-template');
const path = require('path');
if (config.sentry) {
Sentry.init({
@@ -25,7 +27,9 @@ module.exports = async (ctx, next) => {
ctx.set({
'Content-Type': 'text/html; charset=UTF-8',
});
ctx.body = `Looks like something went wrong in RSSHub: <pre>${message}</pre>`;
ctx.body = art(path.resolve(__dirname, '../views/error.art'), {
message,
});
ctx.status = 404;
if (!ctx.debug.errorPaths[ctx.request.path]) {

View File

@@ -12,7 +12,7 @@ module.exports = async (ctx, next) => {
if (ctx.state.data) {
if ((!ctx.state.data.item || ctx.state.data.item.length === 0) && !ctx.state.data.allowEmpty) {
throw Error('this route is empty, please check the original site or create an issue on https://github.com/DIYgod/RSSHub/issues/new/choose');
throw Error('this route is empty, please check the original site or <a href="https://github.com/DIYgod/RSSHub/issues/new/choose">create an issue</a>');
}
if (ctx.query && ctx.query.mode && ctx.query.mode.toLowerCase() === 'fulltext') {

47
lib/views/error.art Normal file
View File

@@ -0,0 +1,47 @@
<!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;
}
</style>
</head>
<body>
<div class="content">
<p>
<img src="https://i.loli.net/2019/09/04/lmBbNQoLzrKGgiM.jpg" alt="RSSHub" width="120">
</p>
<h1>Looks like something went wrong</h1>
<pre class="message">{{@ message }}</pre>
<p style="margin: 50px 0">For online documentation and support please refer to
<a href="https://docs.rsshub.app/" target="_blank">docs.rsshub.app</a>.</p>
</div>
</body>
</html>

View File

@@ -5,8 +5,6 @@
<title>Welcome to RSSHub!</title>
<style>
body {
width: 35em;
margin: 0 auto;
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;
@@ -16,13 +14,16 @@
.content {
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
transform: translate(-50%, 100px);
min-width: 400px;
text-align: center;
}
a {
color: #F5712C;
}
details {
text-align: left;
max-height: 400px;

View File

@@ -10,7 +10,7 @@ describe('error', () => {
it(`error`, async () => {
const response = await request.get('/test/error');
expect(response.status).toBe(404);
expect(response.text).toMatch(/Looks like something went wrong in RSSHub: <pre>Error: Error test/);
expect(response.text).toMatch(/Error: Error test/);
});
});
@@ -19,7 +19,7 @@ describe('httperror', () => {
const response = await request.get('/test/httperror');
expect(response.status).toBe(404);
expect(response.text).toMatch(
/Looks like something went wrong in RSSHub: <pre>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\./
/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\./
);
});
});

View File

@@ -110,7 +110,7 @@ describe('empty', () => {
it(`empty`, async () => {
const response1 = await request.get('/test/empty');
expect(response1.status).toBe(404);
expect(response1.text).toMatch(/Looks like something went wrong in RSSHub: <pre>Error: this route is empty/);
expect(response1.text).toMatch(/Error: this route is empty/);
const response2 = await request.get('/test/1?limit=0');
expect(response2.status).toBe(200);
@@ -132,7 +132,7 @@ describe('wrong_path', () => {
it(`wrong_path`, async () => {
const response = await request.get('/wrong');
expect(response.status).toBe(404);
expect(response.text).toMatch(/Looks like something went wrong in RSSHub: <pre>Error: wrong path/);
expect(response.text).toMatch(/Error: wrong path/);
});
});

View File

@@ -60,7 +60,7 @@ describe('template', () => {
it(`.json`, async () => {
const response = await request.get('/test/1.json');
expect(response.status).toBe(404);
expect(response.text).toMatch(/Looks like something went wrong in RSSHub: <pre>Error: <b>JSON output had been removed/);
expect(response.text).toMatch(/Error: <b>JSON output had been removed/);
});
it(`long title`, async () => {