-
Notifications
You must be signed in to change notification settings - Fork 6
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
[WIP] edit error handling for object with status and response #61
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 |
---|---|---|
|
@@ -6,22 +6,21 @@ var apiClient = new JSONAPIClient(config.host + '/api', { | |
'Accept': 'application/vnd.api+json; version=1', | ||
}, { | ||
beforeEveryRequest: function() { | ||
console.log('using local panoptes-client'); | ||
var auth = require('./auth'); | ||
return auth.checkBearerToken(); | ||
}, | ||
|
||
handleError: function(response) { | ||
handleError: function(error) { | ||
var errorMessage; | ||
if (response instanceof Error) { | ||
throw response; | ||
} else if (typeof response.body === 'object') { | ||
if (response.body.error) { | ||
errorMessage = response.body.error; | ||
if (response.body.error_description) { | ||
errorMessage += ' ' + response.error_description; | ||
if (typeof error.response.body === 'object') { | ||
if (error.response.body.error) { | ||
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. Errors should maybe follow the JSON API spec for error objects. |
||
errorMessage = error.response.body.error; | ||
if (error.response.body.error_description) { | ||
errorMessage += ' ' + error.response.error_description; | ||
} | ||
} else if (Array.isArray(response.body.errors)) { | ||
errorMessage = response.body.errors.map(function(error) { | ||
} else if (Array.isArray(error.response.body.errors)) { | ||
errorMessage = error.response.body.errors.map(function(error) { | ||
if (typeof error.message === 'string') { | ||
return error.message; | ||
} else if (typeof error.message === 'object') { | ||
|
@@ -33,19 +32,25 @@ var apiClient = new JSONAPIClient(config.host + '/api', { | |
} else { | ||
errorMessage = 'Unknown error (bad response body)'; | ||
} | ||
} else if (response.text.indexOf('<!DOCTYPE') !== -1) { | ||
} else if (error.response.text.indexOf('<!DOCTYPE') !== -1) { | ||
// Manually set a reasonable error when we get HTML back (currently 500s will do this). | ||
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. If we have the status code in the response, why are we sniffing the response body to test for a 500 error? |
||
errorMessage = [ | ||
'There was a problem on the server.', | ||
response.req.url, | ||
response.status, | ||
response.statusText, | ||
error.response.req.url, | ||
error.response.status, | ||
error.response.statusText, | ||
].join(' '); | ||
} else { | ||
errorMessage = 'Unknown error (bad response)'; | ||
} | ||
|
||
throw new Error(errorMessage); | ||
if (typeof error === 'undefined') { | ||
error = new Error(errorMessage); | ||
} else if (typeof error === 'object') { | ||
error.message = errorMessage; | ||
} | ||
|
||
throw error; | ||
} | ||
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. Can you call throw with something that isn't an instance of Error? |
||
}); | ||
|
||
|
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.
will remove, keeping while linking dependencies via GitHub branches.