Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

javascript - How to return an error back to ExpressJS from middleware?

I am using [Multer][1] as middleware to process multipart form data. Multer offers some configuration options for setting destination of file uploads and names called diskStorage. It is within this area that one can do some error checking and control whether Multer authorises a file upload or not.

My Express route is basically this:

expressRouter.post(['/create'],
    MulterUpload.single("FileToUpload"), // if this throws an error then have Express return that error to the user
    async function(req, res) {
      // handle the form text fields in req.body here
});

MulterUpload.single() takes the file input field named "FileToUpload" and sends it off to do this:

const MulterUpload = multer({
    storage: MulterStorage
)}

const MulterStorage = multer.diskStorage({
    destination: async function (req, file, cb) {
        try {
            if ("postID" in req.body && req.body.postID != null && req.body.postID.toString().length) {

                const Result = await api.verifyPost(req.body.postID)
                if (Result[0].postverified == false) {
                    const Err = new Error("That is not your post!");
                    Err.code = "ILLEGAL_OPERATION";
                    Err.status = 403;
                    throw(Err); // not authorised to upload
                } else {
                    cb(null, '/tmp/my-uploads') // authorised to upload
                }
            }
        } catch (err) {
            // How do I return the err back to Express so it can send it to the user? The err is an unresolved Promise as I am using async/await
        }
    }
    ,
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
})

I just can't seem to work out how to get the error from MulterStorage back to Express so that it is sent back the browser/user as an error. [1]: https://www.npmjs.com/package/multer

question from:https://stackoverflow.com/questions/66067479/how-to-return-an-error-back-to-expressjs-from-middleware

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can call the completion callback with an Error object as the first argument. So, instead of

cb(null, someResult)

you call the callback with an error object

cb(new Error("I got a disk error"));

Then, if you have multer set up as plain middleware, this will result in next(err) being called and in Express, your generic error handler will receive the error.

Here are a couple examples:

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

https://github.com/expressjs/multer/issues/336#issuecomment-242906859


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...