(function(root, factory) {

if (typeof module === 'object' && module.exports) {
    // CommonJS-like environments that support module.exports, like Node.
    module.exports = factory(require('./CommonUtil'), require('./wrappers/ErrorResponse'));
  } 
}(this, function(CommonUtil, ErrorResponse) {
  'use strict';
  
  var exports = function (message) {
            var _this = this;

            Error.call(_this);
            this.stack = (new Error()).stack;

            _this['message'] = '' + message;
            _this['error_response'] = [];
  };

  exports.prototype = Object.create(Error.prototype);

  exports.prototype.constructor = exports;

  exports.constructFromHttpResponse = function (httpErrorMessage, response, obj) {
            obj = obj || new exports(httpErrorMessage);
            if (response) {
                if (response.hasOwnProperty('status')) {
                    obj['http_status_code'] = response['status'];
                }

                if (response.hasOwnProperty('text')) {
                    obj['response_body'] = response['text'];
		    		if (response['text'] && CommonUtil.isJsonString(response['text'])) {
                        var errorResponse = CommonUtil.parseJSON(response['text']);
                        if (errorResponse && errorResponse.hasOwnProperty('errors') && Array.isArray(errorResponse['errors'])) {
                                obj['error_response'] = ErrorResponse.constructFromObject(errorResponse);
                            }
                    }
                }

                if (response.hasOwnProperty('header')) {
                    obj['http_response_headers'] = response['header'];
                }
            }
            return obj;
  };

  exports.getDefaultException = function (error) {
	var exception = new exports(error.message);
	exception['stack'] = error.stack;
	return exception;
  };

/**
* @member {String} http_status_code
*/
exports.prototype['http_status_code'] = undefined;
/**
* @member {wrappers/ErrorResponse} error_response
*/
exports.prototype['error_response'] = undefined;
/**
* @member {wrappers/InvocationContext} invocation_context
*/
exports.prototype['invocation_context'] = undefined;
/**
* @member {Boolean} is_timed_out
*/
exports.prototype['is_timed_out'] = undefined;

  return exports;

}));