
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
How did you manage to remove the blur property and reach here?
Upgrading gives you access to quizzes so you can test your knowledge, track progress, and improve your skills.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
##Looks like we found a thief monkey By the way, I liked the trick how you reached till here. You have a good sense of humor. You will improve a lot if you join our course with this passion.
var
(function-scoped, outdated)let
(block-scoped, modern and recommended)const
(block-scoped, cannot be reassigned)_
, or $
let let = 5;
is invalid)myVar
and myvar
are different)string
, number
, boolean
, null
, undefined
, bigint
, symbol
Objects
, Arrays
, Functions
Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.
00:00:02 Let's create a global error handling middleware that'll nicely alert us whenever something goes wrong.
00:00:08 We can do that easily by creating a new folder called middlewares.
00:00:13 And within it, we can create a new file called error.middleware.js.
00:00:20 And within it, we can define it just as a regular JavaScript function const errorMiddleware is equal to a function that looks like this.
00:00:30 Now, not just errorMiddleware, but any kind of middleware in Node.js or any other backend software typically looks like this.
00:00:40 Alongside the request and the response, you get two additional things.
00:00:44 The information that happened before the request, and then what happens after, when you're ready to proceed to the next step.
00:00:52 Let me explain what I mean by this.
00:00:54 Like let's say that we have the ability to create a new user.
00:00:59 In a way, when we had a subscription model in the previous lesson, this subscription schema that pre was actually in a way a middleware because it happened
00:01:10 before something and then told us, hey, now we are ready to move to the next step.
00:01:14 So let's say that you're trying to create a subscription, create a subscription.
00:01:20 Then we have a new middleware that maybe checks something, like check for renewal date.
00:01:27 And then you can have as many middlewares as you want.
00:01:30 We can have another middleware, maybe this one will actually check for errors.
00:01:36 And then only when both of these middlewares call their next status, we are actually navigated over to the controller, which handles the actual logic of
00:01:46 creating a subscription.
00:01:47 Hopefully that makes sense.
00:01:49 Basically, some blocks of code that are executed before or after something allowing us to intercept what is happening.
00:01:57 So let's open up a try and catch block.
00:02:00 And in the catch, we get the error and we'll simply send that error over to the next step to let us know that the error actually happened.
00:02:08 But in the try block, we'll actually try to decipher that error to let us know exactly what the issue is.
00:02:14 So I'll say let error is equal to, and I'll create a new object and destructure the error that we're getting through props.
00:02:24 Then I'll set the error message to be equal to err.message.
00:02:30 And I'll console log the error to the dev.
00:02:34 So console.error, and then we pass the error right here.
00:02:39 Just so we know what's happening.
00:02:42 After that, we can try to figure out a type of error.
00:02:46 Maybe it is a Mongoose bad object ID.
00:02:50 This is a very common error that we get when working with Mongoose and MongoDB.
00:02:55 This one would happen if we have a cast error, if that is the name.
00:02:59 If that is the name, we can create a new message, const message is equal to resource not found.
00:03:10 And then we can set the error to be equal to new error, and we can pass this message right in.
00:03:17 We can also set the status code to 404, so we know exactly what happened.
00:03:22 Resource not found.
00:03:24 Another type of error is a Mongoose duplicate key.
00:03:27 That is when we're trying to create something with the same key.
00:03:30 And this one happens if the error code is 11,000. Don't ask me how I know that I've encountered this error one too many times.
00:03:39 So if that is the case, we can again, form a message equal to something like duplicate field value entered.
00:03:46 And we can set that error to be equal to new error message and the status code of 400. Another type of common error is a mongoose validation error.
00:03:57 So when we're trying to create a document and we don't pass all the right props, so we can check if error.name is equal to validation error.
00:04:06 In this case, we'll form the message by mapping over the values of the object.
00:04:12 because we might have many validation errors and we'll show a message for each one.
00:04:17 Once we do that, we can simply set that message to the error and join it based on commas and spaces.
00:04:24 So for each one of these validation errors, we'll add it to the error message.
00:04:29 And finally, We can return the response from this error middleware by saying res.status is equal to the error.status code.
00:04:38 It can be either 404 or 400. Or if it doesn't exist, we can make it 500, which is a general server error.
00:04:47 And we can return a JSON success is false, as well as an error equal to error.message or just a string of server error.
00:05:00 So in simple words, what we're doing here is we're intercepting the error and trying to find a bit more information about it,
00:05:08 so we much more quickly know what went wrong.
00:05:11 Now we can export that error middleware by saying export default error middleware, and we can use it within the app.
00:05:20 So head over to app.js and then let's add it right here below the routes.
00:05:25 That'll be app.use and you can call it error middleware and make sure to import it from errormiddleware.js.
00:05:34 Alongside this custom piece of middleware that we created, Express also has many built-in middlewares that we can use.
00:05:42 Let's actually add them right here at the top.
00:05:45 The first one you can see auto recommended to me is app.use express.json.
00:05:53 This allows your app to handle JSON data sent in requests or API calls.
00:05:58 Something that is super common.
00:06:00 Another one is app.use express.url encoded extended is set to false.
00:06:08 This helps us to process the form data sent via HTML forms in a simple format.
00:06:14 And another one you'll often see used is app.use and that is cookie parser.
00:06:21 This one has to be imported at the top.
00:06:23 So make sure to say import cookie parser coming from cookie-parser.
00:06:31 This one, as the name says, reads cookies from incoming requests so your app can store user data.
00:06:37 If you want to see how all of these works, you can just click into them and immediately see the code.
00:06:43 And since this is the middleware, you know what it has to have.
00:06:46 It has to modify the request, right?
00:06:49 Do something with it.
00:06:50 There's a reason why we had to have it in the first place.
00:06:53 Intercepted it, did something with the request, modified it, and then, check this out, we're calling the next function, letting the request pass through.
00:07:02 And it's similar with all the other middlewares.
00:07:04 Later on, once we start working with our database, we'll make one or two of these rookie mistakes and we'll get a very nice error message saying resource
00:07:13 not found or duplicate field value entered, or maybe a validation error.
00:07:18 Either way, creating this global error handling middleware will save us hours of debugging later on, and it'll make our app that much more scalable.
00:07:27 Great work.