Back to Browse

Error Handling in #Express with Custom Error Class | 404 | RESTful API using #NodeJS and #MySQL

9.0K views
Sep 27, 2020
11:46

Error handling in Express is done using middleware. But this middleware has special properties. The error handling middleware are defined in the same way as other middleware functions, except that error-handling functions MUST have four arguments instead of three – err, req, res, next. For example, to send a response on any error, we can use − Build RESTful APIs with Node.js express and MySQL | Authentication with JWT | In one Video https://youtu.be/WfCJ3sHnLBM Converting callbacks to promises | Handle Promises using async/wait | node js tutorial https://youtu.be/zs6T4Bq9uUs REST API validation in nodejs https://youtu.be/F-1GD_F8jHg download code from github https://github.com/Tariqu/error-handling-nodejs.git ``` app.use((err, req, res, next) = { const statusCode = err.statusCode || 500; res.status(statusCode).json({ success: 0, message: err.message, stack: err.stack }) }) ``` Till now we were handling errors in the routes itself. The error handling middleware allows us to separate our error logic and send responses accordingly. The next() method we discussed in middleware takes us to next middleware/route handler. For error handling, we have the next(err) function. A call to this function skips all middleware and matches us to the next error handler for that route.

Download

1 formats

Video Formats

360pmp421.3 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Error Handling in #Express with Custom Error Class | 404 | RESTful API using #NodeJS and #MySQL | NatokHD