diff --git a/lib/bindings/http/receiver_binary_0_2.js b/lib/bindings/http/receiver_binary_0_2.js index 8859002..639bd1e 100644 --- a/lib/bindings/http/receiver_binary_0_2.js +++ b/lib/bindings/http/receiver_binary_0_2.js @@ -5,6 +5,11 @@ const Spec02 = require("../../specs/spec_0_2.js"); const JSONParser = require("../../formats/json/parser.js"); +const { + isDefinedOrThrow, + isStringOrObjectOrThrow +} = require("../../utils/fun.js"); + const parserByType = {}; parserByType[Constants.MIME_JSON] = new JSONParser(); @@ -48,20 +53,18 @@ setterByHeader[Constants.HEADER_CONTENT_TYPE] = { }; function validateArgs(payload, attributes) { - if(!payload){ - throw {message: "payload is null or undefined"}; - } - if(!attributes) { - throw {message: "attributes is null or undefined"}; - } + Array.of(payload) + .filter(p => isDefinedOrThrow(p, + {message: "payload is null or undefined"})) + .filter(p => isStringOrObjectOrThrow(p, + {message: "payload must be an object or a string"})) + .shift(); - if((typeof payload) !== "object" && (typeof payload) !== "string"){ - throw { - message: "payload must be an object or a string", - errors: [typeof payload] - }; - } + Array.of(attributes) + .filter(a => isDefinedOrThrow(a, + {message: "attributes is null or undefined"})) + .shift(); } function Receiver(configuration) { @@ -128,7 +131,7 @@ Receiver.prototype.parse = function(payload, headers) { parserByType[sanityHeaders[Constants.HEADER_CONTENT_TYPE]] .parse(payload); - // Every unprocessed header should be an extension + // Every unprocessed header can be an extension Array.from(Object.keys(sanityHeaders)) .filter(value => !processedHeaders.includes(value)) .filter(value => diff --git a/lib/bindings/http/receiver_structured_0_2.js b/lib/bindings/http/receiver_structured_0_2.js index 001a223..cff90b0 100644 --- a/lib/bindings/http/receiver_structured_0_2.js +++ b/lib/bindings/http/receiver_structured_0_2.js @@ -5,6 +5,11 @@ const Spec02 = require("../../specs/spec_0_2.js"); var JSONParser = require("../../formats/json/parser.js"); +const { + isDefinedOrThrow, + isStringOrObjectOrThrow +} = require("../../utils/fun.js"); + const spec02 = new Spec02(); const jsonParserSpec02 = new JSONParser(); @@ -50,20 +55,18 @@ setterByAttribute[Constants.STRUCTURED_ATTRS_02.DATA] = { }; function validateArgs(payload, attributes) { - if(!payload){ - throw {message: "payload is null or undefined"}; - } - if(!attributes) { - throw {message: "attributes is null or undefined"}; - } + Array.of(payload) + .filter(p => isDefinedOrThrow(p, + {message: "payload is null or undefined"})) + .filter(p => isStringOrObjectOrThrow(p, + {message: "payload must be an object or string"})) + .shift(); - if((typeof payload) !== "object" && (typeof payload) !== "string"){ - throw { - message: "payload must be an object or string", - errors: [typeof payload] - }; - } + Array.of(attributes) + .filter(a => isDefinedOrThrow(a, + {message: "attributes is null or undefined"})) + .shift(); } function Receiver(configuration) {