Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function error(req, err) {
if (err instanceof multer.MulterError) {
// TODO is special handling for MulterErrors needed
console.error(err);
return validationError(500, req.path, err.message);
} else {
// HACK
// TODO improve multer error handling
const missingField = /Multipart: Boundary not found/i.test(
err.message || '',
);
if (missingField) {
return validationError(400, req.path, 'multipart file(s) required');
} else {
console.error(err);
return validationError(500, req.path, err.message);
}
}
function json(err, req, res, next) {
let message = err.message;
if (err instanceof multer.MulterError) {
res.status(400);
} else if (err instanceof jwt.JsonWebTokenError) {
res.status(403);
} else if (typeof err.status === 'number') {
// oauth2orize errors, bodyparser errors
res.status(err.status);
} else if (typeof err.code === 'number') {
res.status(err.code);
} else if (err.code === 'ENOENT') {
res.status(404);
} else if (err.code === 'EPERM') {
res.status(403);
} else if (err.code === 'EINVAL') {
res.status(400);
} else {
res.status(500);
this.server.use(async (err, req, res, next) => {
if (
process.env.NODE_ENV === 'development' ||
err instanceof multer.MulterError
) {
return res.status(500).json({
error: err,
});
}
return res.status(500).json({ error: 'Internal server error' });
});
}
upload(req,res,err=>{
if (err instanceof multer.MulterError) {
res.json({code:102,msg:err.message});
return false;
} else if (err) {
res.json({code:101,mag:err});
return false;
}
upFileForOss(req, res);
})
})
upload(req, res, function(err) {
if (err instanceof multer.MulterError) {
res.send({ code: '-200', msg: err.toString() })
} else if (err) {
res.send({ code: '-200', msg: err.toString() })
} else {
const file = req.file
res.send({ code: '200', url: file.path })
}
})
}
upload(req,res,err=>{
if (err instanceof multer.MulterError) {
res.json({code:102,msg:err.message});
return false;
} else if (err) {
res.json({code:101,mag:err});
return false;
}
upFileForLocal(req, res);
})
});