How do i produce multer error message in my postman

人走茶凉 提交于 2020-01-06 04:30:12

问题


Im trying to res.status.send a multer error message to postman when my file image exceeds 1MB. But when i try to run my code it only gives me this entire chunk of error message. I just want to get the error message itself(LIMIT_FILE_SIZE).Is there any way to achieve this? IMAGE HERE

My current app.js:

var multer = require('multer');
var storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, './uploads');
    },
    filename: function(req, file, callback) {
        callback(null, path.basename(file.originalname));
    }
})
const upload = multer({
    dest: storage,
    storage: storage,
    limits: {
      fileSize: 1024 * 1024
    },
    fileFilter: function(req, file, callback, error) {
        var ext = path.extname(file.originalname);
        var error_msg = error instanceof multer.MulterError
        if(ext !== '.jpg') {
             req.fileValidationError = "Not a jpg file!";
             return callback(null, false, req.fileValidationError);
        }
        if(error_msg) {
            return callback(null, false, new MulterError('LIMIT_FILE_SIZE'))
        }
        callback(null,true)
    }


  });
app.post("/upload",upload.single('name'),(req,res,next) => {
if(req.fileValidationError) {
        res.status(500).send({message:req.fileValidationError});
    }
    else {
        if(error.code === 'LIMIT_FILE_SIZE') {
            req.fileSizeError = "Image more than 1MB!"
            res.status(500).send({message:req.fileSizeError});
        }
        else {
            console.log('File Received!');
            console.log(req.file);
            var sql = "INSERT INTO `file`(name,description,type,size) VALUES('" + req.file.filename + "', '" + (req.file.encoding + "_" + req.file.destination + "_" + req.file.path)+ "', '" + req.file.mimetype + "', '" + req.file.size + "')";
            db.query(sql, (error, results) => {
                console.log('Inserted Data!');
            });
        const message = "Successfully Uploaded!"
        res.status(200).send({message:message, file_details:req.file})
        }
    }
    })



回答1:


Multer delegates the error to Express which is the standard way of throwing errors in express. To catch a specific error, you can use the multer upload middleware inside the route callback. This is the method as given by multer's documentation, also mentioned by @Mattia Rasulo

router.post('/image', function (req, res, next) {
  upload.single('image')(req, res, function (error) {
    if (req.fileValidationError) {
      res.status(500).send({ message: req.fileValidationError });
    }
    else {
      if (error) {
        res.status(500).send({ message: error.code === 'LIMIT_FILE_SIZE' ? "Image more than 1MB!" : error.message });
      }
      else {
        console.log('File Received!');
        console.log(req.file);
        var sql = "INSERT INTO `file`(name,description,type,size) VALUES('" + req.file.filename + "', '" + (req.file.encoding + "_" + req.file.destination + "_" + req.file.path)+ "', '" + req.file.mimetype + "', '" + req.file.size + "')";
        db.query(sql, (error, results) => {
            console.log('Inserted Data!');
        });
        const message = "Successfully Uploaded!"
        res.status(200).send({message:message, file_details:req.file})
      }
    }
  });
});



回答2:


Multer just sends the error to your global error middleware so you just catch it and check upon what error is:

if(err.message === 'file too large') [change the message as you need].

This is how I've handled your exact same issue!

https://www.npmjs.com/package/multer#error-handling



来源:https://stackoverflow.com/questions/59526207/how-do-i-produce-multer-error-message-in-my-postman

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!