| 
									
										
										
										
											2015-08-04 01:25:34 -07:00
										 |  |  | import errorHanlder from 'errorhandler'; | 
					
						
							|  |  |  | import accepts from 'accepts'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default function prodErrorHandler() { | 
					
						
							|  |  |  |   if (process.env.NODE_ENV === 'development') { | 
					
						
							|  |  |  |     return errorHanlder({ log: true }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // error handling in production.
 | 
					
						
							|  |  |  |   // disabling eslint due to express parity rules for error handlers
 | 
					
						
							|  |  |  |   return function(err, req, res, next) { // eslint-disable-line
 | 
					
						
							|  |  |  |     // respect err.status
 | 
					
						
							|  |  |  |     if (err.status) { | 
					
						
							|  |  |  |       res.statusCode = err.status; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // default status code to 500
 | 
					
						
							|  |  |  |     if (res.statusCode < 400) { | 
					
						
							|  |  |  |       res.statusCode = 500; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // parse res type
 | 
					
						
							|  |  |  |     var accept = accepts(req); | 
					
						
							|  |  |  |     var type = accept.type('html', 'json', 'text'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 13:20:15 -04:00
										 |  |  |     var message = 'Oops! Something went wrong. Please try again later'; | 
					
						
							| 
									
										
										
										
											2015-08-04 01:25:34 -07:00
										 |  |  |     if (type === 'html') { | 
					
						
							| 
									
										
										
										
											2015-08-16 15:26:43 -07:00
										 |  |  |       if (typeof req.flash === 'function') { | 
					
						
							|  |  |  |         req.flash('errors', { | 
					
						
							|  |  |  |           msg: message | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-08-04 01:25:34 -07:00
										 |  |  |       return res.redirect('/'); | 
					
						
							|  |  |  |       // json
 | 
					
						
							|  |  |  |     } else if (type === 'json') { | 
					
						
							|  |  |  |       res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |       return res.send({ | 
					
						
							|  |  |  |         message: message | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       // plain text
 | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       res.setHeader('Content-Type', 'text/plain'); | 
					
						
							|  |  |  |       return res.send(message); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |