| 
									
										
										
										
											2017-07-13 11:39:07 -07:00
										 |  |  | import opbeat from 'opbeat'; | 
					
						
							|  |  |  | import debug from 'debug'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   isHandledError, | 
					
						
							|  |  |  |   unwrapHandledError | 
					
						
							|  |  |  | } from '../utils/create-handled-error.js'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const log = debug('fcc:middlewares:error-reporter'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-18 21:33:57 -08:00
										 |  |  | const isOpbeatDisabled = !opbeat.appId; | 
					
						
							|  |  |  | export default function errrorReporter() { | 
					
						
							| 
									
										
										
										
											2017-07-13 11:39:07 -07:00
										 |  |  |   if (process.env.NODE_ENV !== 'production') { | 
					
						
							|  |  |  |     return (err, req, res, next) => { | 
					
						
							|  |  |  |       if (isHandledError(err)) { | 
					
						
							|  |  |  |         // log out user messages in development
 | 
					
						
							|  |  |  |         const handled = unwrapHandledError(err); | 
					
						
							|  |  |  |         log(handled.message); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       next(err); | 
					
						
							|  |  |  |      }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return (err, req, res, next) => { | 
					
						
							|  |  |  |     // handled errors do not need to be reported
 | 
					
						
							|  |  |  |     // the report a message and redirect the user
 | 
					
						
							| 
									
										
										
										
											2018-01-18 21:33:57 -08:00
										 |  |  |     if ( | 
					
						
							|  |  |  |       isOpbeatDisabled || | 
					
						
							|  |  |  |       isHandledError(err) || | 
					
						
							|  |  |  |       // errors with status codes shouldn't be reported
 | 
					
						
							|  |  |  |       // as they are usually user messages
 | 
					
						
							|  |  |  |       err.statusCode || | 
					
						
							|  |  |  |       err.status | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2017-07-13 11:39:07 -07:00
										 |  |  |       return next(err); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return opbeat.captureError(err, { request: req }, () => next(err)); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |