mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-08 05:59:00 +08:00
feat: replace text
This commit is contained in:
@@ -15,19 +15,17 @@ module.exports = async (ctx, next) => {
|
|||||||
try {
|
try {
|
||||||
await next();
|
await next();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
let message;
|
let message = err;
|
||||||
if (err.name && (err.name === 'HTTPError' || err.name === 'RequestError')) {
|
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.`;
|
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) {
|
} else if (err instanceof Error) {
|
||||||
message = err.stack;
|
message = err.stack;
|
||||||
} else {
|
|
||||||
message = err;
|
|
||||||
}
|
}
|
||||||
logger.error(`Error in ${ctx.request.path}: ${message}`);
|
logger.error(`Error in ${ctx.request.path}: ${message}`);
|
||||||
ctx.set({
|
ctx.set({
|
||||||
'Content-Type': 'text/html; charset=UTF-8',
|
'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;
|
ctx.status = 404;
|
||||||
|
|
||||||
if (!ctx.debug.errorPaths[ctx.request.path]) {
|
if (!ctx.debug.errorPaths[ctx.request.path]) {
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
const config = require('@/config');
|
const config = require('@/config');
|
||||||
let cacheIndex = 0;
|
let cacheIndex = 0;
|
||||||
|
const got = require('@/utils/got');
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
if (ctx.params.id === '0') {
|
if (ctx.params.id === 'error') {
|
||||||
throw Error('Error test');
|
throw Error('Error test');
|
||||||
}
|
}
|
||||||
|
if (ctx.params.id === 'httperror') {
|
||||||
|
await got({
|
||||||
|
method: 'get',
|
||||||
|
url: `https://google.com/404`,
|
||||||
|
});
|
||||||
|
}
|
||||||
let item = [];
|
let item = [];
|
||||||
if (ctx.params.id === 'long') {
|
if (ctx.params.id === 'long') {
|
||||||
item.push({
|
item.push({
|
||||||
|
|||||||
@@ -8,8 +8,18 @@ afterAll(() => {
|
|||||||
|
|
||||||
describe('error', () => {
|
describe('error', () => {
|
||||||
it(`error`, async () => {
|
it(`error`, async () => {
|
||||||
const response = await request.get('/test/0');
|
const response = await request.get('/test/error');
|
||||||
expect(response.status).toBe(404);
|
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\./
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ describe('empty', () => {
|
|||||||
it(`empty`, async () => {
|
it(`empty`, async () => {
|
||||||
const response1 = await request.get('/test/empty');
|
const response1 = await request.get('/test/empty');
|
||||||
expect(response1.status).toBe(404);
|
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');
|
const response2 = await request.get('/test/1?limit=0');
|
||||||
expect(response2.status).toBe(200);
|
expect(response2.status).toBe(200);
|
||||||
@@ -132,7 +132,7 @@ describe('wrong_path', () => {
|
|||||||
it(`wrong_path`, async () => {
|
it(`wrong_path`, async () => {
|
||||||
const response = await request.get('/wrong');
|
const response = await request.get('/wrong');
|
||||||
expect(response.status).toBe(404);
|
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/);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ describe('template', () => {
|
|||||||
it(`.json`, async () => {
|
it(`.json`, async () => {
|
||||||
const response = await request.get('/test/1.json');
|
const response = await request.get('/test/1.json');
|
||||||
expect(response.status).toBe(404);
|
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 () => {
|
it(`long title`, async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user