'use strict';
var enumerate = require('./enumerate');
/**
* @class
* @global
* @param {object} options Construction options
* @classdesc This class is used to report error conditions, frequently as the first parameter to callbacks throughout the Braintree SDK.
* @description <strong>You cannot use this constructor directly. Interact with instances of this class through {@link errback errbacks}.</strong>
*/
function BraintreeError(options) {
if (!BraintreeError.types.hasOwnProperty(options.type)) {
throw new Error(options.type + ' is not a valid type');
}
if (!options.message) {
throw new Error('Error message required');
}
/**
* @type {string}
* @description A short description of the error
*/
this.message = options.message;
/**
* @type {BraintreeError.types}
* @description The type of error
*/
this.type = options.type;
/**
* @type {object=}
* @description Additional information about the error, such as an underlying network error response
*/
this.details = options.details;
}
BraintreeError.prototype = Object.create(Error.prototype);
BraintreeError.prototype.constructor = BraintreeError;
/**
* Enum for {@link BraintreeError} types
* @name BraintreeError.types
* @enum
* @readonly
* @memberof BraintreeError
* @property {string} CUSTOMER Error caused by the customer
* @property {string} MERCHANT Error that is actionable by the merchant
* @property {string} NETWORK Error due to a network problem
* @property {string} INTERNAL Error caused by Braintree code
* @property {string} UNKNOWN Error of unknown origin
*/
BraintreeError.types = enumerate([
'CUSTOMER',
'MERCHANT',
'NETWORK',
'INTERNAL',
'UNKNOWN'
]);
module.exports = BraintreeError;