Skip to content

Commit

Permalink
[FIX] Add a clear message so the user can easily find which one is no… (
Browse files Browse the repository at this point in the history
#1)

* [FIX] Add a clear message so the user can easily find which one is not working

* Throw augmented error

* Cast to custom error when wrong supplier

* List all the invalid params in the error
  • Loading branch information
Raphael Moutard authored May 23, 2019
1 parent 3228d41 commit 140cd62
Show file tree
Hide file tree
Showing 4 changed files with 344 additions and 287 deletions.
8 changes: 6 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
},
"env": {
"browser": true,
"node": true
"node": true,
"es6": true,
},
"extends": "eslint:recommended"
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
}
}
90 changes: 74 additions & 16 deletions lib/sepa.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@
'pain.008.003.02': 'CstmrDrctDbtInitn'
};

class InvalidSupplierError extends Error {
constructor(supplierName, invalidParams, ...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params);

// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, InvalidSupplierError);
}

this.name = 'InvalidSupplierError';
// Custom debugging information
this.supplierName = supplierName;
this.invalidParams = invalidParams;
}
}

function getPainXMLVersion(painFormat) {
var inc = painFormat.indexOf('pain.008') === 0 ? 1 : 0;
return parseInt(painFormat.substr(-2), 10) + inc;
Expand Down Expand Up @@ -347,6 +364,7 @@
},

validate: function() {

// TODO consider using getters/setters instead
var pullFrom = this.method === PaymentInfoTypes.DirectDebit ? 'creditor' : 'debtor';

Expand All @@ -362,14 +380,15 @@

assert_cid(this[pullFrom + 'Id'], pullFrom + 'Id');

assert_length(this[pullFrom + 'Name'], null, 70, pullFrom + 'Name');
assert_length(this[pullFrom + 'Street'], null, 70, pullFrom + 'Street');
assert_length(this[pullFrom + 'City'], null, 70, pullFrom + 'City');
assert_length(this[pullFrom + 'Country'], null, 2, pullFrom + 'Country');
assert_iban(this[pullFrom + 'IBAN'], pullFrom + 'IBAN');
assert_length(this[pullFrom + 'BIC'], [0,8,11], pullFrom + 'BIC');
var countryMatches = (this[pullFrom + 'BIC'].length === 0 || this[pullFrom + 'BIC'].substr(4, 2) === this[pullFrom + 'IBAN'].substr(0, 2));
assert(countryMatches, 'country mismatch in BIC/IBAN');
assert_debitor({
name: this[pullFrom + 'Name'],
street: this[pullFrom + 'Street'],
city: this[pullFrom + 'City'],
country: this[pullFrom + 'Country'],
iban: this[pullFrom + 'IBAN'],
bic: this[pullFrom + 'BIC'],
pullFrom: pullFrom,
});

assert_length(this._payments.length, 1, null, '_payments');
},
Expand Down Expand Up @@ -523,14 +542,15 @@
assert_sepa_id_set2(this.mandateId, 'mandateId');
assert_date(this.mandateSignatureDate, 'mandateSignatureDate');

assert_length(this[pullFrom + 'Name'], null, 70, pullFrom + 'Name');
assert_length(this[pullFrom + 'Street'], null, 70, pullFrom + 'Street');
assert_length(this[pullFrom + 'City'], null, 70, pullFrom + 'City');
assert_length(this[pullFrom + 'Country'], null, 2, pullFrom + 'Country');
assert_iban(this[pullFrom + 'IBAN'], pullFrom + 'IBAN');
assert_fixed(this[pullFrom + 'BIC'].length, [0, 8, 11], pullFrom + 'BIC');
var countryMatches = (this[pullFrom + 'BIC'].length === 0 || this[pullFrom + 'BIC'].substr(4, 2) === this[pullFrom + 'IBAN'].substr(0, 2));
assert(countryMatches, 'country mismatch in BIC/IBAN');
assert_debitor({
name: this[pullFrom + 'Name'],
street: this[pullFrom + 'Street'],
city: this[pullFrom + 'City'],
country: this[pullFrom + 'Country'],
iban: this[pullFrom + 'IBAN'],
bic: this[pullFrom + 'BIC'],
pullFrom: pullFrom,
});

assert_length(this.remittanceInfo, null, 140, 'remittanceInfo');
},
Expand Down Expand Up @@ -688,6 +708,44 @@

// --- Various private functions follow --- //

function assert_debitor({
name,
street,
city,
country,
iban,
bic,
pullFrom,
}) {
// contains all the fields that don't respect the sepa norm.
const invalidFields = [];
if (name && name.length > 70) {
invalidFields.push('name');
}
if (street && street.length > 70) {
invalidFields.push('street');
}
if (city && city.length > 70) {
invalidFields.push('city');
}
if (country && country.length > 2) {
invalidFields.push('country');
}

try {
assert_iban(iban, pullFrom + 'IBAN');
assert_fixed(bic.length, [0, 8, 11], pullFrom + 'BIC');
var countryMatches = (bic.length === 0 || bic.substr(4, 2) === iban.substr(0, 2));
var msg = 'country mismatch in BIC/IBAN for ' + pullFrom + ' called ' + name + ' with BIC: ' + bic + ' and IBAN: ' + iban;
assert(countryMatches, msg);
} catch (err) {
invalidFields.push('IBAN/BIC');
}
if (invalidFields.length > 0) {
throw new InvalidSupplierError(name, invalidFields, msg);
}
}

/** Assert that |cond| is true, otherwise throw an error with |msg| */
function assert(cond, msg) {
if (!cond) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"xmldom": "*"
},
"devDependencies": {
"eslint": "^4.19.1",
"eslint": "*",
"pre-commit": "*",
"uglify-js": "^3.3.20"
},
Expand Down
Loading

0 comments on commit 140cd62

Please sign in to comment.