简体   繁体   English

错误处理程序如何工作?

[英]How does the Error Handler work?

How does the the error handler get triggered? 错误处理程序如何触发?

In sample code, I find that it is placed at the bottom of all the middleware functions. 在示例代码中,我发现它位于所有中间件功能的底部。 Is the position important? 这个职位重要吗?

You can refer below example for some details. 您可以参考以下示例以了解一些详细信息。 Here, for the '/' GET endpoint a middleware explicitly throws an error 'problem error'. 在这里,对于“ /” GET端点,中间件显式抛出错误“问题错误”。 At this point, express error handler mechanism is triggered and it looks for an error handler (with err as a param). 此时,将触发快速错误处理程序机制,并寻找一个错误处理程序(以err作为参数)。 As a result, subsequent 'Hello' is not sent back to the client as its handler is not an error one. 结果,后续的“ Hello”不会发送回客户端,因为其处理程序不是错误。

Subsequent error handlers logErrors, clientErrorHandler and errorHandler are invoked one by one to perform relevant tasks with final one writing back the response. 随后的错误处理程序logErrors,clientErrorHandler和errorHandler被一个接一个地调用以执行相关任务,最后一个则写回响应。

The reason they are placed at the end is to catch the errors thrown by declared middlewares and handle them elegantly ie printing it, logging it, sending mails etc. Think of it in terms of a try catch mechanism in other languages like Java. 将它们放置在末尾的原因是捕获声明的中间件引发的错误并对其进行优雅处理(即打印,记录日志,发送邮件等)。可以使用其他语言(如Java)中的try catch机制来考虑它。 If declared above other middlewares, they would render useless as the errors won't be handled. 如果在其他中间件之上声明,则它们将变得无用,因为不会处理错误。 You can see the difference in output by swapping the order of 'GET' request with the error handlers. 通过将“ GET”请求的顺序与错误处理程序交换,可以看到输出的差异。

 const express = require('express'); const app = express(); app.get('/', (req, res, next) => next(new Error('problem error')), (req, res) => { res.status(200).send("Hello"); }); app.use(logErrors); app.use(clientErrorHandler); app.use(errorHandler); function logErrors (err, req, res, next) { console.error(err.stack) next(err) } function clientErrorHandler (err, req, res, next) { if (req.xhr) { res.status(500).send({ error: 'Something failed!' }) } else { next(err) } } function errorHandler (err, req, res, next) { if (res.headersSent) { return next(err) } res.status(500) res.render('error', { error: err }) } app.listen(3000, () => console.log('Example app listening on port 3000!')) 

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

相关问题 中间件是否可以访问中间件错误处理程序? - Does a middleware have access to middleware error handler? 如何在ExpressJS中触发路由器错误处理程序而不是默认错误处理程序 - How to trigger router error handler instead of default error handler in expressjs 路由器内的Express错误处理程序不起作用 - Express error handler inside router doesn't work Express如何运作? - How does Express work? 如何发出错误才能在客户端的错误处理程序上捕获它? - How to emit error to be possible catch it on error handler on client-side? 一个控制器中的多个请求URL映射处理程序在Node Express JS中不起作用 - Multiple request url mapping handler in one controller does not work in Node Express JS 为什么Express的默认错误处理程序从Readable流中发出错误时会捕获错误? - Why does the Express' default error handler catch error when emitting from Readable stream? express.js,如果我删除下一个参数,错误处理程序将不起作用 - express.js, error handler doesn't work if I remove next parameter 设置error404处理程序(app.get('*',…))时,页面不起作用 - Page don't work when I set the error404 handler (app.get('*', …)) 异步错误处理包装器不起作用 - Async Error handling Wrapper does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM