app: fix error handing

This commit is contained in:
DIYgod 2018-09-12 17:18:43 +08:00
parent c835fa5c02
commit 27371c4bc8
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
3 changed files with 6 additions and 36 deletions

View File

@ -13,7 +13,6 @@ const template = require('./middleware/template');
const favicon = require('koa-favicon');
const debug = require('./middleware/debug');
const accessControl = require('./middleware/access-control');
const auth = require('./middleware/auth');
const router = require('./router');
const protected_router = require('./protected_router');
@ -40,9 +39,6 @@ app.use(favicon(__dirname + '/favicon.png'));
// global error handing
app.use(onerror);
// HTTP basic authentication
app.use(auth);
// 1 set header
app.use(header);

View File

@ -1,31 +0,0 @@
const logger = require('../utils/logger');
const Raven = require('raven');
const config = require('../config');
if (config.sentry) {
Raven.config(config.sentry).install();
}
module.exports = async (ctx, next) => {
try {
await next();
} catch (err) {
if (err.status === 401) {
logger.error('Authentication error: ' + (err instanceof Error ? err.stack : err));
ctx.set('WWW-Authenticate', 'Basic');
ctx.body = 'RSSHub: Unauthenticated access.';
ctx.status = 401;
if (config.sentry) {
Raven.captureException(
err,
{
req: ctx.req,
},
function(err, eventId) {
console.log('Authentication error ' + eventId);
}
);
}
}
}
};

View File

@ -7,6 +7,7 @@ if (config.sentry) {
}
module.exports = async (ctx, next) => {
console.log(123);
try {
await next();
} catch (err) {
@ -15,7 +16,11 @@ module.exports = async (ctx, next) => {
'Content-Type': 'text/html; charset=UTF-8',
});
ctx.body = `RSSHub 发生了一些意外: <pre>${err instanceof Error ? err.stack : err}</pre>`;
ctx.status = 404;
if (err.status === 401) {
ctx.status = 401;
} else {
ctx.status = 404;
}
if (config.sentry) {
Raven.captureException(