Skip to content

Commit

Permalink
159571119 Fix the onerror req/res event issuse for all plugins
Browse files Browse the repository at this point in the history
 Pass 504 or 502 status code to the onrequest_error event of the plugin based on the targetRequest state
 Pass proper error message to the same event for above codes using options.error object key
  • Loading branch information
niheelthakkar89 authored and keyurkarnik committed Nov 12, 2020
1 parent 16fa601 commit f9bd18d
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/plugins-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ module.exports = function(pluginsSeqManager) {
* @private
*/
function getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, correlation_id, cb) {
const gatewayTimeOutErrCode = 504;
const gatewayBadErrCode = 502;
const logger = logging.getLogger();
const config = configService.get();

Expand Down Expand Up @@ -291,6 +293,12 @@ function getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, cor

targetRequest.on('error', function(err) {

if (targetRequest._timedOut) {
sourceResponse.statusCode = gatewayTimeOutErrCode; // Gateway Time-out
} else {
sourceResponse.statusCode = gatewayBadErrCode; // Bad Gateway
}

logger.eventLog({level:'warn',
d: Date.now() - startTime,
err: err,
Expand All @@ -302,7 +310,8 @@ function getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, cor
async.series(getPluginHooksForEvent('error', {
plugins: plugins,
sourceRequest: sourceRequest,
sourceResponse: sourceResponse
sourceResponse: sourceResponse,
error: err
}),
function(e) {
if (e) {
Expand All @@ -313,15 +322,13 @@ function getTargetRequest(sourceRequest, sourceResponse, plugins, startTime, cor
cb(e);
} else {
if (targetRequest._timedOut) {
sourceResponse.statusCode = 504; // Gateway Time-out
//opentracing
traceHelper.setResponseError('','','', 504);
traceHelper.setResponseError('','','', gatewayTimeOutErrCode);
//
cb(new Error("Gateway timed out trying to reach target"));
} else {
sourceResponse.statusCode = 502; // Bad Gateway
//opentracing
traceHelper.setResponseError('','','', 502);
traceHelper.setResponseError('','','', gatewayBadErrCode);
//
cb(err);
}
Expand Down Expand Up @@ -385,8 +392,13 @@ function handleTargetResponse(targetRequest, targetResponse, options, cb) {
targetResponse: targetResponse
}),
function(err, results) {
let logLevel = 'info';
// Make log level Configurable for target error non 2xx
if (config.edgemicro.logTargetErrorsAs && targetResponse.statusCode && targetResponse.statusCode / 100 !== 2) {
logLevel = config.edgemicro.logTargetErrorsAs;
}
logger.eventLog({
level:'info',
level: logLevel,
res: targetResponse,
h: sourceRequest.transactionContextData.targetHostName,
d: Date.now() - ( sourceRequest.headers['target_sent_start_timestamp'] || start ),
Expand Down Expand Up @@ -609,7 +621,8 @@ function _subscribeToResponseEvents(plugins, sourceRequest, sourceResponse, targ
sourceRequest: sourceRequest,
sourceResponse: sourceResponse,
targetRequest: targetRequest,
targetResponse: targetResponse
targetResponse: targetResponse,
error:err
}),
function(err) {
cb(err)
Expand Down Expand Up @@ -648,6 +661,11 @@ function getPluginHookForEvent(plugin, type, options) {
var pluginHandler = plugin['on' + handler];
var argsLength = pluginHandler.length;
var args = null;

//introduced options.error for specifying the error(if found and declared) on all error events
if(options.error){
data = options.error;
}
if (argsLength === 3) {
args = [options.sourceRequest, options.sourceResponse, fx];
} else if (argsLength === 4) {
Expand Down

0 comments on commit f9bd18d

Please sign in to comment.