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

core: use error message from response body #923

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,16 @@ util.noop = noop;
var ApiError = createErrorClass('ApiError', function(errorBody) {
this.errors = errorBody.errors;
this.code = errorBody.code;
this.message = errorBody.message || 'Error during request.';
this.message = errorBody.message;
this.response = errorBody.response;

if (!this.message) {
if (errorBody.errors && errorBody.errors.length === 1) {
this.message = errorBody.errors[0].message;
} else {
this.message = 'Error during request.';
}
}
});

/**
Expand Down
53 changes: 50 additions & 3 deletions test/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,63 @@ describe('common/util', function() {
assert.strictEqual(apiError.response, error.response);
});

it('should build ApiError with default status message', function() {
it('should detect ApiError message from response body', function() {
var errorMessage = 'API error message';

var error = {
errors: [],
errors: [ new Error(errorMessage) ],
code: 100,
response: { a: 'b', c: 'd' }
};

var apiError = new util.ApiError(error);

assert.strictEqual(apiError.message, 'Error during request.');
assert.strictEqual(apiError.message, errorMessage);
});

it('should favor an explicit message over response body', function() {
var errorMessage = 'API error message';
var expectedErrorMessage = 'Custom error message';

var error = {
errors: [ new Error(errorMessage) ],
code: 100,
response: { a: 'b', c: 'd' },
message: expectedErrorMessage
};

var apiError = new util.ApiError(error);

assert.strictEqual(apiError.message, expectedErrorMessage);
});

it('should use default message if there are no errors', function() {
var expectedErrorMessage = 'Error during request.';

var error = {
code: 100,
response: { a: 'b', c: 'd' },
message: expectedErrorMessage
};

var apiError = new util.ApiError(error);

assert.strictEqual(apiError.message, expectedErrorMessage);
});

it('should use default message if too many errors', function() {
var expectedErrorMessage = 'Error during request.';

var error = {
errors: [ new Error(), new Error() ],
code: 100,
response: { a: 'b', c: 'd' },
message: expectedErrorMessage
};

var apiError = new util.ApiError(error);

assert.strictEqual(apiError.message, expectedErrorMessage);
});
});

Expand Down