forked from TheThingSystem/node-flower-power-cloud
/
ApiError.js
36 lines (29 loc) · 834 Bytes
/
ApiError.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
function ApiError(code, body) {
this.code = code;
this.errors = [];
if (body.errors && body.errors.length > 0) {
this.errors = body.errors;
}
else this.errors.push(body);
return (this);
}
ApiError.prototype = Object.create(Error.prototype);
ApiError.prototype.constructor = ApiError;
ApiError.prototype.toString = function() {
var str = "CODE: " + this.code;
for (var error of this.errors) {
str += "\n";
if (error.error && error.error_description) {
str += error.error + ": " + error.error_description;
} else if (error.error_code && error.error_message) {
str += error.error_code + ": " + error.error_message;
} else if (typeof error == 'string') {
str += error;
} else {
var key = Object.keys(error)[0];
str += key + ": " + error[key];
}
}
return (str);
}
module.exports = ApiError;