Skip to content

Commit

Permalink
Merge pull request #76 from trayio/CPS-217/b/non-async-function-metho…
Browse files Browse the repository at this point in the history
…d-throws

CPS-217: non async function method throws suuport bug fix
  • Loading branch information
johnbastian-trayio authored Jun 8, 2020
2 parents 3522602 + ccf6854 commit 54382e1
Show file tree
Hide file tree
Showing 5 changed files with 1,073 additions and 203 deletions.
72 changes: 34 additions & 38 deletions lib/addMethod/addMethodFunction.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var _ = require('lodash');
var when = require('when');
var logger = require('../logger');
const _ = require('lodash');
const when = require('when');
const logger = require('../logger');

var globalize = require('./globalize');
const globalize = require('./globalize');

//The format of the response should always be an object with headers and body
var formatResponse = require('./formatResponse');
const formatResponse = require('./formatResponse');

module.exports = function (threadneedle, config, afterHeadersFunction, params) {

var afterHeadersConfig = {};
const afterHeadersConfig = {};
if (_.isFunction(afterHeadersFunction)) {
afterHeadersConfig.afterHeaders = afterHeadersFunction;
} else {
Expand All @@ -18,38 +18,34 @@ module.exports = function (threadneedle, config, afterHeadersFunction, params) {
}
}

return when.promise(function(resolve, reject) {
when(config.call(threadneedle, params))
.done(
function(body) {

globalize.afterHeaders.call(threadneedle, afterHeadersConfig, null, body, params, null)

.done(
function(headers) {
resolve(formatResponse(headers, body));
},
function(afterHeadersError) {
reject(formatResponse({}, afterHeadersError));
}
);

},
function(functionError) {

globalize.afterHeaders.call(threadneedle, afterHeadersConfig, functionError, null, params, null)

.done(
function(headers) {
reject(formatResponse(headers, functionError));
},
function(afterHeadersError) {
reject(formatResponse({}, afterHeadersError || functionError));
}
);

return when.promise((resolve, reject) => {
new Promise((resolve, reject) => {
try {
resolve(config.call(threadneedle, params));
} catch (functionCallError) {
reject(functionCallError);
}
);
})
.then((body) => {
globalize.afterHeaders.call(threadneedle, afterHeadersConfig, null, body, params, null)

.then((headers) => {
resolve(formatResponse(headers, body));
})
.catch((afterHeadersError) => {
reject(formatResponse({}, afterHeadersError));
});
})
.catch((functionError) => {
globalize.afterHeaders.call(threadneedle, afterHeadersConfig, functionError, null, params, null)

.then((headers) => {
reject(formatResponse(headers, functionError));
})
.catch((afterHeadersError) => {
reject(formatResponse({}, afterHeadersError || functionError));
});
});
});

};
};
Loading

0 comments on commit 54382e1

Please sign in to comment.