Skip to content

Commit

Permalink
Rework handleResponse and response error class
Browse files Browse the repository at this point in the history
  • Loading branch information
zarathustra323 committed Oct 12, 2018
1 parent 24a8ecc commit 4c188b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
21 changes: 11 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const MarketingCloudAuth = require('marketing-cloud-auth');
const applyAuthHeader = require('./utils/apply-auth-header');
const ResponseError = require('./objects/response-error');

class MarkingCloudSOAP {
class MarketingCloudSOAP {
/**
* For WSDL information visit: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/wsdl-endpoint-links.htm
*
Expand Down Expand Up @@ -39,7 +39,7 @@ class MarkingCloudSOAP {
Properties: props,
},
});
return this.handleResponse({ result, rawResponse, rawRequest });
return MarketingCloudSOAP.handleResponse({ result, rawResponse, rawRequest });
}

/**
Expand Down Expand Up @@ -77,16 +77,17 @@ class MarkingCloudSOAP {
* @private
* @param {object} params
*/
handleResponse({ result, rawResponse, rawRequest } = {}) {
if (result && result.OverallStatus && result.OverallStatus !== 'OK') {
throw new ResponseError({
result,
rawResponse,
rawRequest
}, result.OverallStatus);
static handleResponse({ result, rawResponse, rawRequest } = {}) {
if (!result && !result.OverallStatus) {
throw new ResponseError({ result, rawResponse, rawRequest }, 'Unable to parse response status.');
}

const { OverallStatus } = result;
if (ResponseError.pattern.test(OverallStatus)) {
throw new ResponseError({ result, rawResponse, rawRequest }, OverallStatus);
}
return result;
}
}

module.exports = MarkingCloudSOAP;
module.exports = MarketingCloudSOAP;
13 changes: 12 additions & 1 deletion src/objects/response-error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
class ResponseError extends Error {
/**
*
* @param {object} params
* @param {object} params.result
* @param {string} params.rawResponse
* @param {string} params.rawRequest
* @param {string} message
* @param {...any} args
*/
constructor({ result, rawResponse, rawRequest } = {}, message, ...args) {
const formatted = message ? message.replace(/^error: /i, '') : message;
const formatted = message ? message.replace(ResponseError.pattern, '') : message;
super(formatted, ...args);

if (Error.captureStackTrace) {
Expand All @@ -13,4 +22,6 @@ class ResponseError extends Error {
}
}

ResponseError.pattern = /^error: /i;

module.exports = ResponseError;

0 comments on commit 4c188b0

Please sign in to comment.