mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-02 18:18:06 +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 reject = (ctx) => {
|
||||
ctx.response.status = 403;
|
||||
ctx.body = art(path.resolve(__dirname, '../views/rss.art'), {
|
||||
lastBuildDate: new Date().toUTCString(),
|
||||
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',
|
||||
});
|
||||
|
||||
throw Error('Access denied.');
|
||||
};
|
||||
|
||||
module.exports = async (ctx, next) => {
|
||||
|
||||
@@ -26,13 +26,24 @@ module.exports = async (ctx, next) => {
|
||||
message = err.stack;
|
||||
}
|
||||
logger.error(`Error in ${ctx.request.path}: ${message}`);
|
||||
|
||||
if (config.isPackage) {
|
||||
ctx.body = {
|
||||
error: {
|
||||
message: err.message,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
ctx.set({
|
||||
'Content-Type': 'text/html; charset=UTF-8',
|
||||
});
|
||||
ctx.body = art(path.resolve(__dirname, '../views/error.art'), {
|
||||
message,
|
||||
});
|
||||
if (ctx.status !== 403) {
|
||||
ctx.status = 404;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ctx.debug.errorPaths[ctx.request.path]) {
|
||||
ctx.debug.errorPaths[ctx.request.path] = 0;
|
||||
|
||||
@@ -14,7 +14,7 @@ module.exports = {
|
||||
app = require('./app');
|
||||
},
|
||||
request: (path) =>
|
||||
new Promise((resolve) => {
|
||||
new Promise((resolve, reject) => {
|
||||
app.callback()(
|
||||
{
|
||||
url: path,
|
||||
@@ -26,7 +26,12 @@ module.exports = {
|
||||
setHeader: () => {},
|
||||
removeHeader: () => {},
|
||||
end: (data) => {
|
||||
data = JSON.parse(data);
|
||||
if (data.error) {
|
||||
reject(data.error.message);
|
||||
} else {
|
||||
resolve(JSON.parse(data));
|
||||
}
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
const supertest = require('supertest');
|
||||
const Parser = require('rss-parser');
|
||||
const parser = new Parser();
|
||||
let server;
|
||||
|
||||
async function checkBlock(response) {
|
||||
expect(response.status).toBe(403);
|
||||
expect(await parser.parseString(response.text)).toMatchObject({
|
||||
items: [],
|
||||
title: '没有访问权限. Access denied.',
|
||||
});
|
||||
expect(response.text).toMatch(/Access denied\./);
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
Reference in New Issue
Block a user