-
-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add logging for error handling edge cases #742
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
var logger = require( 'pelias-logger' ).get( 'middleware-500' ); | ||
var check = require('check-types'), | ||
logger = require( 'pelias-logger' ).get( 'api' ); | ||
|
||
// handle application errors | ||
function middleware(err, req, res, next) { | ||
|
||
logger.error( 'Error: `%s`. Stack trace: `%s`.', err, err.stack ); | ||
res.header('Cache-Control','public'); | ||
var error = (err && err.message) ? err.message : err; | ||
|
||
if( res.statusCode < 400 ){ res.status(500); } | ||
res.json({ error: typeof error === 'string' ? error : 'internal server error' }); | ||
if( res.statusCode < 400 ){ | ||
logger.info( 'status code changed from', res.statusCode, 'to 500' ); | ||
res.status(500); | ||
} | ||
|
||
var error = ( err && err.message ) ? err.message : err; | ||
res.header('Cache-Control','public'); | ||
res.json({ error: check.nonEmptyString( error ) ? error : 'internal server error' }); | ||
} | ||
|
||
module.exports = middleware; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
var check = require('check-types'), | ||
es = require('elasticsearch'), | ||
logger = require( 'pelias-logger' ).get( 'api' ), | ||
exceptions = require('elasticsearch-exceptions/lib/exceptions/SupportedExceptions'); | ||
|
||
// create a list of regular expressions to match against. | ||
|
@@ -42,7 +43,10 @@ function sendJSONResponse(req, res, next) { | |
if( err instanceof es.errors.RequestTimeout ){ statusCode = Math.max( statusCode, 408 ); } | ||
else if( err instanceof es.errors.NoConnections ){ statusCode = Math.max( statusCode, 502 ); } | ||
else if( err instanceof es.errors.ConnectionFault ){ statusCode = Math.max( statusCode, 502 ); } | ||
else { statusCode = Math.max( statusCode, 500 ); } | ||
else { | ||
logger.warn( 'unknown geocoding error object:', err.constructor.name, err ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i thought we were going to check for existance of err.contructor here? seems a bit optimistic to assume it's there There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a think about it and the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for some reason.. even with a typo the above works :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh right, it works for any function:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and because javascript; everything apparently
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh javascript...
|
||
statusCode = Math.max( statusCode, 500 ); | ||
} | ||
|
||
/* | ||
some elasticsearch errors are only returned as strings (not instances of Error). | ||
|
@@ -55,7 +59,10 @@ function sendJSONResponse(req, res, next) { | |
statusCode = Math.max( statusCode, 500 ); | ||
break; // break on first match | ||
} | ||
logger.warn( 'unknown geocoding error string:', err ); | ||
} | ||
} else { | ||
logger.warn( 'unknown geocoding error type:', typeof err, err ); | ||
} | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need spaces after
from
and beforeto
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I learn something new about javascript every day!