Skip to content

Commit 9119d07

Browse files
common: create PartialFailureError
1 parent 77fc80c commit 9119d07

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

packages/common/src/util.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,30 @@ util.ApiError = function(errorBody) {
107107
return new ApiError(errorBody);
108108
};
109109

110+
/**
111+
* Custom error type for partial errors returned from the API.
112+
*
113+
* @param {object} b - Error object.
114+
*/
115+
var PartialFailureError = createErrorClass('PartialFailureError', function(b) {
116+
var errorObject = b;
117+
118+
this.errors = errorObject.errors;
119+
this.response = errorObject.response;
120+
121+
var defaultErrorMessage = 'A failure occurred during this request.';
122+
this.message = errorObject.message || defaultErrorMessage;
123+
});
124+
125+
/**
126+
* Wrap the PartialFailureError constructor so context isn't lost.
127+
*
128+
* @param {object} errorBody - Error object.
129+
*/
130+
util.PartialFailureError = function(errorBody) {
131+
return new PartialFailureError(errorBody);
132+
};
133+
110134
/**
111135
* Uniformly process an API response.
112136
*

packages/common/test/util.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,35 @@ describe('common/util', function() {
249249
});
250250
});
251251

252+
describe('PartialFailureError', function() {
253+
it('should build correct PartialFailureError', function() {
254+
var error = {
255+
errors: [ new Error(), new Error() ],
256+
response: { a: 'b', c: 'd' },
257+
message: 'Partial failure occurred'
258+
};
259+
260+
var partialFailureError = new util.PartialFailureError(error);
261+
262+
assert.strictEqual(partialFailureError.errors, error.errors);
263+
assert.strictEqual(partialFailureError.response, error.response);
264+
assert.strictEqual(partialFailureError.message, error.message);
265+
});
266+
267+
it('should use default message', function() {
268+
var expectedErrorMessage = 'A failure occurred during this request.';
269+
270+
var error = {
271+
errors: [],
272+
response: { a: 'b', c: 'd' }
273+
};
274+
275+
var partialFailureError = new util.PartialFailureError(error);
276+
277+
assert.strictEqual(partialFailureError.message, expectedErrorMessage);
278+
});
279+
});
280+
252281
describe('extendGlobalConfig', function() {
253282
it('should favor `keyFilename` when `credentials` is global', function() {
254283
var globalConfig = { credentials: {} };

0 commit comments

Comments
 (0)