forked from ceejbot/recurring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecurly-error.js
85 lines (74 loc) · 2.44 KB
/
recurly-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
81
82
83
84
85
"use strict";
const _ = require("lodash");
const util = require("util");
const debug = require("debug")("recurring:error");
const RecurlyError = function RecurlyError(struct) {
debug("constructing RecurlyError from %O", struct);
this.name = "RecurlyError";
this.errors = [];
if (typeof struct === "string") {
this.message = struct;
}
// Handle transaction errors.
if (struct.transaction_error) {
// massage the output a little bit.
if (struct.error) {
struct.error.message = struct.error["#"] || "";
delete struct.error["#"];
// Add the error detail.
this.errors.push(struct.error);
}
// Add extra detail about the error.
this.message = struct.transaction_error.merchant_message;
this.error_code = struct.transaction_error.error_code;
this.error_category = struct.transaction_error.error_category;
this.merchant_message = struct.transaction_error.merchant_message;
this.customer_message = struct.transaction_error.customer_message;
this.gateway_error_code = struct.transaction_error.gateway_error_code;
}
// Handle the case where there is a single validation error.
else if (struct.symbol) {
// massage the output a little bit...
// {
// symbol: 'not_found',
// description: {
// '#': 'Couldn\'t find Account with account_code = some-invalid-id',
// lang: 'en-US'
// }
// }
if (struct.symbol === "not_found") {
this.message = _.get(struct, "description.#");
this.error_code = "not_found";
delete struct.description;
}
// { '#': 'is not a valid email address',
// field: 'account.email',
// symbol: 'invalid_email'
// }
else {
struct.message = struct["#"] || struct.message;
this.message = [struct.field, struct.message].join(" ");
delete struct["#"];
}
this.errors.push(struct);
}
// Handle case where there are multiple validation errors.
else {
const keys = Object.keys(struct);
for (let i = 0; i < keys.length; i++) {
// massage the output a little bit.
const err = struct[keys[i]];
err.message = err["#"];
delete err["#"];
this.errors.push(err);
}
if (this.errors.length === 1) {
this.message = this.errors[0].message;
} else {
this.message = `${this.errors.length} validation errors`;
}
}
debug("constructed RecurlyError", this);
};
util.inherits(RecurlyError, Error);
module.exports = RecurlyError;