diff --git a/index.js b/index.js index 71fa4696..5ba21021 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +exports.config = require('./lib/config'); exports.Client = require('./lib/client'); exports.Dispatcher = require('./lib/dispatcher'); exports.auth = require('./lib/auth'); diff --git a/lib/config/index.js b/lib/config/index.js new file mode 100644 index 00000000..954514a6 --- /dev/null +++ b/lib/config/index.js @@ -0,0 +1,9 @@ +var errors = require('../errors'); + +var config = {}; + +config.setConfig = function(errorName, errorConfig) { + errors[errorName].config = errorConfig; +}; + +module.exports = config; diff --git a/lib/dispatcher.js b/lib/dispatcher.js index f3628ec8..9f0a1f15 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -6,21 +6,13 @@ var querystring = require('querystring'); var VERSION = require('../package.json').version; -var STATUS_MAP = null; - -setTimeout(function() { - STATUS_MAP = Object.keys(errors).reduce(function(map, key) { - if (key === 'config') { - return map; - } - var args = errors.config[key]; - var error = new errors[key](args); - if (error.status) { - map[error.status] = errors[key]; - } - return map; - }, {}); -}, 0); +var STATUS_MAP = Object.keys(errors).reduce(function(map, key) { + var error = new errors[key](null); + if (error.status) { + map[error.status] = errors[key]; + } + return map; +}, {}); // TODO: Provide same set of options for each request as for configuration, // so the config at construction time is just the "defaults" @@ -166,7 +158,9 @@ Dispatcher.prototype.dispatch = function(params, dispatchOptions) { return reject(err); } if (STATUS_MAP[res.statusCode]) { - var error = new STATUS_MAP[res.statusCode](payload); + var errorConfig = STATUS_MAP[res.statusCode].config; + var args = Object.assign(payload, errorConfig); + var error = new STATUS_MAP[res.statusCode](args); if (me.retryOnRateLimit && error instanceof (errors.RateLimitEnforced)) { diff --git a/lib/errors/config.js b/lib/errors/config.js deleted file mode 100644 index fee426ec..00000000 --- a/lib/errors/config.js +++ /dev/null @@ -1,26 +0,0 @@ -var config = {}; - -config.Forbidden = null; - -config.InvalidRequest = null; - -config.NoAuthorization = null; - -config.NotFound = null; - -config.PremiumOnly = null; - -config.RateLimitEnforced = null; - -config.ServerError = null; - -config.setConfig = function(errorName, errorConfig) { - if (!this[errorName]) { - config[errorName] = {}; - } - Object.keys(errorConfig, function(errorKey) { - config[errorKey] = errorConfig[errorKey]; - }); -}; - -module.exports = config; diff --git a/lib/errors/index.js b/lib/errors/index.js index c64376a0..4071a9f3 100644 --- a/lib/errors/index.js +++ b/lib/errors/index.js @@ -1,5 +1,3 @@ -exports.config = require('./config'); - exports.Forbidden = require('./forbidden'); exports.InvalidRequest = require('./invalid_request'); exports.NoAuthorization = require('./no_authorization'); diff --git a/lib/errors/rate_limit_enforced.js b/lib/errors/rate_limit_enforced.js index 007ae860..f37e96f7 100644 --- a/lib/errors/rate_limit_enforced.js +++ b/lib/errors/rate_limit_enforced.js @@ -6,10 +6,9 @@ function RateLimitEnforced(value) { AsanaError.call(this, 'Rate Limit Enforced'); this.status = 429; this.value = value; - this.retryAfterSeconds = value && parseInt(value.retry_after, 10); } util.inherits(RateLimitEnforced, Error); -module.exports = RateLimitEnforced; \ No newline at end of file +module.exports = RateLimitEnforced; diff --git a/test/dispatcher_spec.js b/test/dispatcher_spec.js index 83816a68..82170e4c 100644 --- a/test/dispatcher_spec.js +++ b/test/dispatcher_spec.js @@ -169,9 +169,6 @@ describe('Dispatcher', function() { }); Object.keys(errors).forEach(function(key) { - if (key === 'config') { - return; - } it('should create an error for ' + key, function() { var request = sinon.stub(); var err = new errors[key]();