Skip to content

Commit

Permalink
fix: dep. inject
Browse files Browse the repository at this point in the history
  • Loading branch information
elin.angelov committed Mar 9, 2018
1 parent 3387ddb commit 4de52fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
41 changes: 20 additions & 21 deletions errors/index.js
@@ -1,26 +1,25 @@
const errorList = Object.keys(require('./list.json'));
const messages = Object.keys(require('../messages.json'));
const defineError = require('ut-error').define;
const Payshield = defineError('payshield');
const Parser = defineError('parser', Payshield, 'parser error');

var errors = {
parser: Parser,
unableMatchingHeaderPattern: defineError('unableMatchingHeaderPattern', Payshield, 'Unable to match header to header pattern!'),
unknownResponseCode: defineError('unknownResponseCode', Payshield, 'Unknown response code'),
notimplemented: defineError('notimplemented', Payshield, 'Not implemented'),
unableMatchingResponseCode: defineError('unableMatchingResponseCode', Payshield, 'Unable to match response errorCode!'),
unableMatchingPattern: defineError('unableMatchingPattern', Payshield, 'Unable to match pattern')
module.exports = (defineError) => {
const Payshield = defineError('payshield');
const Parser = defineError('parser', Payshield, 'parser error');

};

errors = messages.reduce((a1, c1) => {
const MsgErr = defineError(c1, Payshield, `Error in method: ${c1}`);
return errorList.reduce((a2, c2) => {
var Err = defineError(c2, MsgErr, errorList[c2]);
a2[`${c1}.${c2}`] = (cause) => (new Err(cause));
return a2;
}, a1);
}, errors);
var errors = {
parser: Parser,
unableMatchingHeaderPattern: defineError('unableMatchingHeaderPattern', Payshield, 'Unable to match header to header pattern!'),
unknownResponseCode: defineError('unknownResponseCode', Payshield, 'Unknown response code'),
notimplemented: defineError('notimplemented', Payshield, 'Not implemented'),
unableMatchingResponseCode: defineError('unableMatchingResponseCode', Payshield, 'Unable to match response errorCode!'),
unableMatchingPattern: defineError('unableMatchingPattern', Payshield, 'Unable to match pattern')
};

module.exports = errors;
return messages.reduce((a1, c1) => {
const MsgErr = defineError(c1, Payshield, `Error in method: ${c1}`);
return errorList.reduce((a2, c2) => {
var Err = defineError(c2, MsgErr, errorList[c2]);
a2[`${c1}.${c2}`] = (cause) => (new Err(cause));
return a2;
}, a1);
}, errors);
};
24 changes: 12 additions & 12 deletions index.js
@@ -1,6 +1,5 @@
var bitsyntax = require('ut-bitsyntax');
var merge = require('lodash.merge');
var errors = require('./errors');
var defaultFormat = require('./messages');

function PayshieldCodec(config, val, log) {
Expand All @@ -11,6 +10,7 @@ function PayshieldCodec(config, val, log) {
this.headerPattern = null;
this.commandNames = {};
this.init(config);
this.errors = require('./errors')(config.defineError);
}

PayshieldCodec.prototype.init = function(config) {
Expand All @@ -28,14 +28,14 @@ PayshieldCodec.prototype.init = function(config) {
var commandsObj = merge({}, defaultFormat, config.messageFormat);

if (this.headerPattern === false) {
throw errors.parser({cause: 'Cant parse header pattern!'});
throw this.errors.parser({cause: 'Cant parse header pattern!'});
}
for (var property in commandsObj) {
if (commandsObj.hasOwnProperty(property)) {
if (commandsObj[property].requestPattern) {
var requestPattern = bitsyntax.parse(commandsObj[property].requestPattern);
if (!requestPattern) {
throw errors.parser({cause: `Cant parse request pattern for command:${property}!`});
throw this.errors.parser({cause: `Cant parse request pattern for command:${property}!`});
}
this.commands[property + ':request'] = {
pattern: requestPattern,
Expand All @@ -50,7 +50,7 @@ PayshieldCodec.prototype.init = function(config) {
if (commandsObj[property].responsePattern) {
var responsePattern = bitsyntax.parse(commandsObj[property].responsePattern);
if (!responsePattern) {
throw errors.parser({cause: `Cant parse response pattern for command:${property}!`});
throw this.errors.parser({cause: `Cant parse response pattern for command:${property}!`});
}
this.commands[property + ':response'] = {
pattern: responsePattern,
Expand All @@ -72,28 +72,28 @@ PayshieldCodec.prototype.decode = function(buff, $meta) {
}
var headObj = this.headerMatcher(buff);
if (!headObj) {
throw errors.unableMatchingHeaderPattern();
throw this.errors.unableMatchingHeaderPattern();
}

var commandName = this.commandNames[headObj.code];
if (!commandName) {
throw errors.unknownResponseCode({cause: `Unknown response code:${headObj.code}`});
throw this.errors.unknownResponseCode({cause: `Unknown response code:${headObj.code}`});
}
var cmd = this.commands[commandName];
if (!cmd) {
throw errors.notimplemented({cause: `Not implemented opcode:${commandName}`});
throw this.errors.notimplemented({cause: `Not implemented opcode:${commandName}`});
}

var bodyObj = this.errorMatcher(headObj.body);
if (!bodyObj) {
throw errors.unableMatchingResponseCode();
throw this.errors.unableMatchingResponseCode();
}
// 00 = No error
// 02 = Key inappropriate length for algorithm (in some cases is warning)
if (['00', '02'].includes(bodyObj.errorCode)) {
bodyObj = cmd.matcher(headObj.body);
if (!bodyObj) {
throw errors.unableMatchingPattern({cause: `Unable to match pattern for opcode:${commandName}!`});
throw this.errors.unableMatchingPattern({cause: `Unable to match pattern for opcode:${commandName}!`});
}
$meta.trace = headObj.headerNo;
$meta.mtid = cmd.mtid;
Expand All @@ -106,7 +106,7 @@ PayshieldCodec.prototype.decode = function(buff, $meta) {
if (cmd.errorMatcher) { // try to match errorPattern if it exists
defErrCode = (cmd.errorMatcher(headObj.body) || bodyObj).errorCode || defErrCode;
}
let e = errors[`${cmd.method}.${defErrCode}`](bodyObj);
let e = this.errors[`${cmd.method}.${defErrCode}`](bodyObj);
this.log.error && this.log.error(e);
return e;
}
Expand All @@ -119,7 +119,7 @@ PayshieldCodec.prototype.encode = function(data, $meta, context) {
var commandName = $meta.method.split('.').pop() + ':' + $meta.mtid;

if (this.commands[commandName] === undefined) {
throw errors.notimplemented({cause: `Not implemented opcode:${commandName}`});
throw this.errors.notimplemented({cause: `Not implemented opcode:${commandName}`});
}

var headerNo = $meta.trace;
Expand All @@ -133,7 +133,7 @@ PayshieldCodec.prototype.encode = function(data, $meta, context) {

var bodyBuff = bitsyntax.build(this.commands[commandName].pattern, data);
if (!bodyBuff) {
throw errors.parser({cause: `Unable to build body of opcode:${commandName}!`});
throw this.errors.parser({cause: `Unable to build body of opcode:${commandName}!`});
}

var cmdCode = this.commands[commandName].code;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -4,8 +4,7 @@
"main": "./index.js",
"dependencies": {
"lodash.merge": "4.6.0",
"ut-bitsyntax": "^6.0.15",
"ut-error": "^5.4.9"
"ut-bitsyntax": "^6.0.15"
},
"devDependencies": {
"ut-tools": "^5.32.5"
Expand Down

0 comments on commit 4de52fb

Please sign in to comment.