mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-03 02:28:23 +08:00
feat(pkg): error handling
This commit is contained in:
@@ -1,16 +1,9 @@
|
|||||||
const art = require('art-template');
|
|
||||||
const path = require('path');
|
|
||||||
const config = require('@/config').value;
|
const config = require('@/config').value;
|
||||||
|
|
||||||
const reject = (ctx) => {
|
const reject = (ctx) => {
|
||||||
ctx.response.status = 403;
|
ctx.response.status = 403;
|
||||||
ctx.body = art(path.resolve(__dirname, '../views/rss.art'), {
|
|
||||||
lastBuildDate: new Date().toUTCString(),
|
throw Error('Access denied.');
|
||||||
updated: new Date().toISOString(),
|
|
||||||
ttl: 24 * 60 * 60,
|
|
||||||
title: '没有访问权限. Access denied.',
|
|
||||||
link: 'https://docs.rsshub.app/install/#%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6',
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = async (ctx, next) => {
|
module.exports = async (ctx, next) => {
|
||||||
|
|||||||
@@ -26,13 +26,24 @@ module.exports = async (ctx, next) => {
|
|||||||
message = err.stack;
|
message = err.stack;
|
||||||
}
|
}
|
||||||
logger.error(`Error in ${ctx.request.path}: ${message}`);
|
logger.error(`Error in ${ctx.request.path}: ${message}`);
|
||||||
|
|
||||||
|
if (config.isPackage) {
|
||||||
|
ctx.body = {
|
||||||
|
error: {
|
||||||
|
message: err.message,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} else {
|
||||||
ctx.set({
|
ctx.set({
|
||||||
'Content-Type': 'text/html; charset=UTF-8',
|
'Content-Type': 'text/html; charset=UTF-8',
|
||||||
});
|
});
|
||||||
ctx.body = art(path.resolve(__dirname, '../views/error.art'), {
|
ctx.body = art(path.resolve(__dirname, '../views/error.art'), {
|
||||||
message,
|
message,
|
||||||
});
|
});
|
||||||
|
if (ctx.status !== 403) {
|
||||||
ctx.status = 404;
|
ctx.status = 404;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!ctx.debug.errorPaths[ctx.request.path]) {
|
if (!ctx.debug.errorPaths[ctx.request.path]) {
|
||||||
ctx.debug.errorPaths[ctx.request.path] = 0;
|
ctx.debug.errorPaths[ctx.request.path] = 0;
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ module.exports = {
|
|||||||
app = require('./app');
|
app = require('./app');
|
||||||
},
|
},
|
||||||
request: (path) =>
|
request: (path) =>
|
||||||
new Promise((resolve) => {
|
new Promise((resolve, reject) => {
|
||||||
app.callback()(
|
app.callback()(
|
||||||
{
|
{
|
||||||
url: path,
|
url: path,
|
||||||
@@ -26,7 +26,12 @@ module.exports = {
|
|||||||
setHeader: () => {},
|
setHeader: () => {},
|
||||||
removeHeader: () => {},
|
removeHeader: () => {},
|
||||||
end: (data) => {
|
end: (data) => {
|
||||||
|
data = JSON.parse(data);
|
||||||
|
if (data.error) {
|
||||||
|
reject(data.error.message);
|
||||||
|
} else {
|
||||||
resolve(JSON.parse(data));
|
resolve(JSON.parse(data));
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
const supertest = require('supertest');
|
const supertest = require('supertest');
|
||||||
const Parser = require('rss-parser');
|
|
||||||
const parser = new Parser();
|
|
||||||
let server;
|
let server;
|
||||||
|
|
||||||
async function checkBlock(response) {
|
async function checkBlock(response) {
|
||||||
expect(response.status).toBe(403);
|
expect(response.status).toBe(403);
|
||||||
expect(await parser.parseString(response.text)).toMatchObject({
|
expect(response.text).toMatch(/Access denied\./);
|
||||||
items: [],
|
|
||||||
title: '没有访问权限. Access denied.',
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user