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'), { app.use(require('koa-static-cache')(path.join(__dirname, 'public'), {
maxAge: config.cacheAge 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'), { app.use(require('koa-views')(path.join(__dirname, 'views'), {
extension: 'pug' extension: 'pug'
})); }));
app.use(router.routes(), router.allowedMethods()); app.use(router.routes(), router.allowedMethods());
app.use((ctx) => ctx.throw(404));
module.exports = app; module.exports = app;

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

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

@ -1,7 +1,24 @@
const config = require('config');
const router = require('koa-router')(); const router = require('koa-router')();
const conditional = require('koa-conditional-get')(); const conditional = require('koa-conditional-get')();
const etag = require('koa-etag')(); 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'); const pastes = require('./controllers/pastes');
router router
@ -17,7 +34,7 @@ router
highlights: config.highlights highlights: config.highlights
}); });
}) })
.post('/', pastes.create) .post('/', body, pastes.create)
.get('/:id', conditional, etag, pastes.view); .get('/:id', conditional, etag, pastes.view);
module.exports = router; module.exports = router;

Loading…
Cancel
Save