0% found this document useful (0 votes)
4 views

Express error handling middleware

Uploaded by

monkeiydluffy752
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Express error handling middleware

Uploaded by

monkeiydluffy752
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Error handling middleware

Default error handling in express


Express has default handling. See the documentation to know more

Custom way to error handliing


app.get('/abc' , (req , res) => {
res.send(abc);
})

Now if we go to http://localhost:8080/abc this will show:

ReferenceError: abc is not defined


at /Users/akash/All code/Html Css and js/Javascript/Learn
ing/Node js/middleware/index.js:7:14
at Layer.handle [as handle_request] (/Users/akash/All cod
e/Html Css and js/Javascript/Learning/Node js/middleware/node
_modules/express/lib/router/layer.js:95:5)
at next (/Users/akash/All code/Html Css and js/Javascrip
t/Learning/Node js/middleware/node_modules/express/lib/route
r/route.js:149:13)
at Route.dispatch (/Users/akash/All code/Html Css and js/
Javascript/Learning/Node js/middleware/node_modules/express/l
ib/router/route.js:119:3)
at Layer.handle [as handle_request] (/Users/akash/All cod
e/Html Css and js/Javascript/Learning/Node js/middleware/node
_modules/express/lib/router/layer.js:95:5)
at /Users/akash/All code/Html Css and js/Javascript/Learn
ing/Node js/middleware/node_modules/express/lib/router/index.
js:284:15
at Function.process_params (/Users/akash/All code/Html Cs
s and js/Javascript/Learning/Node js/middleware/node_modules/

Error handling middleware 1


express/lib/router/index.js:346:12)
at next (/Users/akash/All code/Html Css and js/Javascrip
t/Learning/Node js/middleware/node_modules/express/lib/route
r/index.js:280:10)
at expressInit (/Users/akash/All code/Html Css and js/Jav
ascript/Learning/Node js/middleware/node_modules/express/lib/
middleware/init.js:40:5)
at Layer.handle [as handle_request] (/Users/akash/All cod
e/Html Css and js/Javascript/Learning/Node js/middleware/node
_modules/express/lib/router/layer.js:95:5)

This is express default error handling. Here show where the error starts the stack
of the error
To show custom message to user we use custom errror handling middleware

app.get('/abc' , (req , res) => {


res.send(abc);
})

app.use((err , req , res , next) => {


console.log(err);
res.send('Something went wrong');
})

Now if we go to again the same address we will see in the page

Something went wrong

Or

app.get('/abc' , (req , res) => {


res.send(abc);
})

app.use((err , req , res , next) => {

Error handling middleware 2


console.log(err);
next();
})

app.use((req , res , next) => {


res.send('Page not found');
})

Here we will see

Page not found

Because here next() will search the next non error handling middleware. SO it
founds the next one and executes it .

See this

app.get('/abc' , (req , res) => {


res.send(abc);
})

app.use((err , req , res , next) => {


console.log(err);
next();
})

As next() is searching non error handling middleware and no such middleware


doesn’t exist so in page in http://localhost:8080/abc we will see:

Cannot GET /abc

Again

app.get('/abc' , (req , res) => {


res.send(abc);

Error handling middleware 3


})

app.use((err , req , res , next) => {


console.log('Error middleware 1');
next(err);
})

app.use((err , req , res , next) => {


console.log('Error middleware 2');
res.send('error');
})
app.use((req , res , next) => {
res.send('Page not found');
})

We pass a parameter in next . Now it will search for the next error handling

middleware

So we will see in the page

error

But if there is no next error handling middleware then the express default error
handling middleware will execute
See the ex

app.get('/abc' , (req , res) => {


res.send(abc);
})

app.use((err , req , res , next) => {


console.log('Error middleware 1');
next(err);
})

app.use((req , res , next) => {

Error handling middleware 4


res.send('Page not found');
})

This will output:

ReferenceError: abc is not defined


at /Users/akash/All code/Html Css and js/Javascript/Learn
ing/Node js/middleware/index.js:7:14
at Layer.handle [as handle_request] (/Users/akash/All cod
e/Html Css and js/Javascript/Learning/Node js/middleware/node
_modules/express/lib/router/layer.js:95:5)
at next (/Users/akash/All code/Html Css and js/Javascrip
t/Learning/Node js/middleware/node_modules/express/lib/route
r/route.js:149:13)
at Route.dispatch (/Users/akash/All code/Html Css and js/
Javascript/Learning/Node js/middleware/node_modules/express/l
ib/router/route.js:119:3)
at Layer.handle [as handle_request] (/Users/akash/All cod
e/Html Css and js/Javascript/Learning/Node js/middleware/node
_modules/express/lib/router/layer.js:95:5)
at /Users/akash/All code/Html Css and js/Javascript/Learn
ing/Node js/middleware/node_modules/express/lib/router/index.
js:284:15
at Function.process_params (/Users/akash/All code/Html Cs
s and js/Javascript/Learning/Node js/middleware/node_modules/
express/lib/router/index.js:346:12)
at next (/Users/akash/All code/Html Css and js/Javascrip
t/Learning/Node js/middleware/node_modules/express/lib/route
r/index.js:280:10)
at expressInit (/Users/akash/All code/Html Css and js/Jav
ascript/Learning/Node js/middleware/node_modules/express/lib/
middleware/init.js:40:5)
at Layer.handle [as handle_request] (/Users/akash/All cod
e/Html Css and js/Javascript/Learning/Node js/middleware/node
_modules/express/lib/router/layer.js:95:5)

Error handling middleware 5


Custom error gen
app.use((err , req , res , next) => { //middleware1
console.log('Error middleware 1');
throw new Error('My custom Error message');
})

app.use((err , req , res , next) => {. //middleware2


console.log('Error middleware 2');
next(err)
})
app.use((req , res , next) => { //middleware3
console.log('Page not found middleware');
res.send('Page not found');
})

Here when error generated, first middleware 1 will execute. This throw an error
with custom message. This will search the next error handling middleware just like
next(err) but with custom message .So the middleware2 executes.

And as there next with parameter and no other error handling middleware so the
express default middleware executes with custom message
The output in console and in page:

Error middleware 1
Error middleware 2

Error: My custom Error message


at /Users/akash/All code/Html Css and js/Javascript/Learn
ing/Node js/middleware/index.js:13:11
at Layer.handle_error (/Users/akash/All code/Html Css and
js/Javascript/Learning/Node js/middleware/node_modules/expres
s/lib/router/layer.js:71:5)
at trim_prefix (/Users/akash/All code/Html Css and js/Jav
ascript/Learning/Node js/middleware/node_modules/express/lib/

Error handling middleware 6


router/index.js:326:13)
at /Users/akash/All code/Html Css and js/Javascript/Learn
ing/Node js/middleware/node_modules/express/lib/router/index.
js:286:9
at Function.process_params (/Users/akash/All code/Html Cs
s and js/Javascript/Learning/Node js/middleware/node_modules/
express/lib/router/index.js:346:12)
at next (/Users/akash/All code/Html Css and js/Javascrip
t/Learning/Node js/middleware/node_modules/express/lib/route
r/index.js:280:10)
at next (/Users/akash/All code/Html Css and js/Javascrip
t/Learning/Node js/middleware/node_modules/express/lib/route
r/route.js:141:14)
at Layer.handle [as handle_request] (/Users/akash/All cod
e/Html Css and js/Javascript/Learning/Node js/middleware/node
_modules/express/lib/router/layer.js:97:5)
at next (/Users/akash/All code/Html Css and js/Javascrip
t/Learning/Node js/middleware/node_modules/express/lib/route
r/route.js:149:13)
at Route.dispatch (/Users/akash/All code/Html Css and js/
Javascript/Learning/Node js/middleware/node_modules/express/l
ib/router/route.js:119:3)

But if

app.use((err , req , res , next) => { //middleware1


console.log('Error middleware 1');
throw new Error('My custom Error message');
})

app.use((err , req , res , next) => {. //middleware2


console.log('Error middleware 2');
next()
})
app.use((req , res , next) => { //middleware3

Error handling middleware 7


console.log('Page not found middleware');
res.send('Page not found');
})

output in page

Page not found

Output in console:

Error middleware 1
Error middleware 2
Page not found middleware

Custom Error class


Works same as before
The following two files are in the same directory

ExpressError.js:

class ExpressError extends Error {


constructor(message , status) {
super();
this.message = message;
this.status = status;
}
}
module.exports = ExpressError

index.js

const ExpressError = require('./ExpressError');


app.use((err , req , res , next) => {
console.log('Error middleware 1');

Error handling middleware 8


throw new ExpressError('Something went wrong' , 401);
})

app.use((err , req , res , next) => {


console.log('Error middleware 2');
res.send(err)
})
app.use((req , res , next) => {
console.log('Page not found middleware');
res.send('Page not found');
})

Output will see in page:

{"message":"Something went wrong","status":401}

in console

Error middleware 1
Error middleware 2

Error handling middleware 9

You might also like