{"action":"created","issue":{"url":"https://api.github.com/repos/FreeCodeCamp/freecodecamp/issues/261","labels_url":"https://api.github.com/repos/FreeCodeCamp/freecodecamp/issues/261/labels{/name}","comments_url":"https://api.github.com/repos/FreeCodeCamp/freecodecamp/issues/261/comments","events_url":"https://api.github.com/repos/FreeCodeCamp/freecodecamp/issues/261/events","html_url":"https://github.com/FreeCodeCamp/freecodecamp/issues/261","id":63886220,"number":261,"title":"Error handlers needed","user":{"login":"BerkeleyTrue","id":6775919,"avatar_url":"https://avatars.githubusercontent.com/u/6775919?v=3","gravatar_id":"","url":"https://api.github.com/users/BerkeleyTrue","html_url":"https://github.com/BerkeleyTrue","followers_url":"https://api.github.com/users/BerkeleyTrue/followers","following_url":"https://api.github.com/users/BerkeleyTrue/following{/other_user}","gists_url":"https://api.github.com/users/BerkeleyTrue/gists{/gist_id}","starred_url":"https://api.github.com/users/BerkeleyTrue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BerkeleyTrue/subscriptions","organizations_url":"https://api.github.com/users/BerkeleyTrue/orgs","repos_url":"https://api.github.com/users/BerkeleyTrue/repos","events_url":"https://api.github.com/users/BerkeleyTrue/events{/privacy}","received_events_url":"https://api.github.com/users/BerkeleyTrue/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/FreeCodeCamp/freecodecamp/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/FreeCodeCamp/freecodecamp/labels/todo","name":"todo","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-03-24T02:34:51Z","updated_at":"2015-03-26T04:01:07Z","closed_at":null,"body":"These files need eror handling. \r\n\r\nProper handling tips:\r\n\r\n1. end early\r\n2. use `next` as callback in express middleware\r\n3. If there are multiple asynchronous functions, such as database queries, they must either be nested or use promise chains to resolve, preferably promises.\r\n\r\n## End Early\r\n\r\nWhenever 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?\r\n\r\n\r\n\r\nDown 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:\r\n\r\n1. this is handler is what is called a middleware\r\n2. 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.\r\n\r\n```js\r\napp.use('/some-end-point', function(req, res, next) {\r\n  // some really cool code happens here <<<<\r\n  // then an async call below. Notice the callback being passed into the save method\r\n  User.save(function (err) {\r\n    // 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.\r\n    if (err) {\r\n      // if we get to this point that means `err` exists and we need to end this function and call the callback.\r\n      return next(err);\r\n    }\r\n    // 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.\r\n   res.send('stuff happend, saved user, end of line');\r\n   // 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. \r\n  });\r\n});\r\n\r\n```\r\n\r\nHere 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.\r\n\r\n\r\n- [ ] controllers/bonfires\r\n- [ ] controllers/challenges (this one is easy)\r\n- [ ] [controllers/courseware](https://github.com/FreeCodeCamp/freecodecamp/blob/master/controllers/courseware.js)\r\n- [ ] [controllers/story](https://github.com/FreeCodeCamp/freecodecamp/blob/master/controllers/story.js) (on ice as there is a PR waiting to be merged)\r\n- [ ] [controllers/user](https://github.com/FreeCodeCamp/freecodecamp/blob/master/controllers/user.js)"},"comment":{"url":"https://api.github.com/repos/FreeCodeCamp/freecodecamp/issues/comments/86330126","html_url":"https://github.com/FreeCodeCamp/freecodecamp/issues/261#issuecomment-86330126","issue_url":"https://api.github.com/repos/FreeCodeCamp/freecodecamp/issues/261","id":86330126,"user":{"login":"BerkeleyTrue","id":6775919,"avatar_url":"https://avatars.githubusercontent.com/u/6775919?v=3","gravatar_id":"","url":"https://api.github.com/users/BerkeleyTrue","html_url":"https://github.com/BerkeleyTrue","followers_url":"https://api.github.com/users/BerkeleyTrue/followers","following_url":"https://api.github.com/users/BerkeleyTrue/following{/other_user}","gists_url":"https://api.github.com/users/BerkeleyTrue/gists{/gist_id}","starred_url":"https://api.github.com/users/BerkeleyTrue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BerkeleyTrue/subscriptions","organizations_url":"https://api.github.com/users/BerkeleyTrue/orgs","repos_url":"https://api.github.com/users/BerkeleyTrue/repos","events_url":"https://api.github.com/users/BerkeleyTrue/events{/privacy}","received_events_url":"https://api.github.com/users/BerkeleyTrue/received_events","type":"User","site_admin":false},"created_at":"2015-03-26T04:01:07Z","updated_at":"2015-03-26T04:01:07Z","body":"@jameskopacz looks good. Make the PR and I will merge. There is still one more issue with that file but that can be done in a separate PR if you want. There is a `req.user.save()` that needs a callback."}}