7digital-api
Version:
7digital API client for nodeJS
130 lines (112 loc) • 3.65 kB
JavaScript
'use strict';
var util = require('util');
var VError = require('verror');
// IdendifiedApiError
//
// Marker class for errors that have been identified as known errors
// communicating with the API. You should *not* instantiate these
// directly.
//
// - @constructor
function IdentifiedApiError(){}
util.inherits(IdentifiedApiError, Error);
// ApiHttpError
//
// Creates a new ApiHttpError supplied to callbacks when an error response is
// received at transport level.
//
// - @constructor
// - @param {Number} statusCode - The HTTP status code of the request.
// - @param {String} response - (Optional) The response body.
// - @param {String} message - (Optional) The message
function ApiHttpError(statusCode, response, message) {
this.name = "ApiHttpError";
this.statusCode = statusCode;
this.response = response;
this.message = message || response
|| util.format('Unexpected %s status code', statusCode);
if (Error.captureStackTrace
&& typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, ApiHttpError);
}
}
util.inherits(ApiHttpError, IdentifiedApiError);
// ApiParseError
//
// Creates a new ApiParseError supplied to callbacks when an invalid or
// unexpected response is received.
//
// - @constructor
// - @param {String} parseErrorMessage - Custom error message
// describing the nature of the parse error.
// - @param {String} response - The response body string.
// - @param {Integer} statusCode - The response HTTP status.
function ApiParseError(parseErrorMessage, response, statusCode) {
this.name = "ApiParseError";
this.response = response;
this.message = parseErrorMessage;
this.statusCode = statusCode;
if (Error.captureStackTrace
&& typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, ApiParseError);
}
}
util.inherits(ApiParseError, IdentifiedApiError);
// RequestError
//
// Creates a new RequestError supplied to callbacks when the request to
// the api fails.
//
function RequestError(err, url) {
VError.call(this, err, 'for url %s', url);
this.name = RequestError.name;
}
util.inherits(RequestError, VError);
// OAuthError
//
// Creates a new ApiError supplied to callbacks when a valid error response is
// received.
//
// - @constructor
// - @param {Object} errorResponse - The parsed API error response
// - @param {Object} message - The message
function OAuthError(errorResponse, message) {
this.name = "OAuthError";
this.message = message || errorResponse.errorMessage;
this.code = errorResponse.code;
this.response = errorResponse;
if (Error.captureStackTrace
&& typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, OAuthError);
}
}
util.inherits(OAuthError, IdentifiedApiError);
// ApiError
//
// Creates a new ApiError supplied to callbacks when a valid error response is
// received.
//
// - @constructor
// - @param {Object} errorResponse - The parsed API error response
// - @param {Object} message - The message
// - @param {Integer} statusCode - The response HTTP status.
function ApiError(errorResponse, message, statusCode) {
this.name = "ApiError";
this.message = message || errorResponse.errorMessage;
this.code = errorResponse.code;
this.response = errorResponse;
this.statusCode = statusCode;
if (Error.captureStackTrace
&& typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, ApiError);
}
}
util.inherits(ApiError, IdentifiedApiError);
module.exports = {
ApiHttpError: ApiHttpError,
ApiParseError: ApiParseError,
OAuthError: OAuthError,
ApiError: ApiError,
IdentifiedApiError: IdentifiedApiError,
RequestError: RequestError
};