error handling – cleaner logs for invalid image uploads

This commit is contained in:
Andrea Vos 2023-05-31 12:55:56 +02:00
parent a734604fec
commit 5b15be1f3b
2 changed files with 6 additions and 2 deletions

View File

@ -59,7 +59,11 @@ router.post('/images/upload', multer({limits: {fileSize: 10 * 1024 * 1024}}).any
const ids = [];
for (let file of req.files) {
const id = ulid();
const image = await loadImage(await sharp(file.buffer).png().toBuffer());
try {
const image = await loadImage(await sharp(file.buffer).png().toBuffer());
} catch {
return res.status(400).json({error: 'File type not supported'});
}
for (let s in sizes) {
if (!sizes.hasOwnProperty(s)) { continue; }

View File

@ -1,3 +1,3 @@
module.exports = (err, req) => {
return `[${new Date().toISOString()}][${req ? (req.method + ' ' + req.url) : ''}] ${err.message || err} ${err.stack}`;
return `[${new Date().toISOString()}] [${req ? (req.method + ' ' + req.url) : ''}] ${err.message || err} ${err.stack}`;
}