How does Express 5 error handling middleware turn a thrown error into a JSON response? In this animated Node.js REST API tutorial, we’ll build a custom error class called AppError and follow one failed request from start to finish.
Imagine you’re adding a course, but another course already uses the same slug. Your API should return 409 Conflict. So how does the 409 you pass to new AppError() reach the client?
Using the Courses API from our live backend class, we’ll walk through:
• How AppError extends JavaScript’s Error class • What super(message) does and why we store statusCode on the error • How ensureUniqueSlug() detects a duplicate and throws AppError • How Express error middleware uses (err, req, res, next) • Why err.statusCode ?? 500 gives unexpected errors a fallback • How to log a 500 error on the server and send a simple message to the client
Quick challenge 👇 If you replace AppError with a plain new Error("A course with this title already exists"), what status code will this middleware send back, and what message will the client see? Drop both answers in the comments.
This video provides a comprehensive guide on implementing a professional error-handling system in Express 5. The instructor demonstrates how to move from generic \"something went wrong\" server responses to specific, user-friendly error messages by utilizing custom error classes and centralized middleware.
Hey guys, you can see here that I am a
Posting a request to my REST API
where I have a course there
But I want to add another new course.
Yeah, but I intentionally made a mistake, and that is
The parameter to be passed, okay, but here some
Things are there about the best way how you handle your errors
I will tell you today how to create Express Five
Yes, I've seen many, I made it this way, I've shown you
I've already pointed out that there's a problem with the title, the existing one.
Hey, if I send it, and boom, guys, check this out
If you look here, look inside the message—what do you see?
Something went wrong on the server.
but if you look at our terminal, basically the console,
Here they have clearly stated it: there is a course.
Look, a course with this title already exists.
I mean, it's appearing in the console.
What is the user seeing?
Something went wrong on this server
That's wrong, isn't it?
So why would that have happened?
I will tell you.
Now we understand step by step.
How do you make this journey your own?
You can implement error handling in Express 5.
In the most perfect way
Alright, so I'm removing this. Okay, this is basically using the Thunder Client.
I'm doing it; this is my app, okay? You can see this is my route.
API forward slash courses course route is correct here
But I've set it up this way, check out my post
If you need to request, this is my route, isn't it?
Default is the end and this is the dot post, this is the create course.
I went to the controller's place; this is my controller.
In the controller, I retrieved the data from request.body.
Alright, I've validated it, just leave it alone.
There's no need to take or give anything from him right now; I've just obtained the data, the one with valid text.
And I sent it to service; Create Course is one of our services.
Alright, the controller has sent this service the data here.
Now get the data from the first course, right?
Check the fields to see if they are all equal.
Here, this is important, because
Any course it is, you're building an API.
So ID is such a part,
What is very important,
Because going further, you will have the ID
You can search by virtue of it.
You can filter it.
Basically, through this, many things will happen ahead.
Whether you're creating a dynamic page, you need either an ID or a slug.
If you do it, there are basically two such things, two fields like this.
One that can provide the server itself, for example
Now, obviously, the next element I'll be adding here
A new course, it should be 17, the user can write anything.
So they won't give that authority to us, or to whoever is handling it.
second slug, if you look closely, the slug is basically,
We will create a slug based on whatever title there is.
I mean, if the server just gets the title, then
We need to provide them so that I can add a new course.
Alright, the journey is clear to you, correct?
We came here after entering the controller
So, these three things we are taking there at Tek Ker.
The job of a pick course is that it should be filled, right? Whatever it is, it should be filled.
Later I'll go down and check the ID we've generated. After that
Look here, after that, the ID should just be relieved; this line should be here.
Alright, because once a slug is created,
Then we'll directly check afterward whether it's unique or not.
Simple, right here is where our game is fixed, okay?
Look closely, from here on, it finally starts.
So here it is, guys, our function, and here we have a courses slug,
and for now you can ignore this, okay, but you see
Yes or no, true or false, is it or isn't it, just end it,
I don't need anything more from him, and for that reason alone we
are using some, okay, and simple we've courses.slug if triple equal
to slug is, and if the ID which is, ignore ID basically is not undefined,
So, the simple meaning of this is that the course in question is
We have already created it before.
I mean, that slug already exists, and if it exists, then check it out.
I even sent a message saying, "Yes, brother."
There is a spelling mistake here
The title already exists.
in the course and throw error
the moment you throw
So Express 5 catches him.
And Siddha, you go there.
Your own error handling
I have already created the middleware.
You can see, if you look closely here, this is our middleware.
Look here, and here we have our validated course
Sorry, the course is not valid, error handler
Here you can see, okay, so he went over there.
Why did he come here? He came here because of a short while ago.
I've already told you, if you want your own default error
I want to create a handling middleware function
Then you'll have to write it in this manner,
You can see for yourself here that you need to pass four arguments.
whether the user has provided the status code or not; if not provided then
By default, we will take it as 500.
And if the default is 500, then on the console I'm showing the data
But what am I doing over there in the other place?
The response is, there I said aloud that
If it remains 500, something went wrong
on the server if there is a status code
Then we get error.message
You're understanding, and it's also necessary, isn't it?
Because if you don't provide the status code at all
So, brother, server or us
How to decide for yourself
You understand now, there could be many errors.
for example, 409 for duplicate
Course not found, 404
Wrong data 400
401 on login next
So I hope you understand why there could be so many status codes
So we can't just imagine it ourselves, can we?
For that reason, we've set the default unexpected bug to 500.
Alright, but there is a way to fix it.
The way to fix this is this
because now you see for yourself why the error instance is there
It doesn't give you any status code by default.
Anyway, here we create a new object.
and in the object, if there is one property
If we need to add it, we can do it easily.
and for that reason, on this very error
Alright, we've already created the error instance.
But I'm just adding the status code right there.
and that's this time 409 duplicates
Okay, 409, I'm adding it.
So the moment I do this
Well, you'll just have to write it down, okay?
and as soon as you throw it
So I already have the message.
By default, the dot message goes to default.
I also added a new property for the status code inside that object.
Naoh Ro wrote, and then he went to our default middlewear error
We have handling at Middleway, and it's time now.
It won't work if it's not 500; otherwise, it will come straight here.
The status code is not 500, so for that reason
Now the actual message we have
What we have sent
If the title is the same, it's basically the slug.
So what should I show?
So if I refresh
And boom, guys, check this out
Our work is done, a very good job, our work is done.
I'm glad, isn't it? Now, second time, once this happens, you have
Notice one thing here: just throw error and error in
We need to write this three-line code to give message and status code
It's raining, but what if this three-line thing is this three
If we can write the line just in one line, that would be awesome.
It will happen, won't it, because going forward, going forward again, once more, I have
As I told you, on the duplicate, I didn't get the 409 course.
So 404, wrong data
400, proceed to login
401, so basically, brother
You will have to write these 3 lines everywhere.
Three, three, three—imagine how much more it will become
You're getting ahead, you know, meaning, on one hand
You will have 100 lines; if you are in one line
If you do it in three lines, you'll end up with 300 lines.
There, one lakh; here, three lakh; one crore; here, three crore;
A very big difference will arise; then what will we do now?
We will create our own custom error class.
Error class, yes, guys, so finally, this is also very awesome,
Now, look here, I've created this, so we have a utils,
Okay, inside the utils folder, one second, I don't know,
I think this will look perfect, so we have these utilities,
And here we have this app-error.js, okay.
Here, if you're walking, this is my custom error class.
Here you can see that I've already entered the complete definition line
If it's written line by line, you can also view it by pausing.
But let me quickly tell you, we are creating a new class type.
Alright, we are extending the error that exists.
error basically in JavaScript, you have your own built-in error
Alright, just as you can accept it as it is
You have to throw here too, you have to throw here too, but
Now we have the new app error—basically, the custom error class we had ourselves
I'm calling him here now, so the moment you get the new app error, why
Because the app error is this, look here, the app error is fine, whenever you have a new
You're calling this custom error class
If you're calling this constructor function of yours
By default, this call gets triggered, okay? It starts running now. When you make this call
When you call the new app error, you will receive a message along with it.
Number is basically your status code, so one message, one status code.
As you wrote "new app error," this is what appeared here.
Constructor, your message has arrived here, this is basically the value that has come here.
The value is already here on the status code, okay.
And whenever you write extend, we need to write super.
It will be from Super that whatever this message is,
That's our parent, error, add it, I mean add it in there
You've already added the message here, this is the error object
Take any value for the global error object on the object
After that, we need to add one more thing there.
and that is the status code
So that we also get it, and later we can access it comfortably.
We have also added the status code using this.status code
so the custom error class we have created basically
It has one job: to create an error object with a message and status code.
It's fine, we've just created the message and status code once.
Because that is what we had to do
Here too, the message and my status code
So I saved these two lines
An error of your own
The custom class that I've created here is basically a custom error class.
Alright, so wherever we see a problem, we need to address it now.
Just send the simple new app error message and status code.
Your own custom error object, which you've written this much,
Created with message and status code, OK,
Now after that, you have to throw, because as soon as you throw,
So finally, it will go to the middleware of Express 5.
Alright, where you also have the status code at his place and
the error.message will also be obtained because
We've passed it, so relax, whatever your status is
The code will come here, meaning it's not 500, so this
No, Siddha won't come here.
Not even close, it's not 500 either
It's 409, okay, fine then.
So this err.message is yours
It will be found, and yes, one thing that has not yet
Maybe you're feeling like, dude
I've tried everything, but nothing seems to be working for me.
And remember one thing about this middleware here
This is your own program.
app. Whatever it is
At the very end, you have to use this middleware
Look at this
app. use and then error handler
With this approach, the middleware that you have here
You have to use this here.
And there's nothing else to do anywhere else.
Alright, that's it, and look, our work is done, app, route,
controller, service, and basically a custom error handler
So basically, you've seen the custom error class and your own custom
Middleware error middleware, right? Basically, this is how you do it.
Everywhere empty throw new app error message and status code, so I hope guys
You've got it, just look at this new app here, it's throwing a new app error, that's all.
That's all you need to do; your work will automatically take care of itself, right? It was so easy, wasn't it?
So, guys, this is it for today's video
That's all I hope, so I trust you enjoyed the video.
And I would like to inform you that MySQL will be coming from Wednesday onwards.
Live Online Classes that are about to start where
But I will teach you myself live every day after 9 PM
So if you're interested, let me know in the comment section
I'll send you the link there, you'll enroll, okay?
Yes, that's all for today on this video, guys. If you like the video, please like it.
You will subscribe and share
See you in the next video, till then guys, thank you so much, take care