Improve error handling

master
Joe Biellik 4 years ago
parent 11862be2bd
commit d27c32abf1

@ -18,17 +18,12 @@ app.use(require('koa-compress')());
app.use(require('koa-static-cache')(path.join(__dirname, 'public'), {
maxAge: config.cacheAge
}));
app.use(require('koa-body')({
json: false,
multipart: true,
jsonLimit: config.sizeLimit,
formLimit: config.sizeLimit,
textLimit: config.sizeLimit
}));
app.use(require('koa-views')(path.join(__dirname, 'views'), {
extension: 'pug'
}));
app.use(router.routes(), router.allowedMethods());
app.use((ctx) => ctx.throw(404));
module.exports = app;

@ -22,11 +22,7 @@ module.exports = {
ctx.body = paste.paste;
}
} catch (ex) {
ctx.throw(404, 'Paste Not Found', {
headers: {
'Cache-Control': 'no-cache'
}
});
ctx.throw(404, 'Paste Not Found');
}
},
@ -52,11 +48,7 @@ module.exports = {
// Ignore
}
} catch (ex) {
ctx.throw(400, 'Error Processing Request Body', {
headers: {
'Cache-Control': 'no-cache'
}
});
ctx.throw(400, 'Bad Paste Body');
}
}
@ -66,11 +58,7 @@ module.exports = {
ctx.request.body.expire = ctx.request.body.expire * ctx.request.body.multiplier;
}
} catch (ex) {
ctx.throw(400, 'Error Processing Paste Expiry', {
headers: {
'Cache-Control': 'no-cache'
}
});
ctx.throw(400, 'Bad Paste Expiry');
}
// Raw request body
@ -81,11 +69,7 @@ module.exports = {
}
if (!ctx.request.body.paste) {
ctx.throw(400, 'Error No Paste Provided', {
headers: {
'Cache-Control': 'no-cache'
}
});
ctx.throw(400, 'No Paste Provided');
}
// /?expire=xxx
@ -106,11 +90,7 @@ module.exports = {
try {
await paste.save();
} catch (ex) {
ctx.throw(500, 'Error Storing Paste', {
headers: {
'Cache-Control': 'no-cache'
}
});
ctx.throw(500, 'Internal Storage Error');
}
// /?highlight=xxx

@ -22,6 +22,7 @@
"lint": "eslint . && pug-lint ./views"
},
"dependencies": {
"bytes": "^3.1.0",
"config": "^3.3.1",
"koa": "^2.13.0",
"koa-body": "^4.2.0",

@ -1,7 +1,24 @@
const config = require('config');
const router = require('koa-router')();
const conditional = require('koa-conditional-get')();
const etag = require('koa-etag')();
const config = require('config');
const body = require('koa-body')({
json: false,
multipart: true,
formLimit: config.sizeLimit,
textLimit: config.sizeLimit,
formidable: {
multiples: false,
maxFileSize: require('bytes').parse(config.sizeLimit)
},
onError: (err, ctx) => {
if (err.message.startsWith('maxFileSize')) {
ctx.throw(400, 'Paste Exceeds Maximum Size (' + config.sizeLimit.toUpperCase() + ')');
} else {
ctx.throw(500, err.message);
}
}
});
const pastes = require('./controllers/pastes');
router
@ -17,7 +34,7 @@ router
highlights: config.highlights
});
})
.post('/', pastes.create)
.post('/', body, pastes.create)
.get('/:id', conditional, etag, pastes.view);
module.exports = router;

Loading…
Cancel
Save