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

common: create PartialFailureError #1764

Merged
merged 2 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions packages/common/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ util.ApiError = function(errorBody) {
return new ApiError(errorBody);
};

/**
* Custom error type for partial errors returned from the API.
*
* @param {object} b - Error object.
*/
var PartialFailureError = createErrorClass('PartialFailureError', function(b) {
var errorObject = b;

this.errors = errorObject.errors;
this.response = errorObject.response;

var defaultErrorMessage = 'A failure occurred during this request.';
this.message = errorObject.message || defaultErrorMessage;
});

/**
* Wrap the PartialFailureError constructor so context isn't lost.
*
* @param {object} errorBody - Error object.
*/
util.PartialFailureError = function(errorBody) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

return new PartialFailureError(errorBody);
};

/**
* Uniformly process an API response.
*
Expand Down
29 changes: 29 additions & 0 deletions packages/common/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,35 @@ describe('common/util', function() {
});
});

describe('PartialFailureError', function() {
it('should build correct PartialFailureError', function() {
var error = {
errors: [ new Error(), new Error() ],
response: { a: 'b', c: 'd' },
message: 'Partial failure occurred'
};

var partialFailureError = new util.PartialFailureError(error);

assert.strictEqual(partialFailureError.errors, error.errors);
assert.strictEqual(partialFailureError.response, error.response);
assert.strictEqual(partialFailureError.message, error.message);
});

it('should use default message', function() {
var expectedErrorMessage = 'A failure occurred during this request.';

var error = {
errors: [],
response: { a: 'b', c: 'd' }
};

var partialFailureError = new util.PartialFailureError(error);

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

describe('extendGlobalConfig', function() {
it('should favor `keyFilename` when `credentials` is global', function() {
var globalConfig = { credentials: {} };
Expand Down