diff --git a/lib/common/util.js b/lib/common/util.js index f558dcb93c3..9a8e89c1729 100644 --- a/lib/common/util.js +++ b/lib/common/util.js @@ -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 @@ -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. diff --git a/lib/storage/file.js b/lib/storage/file.js index b4b08a968a2..f8a748827b8 100644 --- a/lib/storage/file.js +++ b/lib/storage/file.js @@ -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'); @@ -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 @@ -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; } @@ -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; } diff --git a/package.json b/package.json index fa2b14c2815..48fd4d6ce62 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/storage/file.js b/test/storage/file.js index 72af4c0bfd7..62364836c02 100644 --- a/test/storage/file.js +++ b/test/storage/file.js @@ -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(); }); }); @@ -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(); }); @@ -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(); }); }); @@ -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(); });