Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions lib/api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Contributor Author

@mcbouslog mcbouslog Mar 10, 2017

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.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error.response.body.error is pretty convoluted, and confusing, here.

Copy link
Contributor

Choose a reason for hiding this comment

The 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') {
Expand All @@ -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).
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

});

Expand Down