feat: replace text

This commit is contained in:
DIYgod
2019-09-04 14:28:29 +08:00
parent f10c66a7db
commit 0af16097b3
5 changed files with 25 additions and 10 deletions

View File

@@ -15,19 +15,17 @@ module.exports = async (ctx, next) => {
try {
await next();
} catch (err) {
let message;
let message = err;
if (err.name && (err.name === 'HTTPError' || err.name === 'RequestError')) {
message = `${err.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 (err instanceof Error) {
message = err.stack;
} else {
message = err;
}
logger.error(`Error in ${ctx.request.path}: ${message}`);
ctx.set({
'Content-Type': 'text/html; charset=UTF-8',
});
ctx.body = `RSSHub 发生了一些意外: <pre>${message}</pre>`;
ctx.body = `Looks like something went wrong in RSSHub: <pre>${message}</pre>`;
ctx.status = 404;
if (!ctx.debug.errorPaths[ctx.request.path]) {

View File

@@ -1,10 +1,17 @@
const config = require('@/config');
let cacheIndex = 0;
const got = require('@/utils/got');
module.exports = async (ctx) => {
if (ctx.params.id === '0') {
if (ctx.params.id === 'error') {
throw Error('Error test');
}
if (ctx.params.id === 'httperror') {
await got({
method: 'get',
url: `https://google.com/404`,
});
}
let item = [];
if (ctx.params.id === 'long') {
item.push({

View File

@@ -8,8 +8,18 @@ afterAll(() => {
describe('error', () => {
it(`error`, async () => {
const response = await request.get('/test/0');
const response = await request.get('/test/error');
expect(response.status).toBe(404);
expect(response.text).toMatch(/RSSHub 发生了一些意外: <pre>Error: Error test/);
expect(response.text).toMatch(/Looks like something went wrong in RSSHub: <pre>Error: Error test/);
});
});
describe('httperror', () => {
it(`httperror`, async () => {
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\./
);
});
});

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(/RSSHub 发生了一些意外: <pre>Error: this route is empty/);
expect(response1.text).toMatch(/Looks like something went wrong in RSSHub: <pre>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(/RSSHub 发生了一些意外: <pre>Error: wrong path/);
expect(response.text).toMatch(/Looks like something went wrong in RSSHub: <pre>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(/RSSHub 发生了一些意外: <pre>Error: <b>JSON output had been removed/);
expect(response.text).toMatch(/Looks like something went wrong in RSSHub: <pre>Error: <b>JSON output had been removed/);
});
it(`long title`, async () => {