-
-
Notifications
You must be signed in to change notification settings - Fork 44k
Error handlers needed #261
Copy link
Copy link
Closed
Description
These files need eror handling.
Proper handling tips:
- end early
- use
nextas callback in express middleware - If there are multiple asynchronous functions, such as database queries, they must either be nested or use promise chains to resolve, preferably promises.
End Early
Whenever an error exist in express middleware, the callback function should be ended by returning the callback call with err as the first argument. What does that mean?
Down below is an example of what a proper error handling express route should look like. Notice how the function being passed into app.use method as the second argument has three parameters. This is a standard pattern in express to signify to express two things:
- this is handler is what is called a middleware
- if
next, the callback for the route handler, is called by us, the camper-developer extraordinaire, with an argument, an error has occurred and needs to be handled by the error handler down the road.
app.use('/some-end-point', function(req, res, next) {
// some really cool code happens here <<<<
// then an async call below. Notice the callback being passed into the save method
User.save(function (err) {
// the save method must call the database. Lots of things can go wrong here, so if the driver that connects to the database detects something bad it will call this function that we pass into it with the error it detects. We now need to be responsible campers and deal with this error.
if (err) {
// if we get to this point that means `err` exists and we need to end this function and call the callback.
return next(err);
}
// because of the return in the conditional above, if we reach the code below that means the database returned with no errors. Now we can proceed.
res.send('stuff happend, saved user, end of line');
// it is important to end the route handler in two ways. Either by calling `next` with or without an err argument. This will tell express to look for the next route handler. The other way is to send something down to the user using a `res` method. `res` here is short for response meaning the response to the user. Most of the time you will be using `res.render` but you could also call `res.end` to not send the user anything other than a message that says the request to the server has ended.
});
});Here are some files where errors are not properly being handled. If you would like to help out respond here with the file that you would like call dibs on.
- controllers/bonfires
- controllers/challenges (this one is easy)
- controllers/courseware
- controllers/story (on ice as there is a PR waiting to be merged)
- controllers/user
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels