简体   繁体   English

快速错误处理:向前端发送消息

[英]Express Error Handling: Sending a Message to Frontend

I'm having some trouble error handling my authentication API calls.我在处理身份验证 API 调用时遇到了一些问题。 When I send the 500 status from Express, my frontend (Vue in this case) only picks up the message Request failed with status code 500 rather than something more helpful for triage like this is the worst error ever (in the example below).当我从 Express 发送 500 状态时,我的前端(在本例中为 Vue)只接收消息Request failed with status code 500而不是更有助于分类的消息,例如这是有史以来最严重的错误(在下面的示例中)。

In the below example, when I call '/post' from the API, I throw an error which is handled by my custom middleware.在下面的示例中,当我从 API 调用“/post”时,我抛出了一个由我的自定义中间件处理的错误。 The middleware successfully handles the error and sends the appropriate status to my frontend, but I can't figure out how to send useful messages (eg 'this is the worst error ever') / access them in the front end.中间件成功地处理了错误并将适当的状态发送到我的前端,但我不知道如何发送有用的消息(例如,“这是有史以来最糟糕的错误”)/在前端访问它们。

Is this a common use case?这是一个常见的用例吗? Am I doing anything obviously wrong?我在做任何明显错误的事情吗? Does the message I send come up in the (err) parameter, or do I need to add a resp parameter?我发送的消息是否出现在 (err) 参数中,还是我需要添加一个 resp 参数?

Express Route for '/login' '/login' 的快速路由

router.post('/login', (req, res, next) => {
    throw Error('this is the worst error ever')
})

Custom express error handler自定义快速错误处理程序

app.use(function(err, req, res, next) {
    res.status(err.status || 500).send({
        error: {
            status: err.status || 500,
            message: err.message || 'Internal Server Error',
        },
    });
});

Handle the API Response in Vue在 Vue 中处理 API 响应

login (e) {
    e.preventDefault();
    UserService.login(this.username, this.password) //this is simple axios post call 
    .catch((err) => {
         this.loginStatus = err.message
         return
    })
}

I think you need to add:我认为您需要添加:

Throw new Error()

instead of代替

Throw Error

If you are making an asynchronous calling you can do this如果您正在进行异步调用,您可以执行此操作

app.get('/', function (req, res, next) {
  fs.readFile('/file-does-not-exist', function (err, data) {
    if (err) {
       next(err) // Pass errors to Express.
    } else {
       res.send(data)
    }
  })
})

Code should look like this:代码应如下所示:

router.post('/login', (req, res, next) => {
   throw new Error('this is the worst error ever')
})

Check this in express documentation快速文档中检查这一点

Found the answer to this for those that find this helpful.为那些认为这很有帮助的人找到了答案。 The err that is caught has a response variable.捕获的错误有一个响应变量。 This is where the data is sent via the express send command.这是通过快速发送命令发送数据的地方。 See corrected code below for the frontend:请参阅下面的前端更正代码:

login (e) {
    e.preventDefault();
    UserService.login(this.username, this.password) //this is simple axios post call 
    .catch((err) => {
         this.loginStatus = err.response.data.message
         return
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM