Skip to content

Commit

Permalink
Merge pull request #886 from stephenplusplus/spp--custom-errors
Browse files Browse the repository at this point in the history
core: use custom error types
  • Loading branch information
callmehiphop committed Sep 23, 2015
2 parents c65e52c + 5b4d509 commit f229d13
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
30 changes: 15 additions & 15 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
* limitations under the License.
*/

/*jshint strict:false, noarg:false */

/**
* @private
/*!
* @module common/util
*/

'use strict';

var createErrorClass = require('create-error-class');
var extend = require('extend');
var googleAuth = require('google-auto-auth');
var is = require('is');
var nodeutil = require('util');
var request = require('request').defaults({
pool: {
maxSockets: Infinity
Expand Down Expand Up @@ -109,24 +108,25 @@ function noop() {}
util.noop = noop;

/**
* Extend the native Error object.
*
* @constructor
* Custom error type for API errors.
*
* @param {object} errorBody - Error object.
*/
function ApiError(errorBody) {
Error.call(this);
Error.captureStackTrace(this, arguments.callee);
var ApiError = createErrorClass('ApiError', function(errorBody) {
this.errors = errorBody.errors;
this.code = errorBody.code;
this.message = errorBody.message || 'Error during request.';
this.response = errorBody.response;
}

nodeutil.inherits(ApiError, Error);
});

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

/**
* Uniformly process an API response.
Expand Down
27 changes: 17 additions & 10 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'use strict';

var concat = require('concat-stream');
var createErrorClass = require('create-error-class');
var crypto = require('crypto');
var duplexify = require('duplexify');
var format = require('string-format-obj');
Expand All @@ -46,6 +47,16 @@ var Acl = require('./acl.js');
*/
var util = require('../common/util.js');

/**
* Custom error type for errors related to getting signed errors and policies.
*
* @param {string} message - Custom error message.
* @return {Error}
*/
var SigningError = createErrorClass('SigningError', function(message) {
this.message = message;
});

/**
* @const {string}
* @private
Expand Down Expand Up @@ -1066,19 +1077,17 @@ File.prototype.getSignedPolicy = function(options, callback) {

makeAuthenticatedRequest_.getCredentials(function(err, credentials) {
if (err) {
var signingError = new Error('Signing failed. See `error` property.');
signingError.error = err;
callback(signingError);
callback(new SigningError(err.message));
return;
}

if (!credentials.private_key) {
var errorMessage = [
'Signing failed. Could not find a `private_key`.',
'Could not find a `private_key`.',
'Please verify you are authorized with this property available.'
].join(' ');

callback(new Error(errorMessage));
callback(new SigningError(errorMessage));
return;
}

Expand Down Expand Up @@ -1197,19 +1206,17 @@ File.prototype.getSignedUrl = function(options, callback) {

makeAuthenticatedRequest_.getCredentials(function(err, credentials) {
if (err) {
var signingError = new Error('Signing failed. See `error` property.');
signingError.error = err;
callback(signingError);
callback(new SigningError(err.message));
return;
}

if (!credentials.private_key || !credentials.client_email) {
var errorMessage = [
'Signing failed. Could not find a `private_key` or `client_email`.',
'Could not find a `private_key` or `client_email`.',
'Please verify you are authorized with these credentials available.'
].join(' ');

callback(new Error(errorMessage));
callback(new SigningError(errorMessage));
return;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"arrify": "^1.0.0",
"async": "^1.4.2",
"concat-stream": "^1.5.0",
"create-error-class": "^2.0.1",
"dns-zonefile": "0.1.10",
"duplexify": "^3.2.0",
"extend": "^3.0.0",
Expand Down
16 changes: 8 additions & 8 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1499,9 +1499,8 @@ describe('File', function() {
file.getSignedPolicy({
expires: Date.now() + 5
}, function(err) {
var errorMessage = 'Signing failed. See `error` property.';
assert.strictEqual(err.message, errorMessage);
assert.strictEqual(err.error, error);
assert.strictEqual(err.name, 'SigningError');
assert.strictEqual(err.message, error.message);
done();
});
});
Expand All @@ -1516,10 +1515,11 @@ describe('File', function() {
expires: Date.now() + 5
}, function(err) {
var errorMessage = [
'Signing failed. Could not find a `private_key`.',
'Could not find a `private_key`.',
'Please verify you are authorized with this property available.'
].join(' ');

assert.strictEqual(err.name, 'SigningError');
assert.strictEqual(err.message, errorMessage);
done();
});
Expand Down Expand Up @@ -1789,9 +1789,8 @@ describe('File', function() {
action: 'read',
expires: Date.now() + 5
}, function(err) {
var errorMessage = 'Signing failed. See `error` property.';
assert.strictEqual(err.message, errorMessage);
assert.strictEqual(err.error, error);
assert.strictEqual(err.name, 'SigningError');
assert.strictEqual(err.message, error.message);
done();
});
});
Expand All @@ -1807,10 +1806,11 @@ describe('File', function() {
expires: Date.now() + 5
}, function(err) {
var errorMessage = [
'Signing failed. Could not find a `private_key` or `client_email`.',
'Could not find a `private_key` or `client_email`.',
'Please verify you are authorized with these credentials available.'
].join(' ');

assert.strictEqual(err.name, 'SigningError');
assert.strictEqual(err.message, errorMessage);
done();
});
Expand Down

0 comments on commit f229d13

Please sign in to comment.