-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.js
80 lines (71 loc) · 2.67 KB
/
Error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
var utils = require('./utils');
module.exports = _Error;
/**
* Generic Error klass to wrap any errors returned by figure-sdk-node
*/
function _Error(raw) {
this.populate.apply(this, arguments);
this.stack = new Error(this.message).stack;
}
// Extend Native Error
_Error.prototype = Object.create(Error.prototype);
_Error.prototype.type = 'GenericError';
_Error.prototype.populate = function (type, message) {
this.type = type;
this.message = message;
};
_Error.extend = utils.protoExtend;
/**
* Create subclass of internal Error klass
* (Specifically for errors returned from Figure's REST API)
*/
var FigureError = _Error.FigureError = _Error.extend({
type: 'figure_error',
populate: function populate(error) {
// Move from prototype def (so it appears in stringified obj)
this.type = this.type;
this.stack = new Error(error.message).stack;
this.status = error.status;
this.message = error.message;
this.text = error.text;
if (error.body) this.body = error.body;
this.raw = error;
}
});
/**
* Helper factory which takes raw figure errors and outputs wrapping instances
*/
FigureError.generate = function (rawFigureError) {
switch (rawFigureError.status) {
case 400:
return new _Error.FigureBadRequestError(rawFigureError);
case 401:
return new _Error.FigureAuthenticationError(rawFigureError);
case 403:
return new _Error.FigureAuthorizationError(rawFigureError);
case 404:
return new _Error.FigureNotFoundError(rawFigureError);
case 410:
return new _Error.FigureResourceDeletedError(rawFigureError);
case 429:
return new _Error.FigureRateLimitError(rawFigureError);
case 500:
return new _Error.FigureInternalServerError(rawFigureError);
case 502:
return new _Error.FigureConnectionError(rawFigureError);
case 503:
return new _Error.FigureConnectionError(rawFigureError);
}
return new _Error('Generic', 'Unknown Error');
};
// Specific Figure Error types:
_Error.FigureBadRequestError = FigureError.extend({ type: 'bad_request_error' });
_Error.FigureAuthenticationError = FigureError.extend({ type: 'authentication_error' });
_Error.FigureAuthorizationError = FigureError.extend({ type: 'authorization_error' });
_Error.FigureNotFoundError = FigureError.extend({ type: 'not_found_error' });
_Error.FigureRateLimitError = FigureError.extend({ type: 'rate_limit_error' });
_Error.FigureInternalServerError = FigureError.extend({ type: 'internal_server_error' });
_Error.FigureConnectionError = FigureError.extend({ type: 'connection_error' });
_Error.FigureNotAvailableYetError = FigureError.extend({ type: 'not_available_yet_error' });
_Error.FigureResourceDeletedError = FigureError.extend({ type: 'resource_deleted_error' });