0% found this document useful (0 votes)
12 views9 pages

5.3 Unit 5 Error Handling

Error handling in Express is managed through middleware that requires four arguments: err, req, res, and next. This allows for the separation of error logic from route handling, enabling customized responses based on error types. Additionally, Express utilizes the Debug module for logging internal operations, which can be configured to show specific logs as needed.

Uploaded by

Kshitij Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

5.3 Unit 5 Error Handling

Error handling in Express is managed through middleware that requires four arguments: err, req, res, and next. This allows for the separation of error logic from route handling, enabling customized responses based on error types. Additionally, Express utilizes the Debug module for logging internal operations, which can be configured to show specific logs as needed.

Uploaded by

Kshitij Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Error Handling using Express

Error Handling
• 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:

2
Error Handling
• 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.

3
Error Handling
• 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.
• Let us understand this through an example.

4
Error Handling
• This error handling middleware can be strategically placed after routes or contain
conditions to detect error types and respond to the clients accordingly.
• The above program will display the following output.

5
Debugging
• Express uses the Debug module to internally log information about route matching,
middleware functions, application mode, etc.
• To see all internal logs used in Express, set the DEBUG environment variable
to Express:* when starting the app:

6
Debugging
• The following output will be displayed.

7
Debugging
• These logs are very helpful when a component of your app is not functioning right.
• This verbose output might be a little overwhelming.
• You can also restrict the DEBUG variable to specific area to be logged.
• For example, if you wish to restrict the logger to application and router, you can use
the following code.

• Debug is turned off by default and is automatically turned on in production


environment.
• Debug can also be extended to meet your needs, you can read more about it at its
npm page.
8
Thank You

You might also like