UNPKG

3.11 kBJavaScriptView Raw
1'use strict';
2
3var util = require('util');
4
5// IdendifiedApiError
6//
7// Marker class for errors that have been identified as known errors
8// communicating with the API. You should *not* instantiate these
9// directly.
10//
11// - @constructor
12function IdentifiedApiError(){}
13util.inherits(IdentifiedApiError, Error);
14
15// ApiHttpError
16//
17// Creates a new ApiHttpError supplied to callbacks when an error response is
18// received at transport level.
19//
20// - @constructor
21// - @param {Number} statusCode - The HTTP status code of the request.
22// - @param {String} response - (Optional) The response body.
23// - @param {String} message - (Optional) The message
24function ApiHttpError(statusCode, response, message) {
25 this.name = "ApiHttpError";
26 this.statusCode = statusCode;
27 this.response = response;
28 this.message = message || response
29 || util.format('Unexpected %s status code', statusCode);
30
31 if (Error.captureStackTrace
32 && typeof Error.captureStackTrace === 'function') {
33 Error.captureStackTrace(this, ApiHttpError);
34 }
35}
36
37util.inherits(ApiHttpError, IdentifiedApiError);
38
39// ApiParseError
40//
41// Creates a new ApiParseError supplied to callbacks when an invalid or
42// unexpected response is received.
43//
44// - @constructor
45// - @param {String} parseErrorMessage - Custom error message
46// describing the nature of the parse error.
47// - @param {String} response - The response body string.
48function ApiParseError(parseErrorMessage, response) {
49 this.name = "ApiParseError";
50 this.response = response;
51 this.message = parseErrorMessage;
52
53 if (Error.captureStackTrace
54 && typeof Error.captureStackTrace === 'function') {
55 Error.captureStackTrace(this, ApiParseError);
56 }
57}
58
59util.inherits(ApiParseError, IdentifiedApiError);
60
61// OAuthError
62//
63// Creates a new ApiError supplied to callbacks when a valid error response is
64// received.
65//
66// - @constructor
67// - @param {Object} errorResponse - The parsed API error response
68// - @param {Object} message - The message
69function OAuthError(errorResponse, message) {
70 this.name = "OAuthError";
71 this.message = message || errorResponse.errorMessage;
72 this.code = errorResponse.code;
73 this.response = errorResponse;
74
75 if (Error.captureStackTrace
76 && typeof Error.captureStackTrace === 'function') {
77 Error.captureStackTrace(this, OAuthError);
78 }
79}
80
81util.inherits(OAuthError, IdentifiedApiError);
82
83// ApiError
84//
85// Creates a new ApiError supplied to callbacks when a valid error response is
86// received.
87//
88// - @constructor
89// - @param {Object} errorResponse - The parsed API error response
90// - @param {Object} message - The message
91function ApiError(errorResponse, message) {
92 this.name = "ApiError";
93 this.message = message || errorResponse.errorMessage;
94 this.code = errorResponse.code;
95 this.response = errorResponse;
96
97 if (Error.captureStackTrace
98 && typeof Error.captureStackTrace === 'function') {
99 Error.captureStackTrace(this, ApiError);
100 }
101}
102
103util.inherits(ApiError, IdentifiedApiError);
104
105module.exports = {
106 ApiHttpError: ApiHttpError,
107 ApiParseError: ApiParseError,
108 OAuthError: OAuthError,
109 ApiError: ApiError,
110 IdentifiedApiError: IdentifiedApiError
111};