onerror_request and onerror_response hooks in custom plugins to handle errors

Hello All,

I'm trying to figure it out when to use "onerror_request" and "onerror_response" hooks in a custom plugin. In documentation there are few information on that matter.

I wrote some sample code:

function _handleErrors (req, res, next) {
  _logger.info('Plugin invoked')
  throw new Error('Some random error just for sake of testing...')
}

module.exports.init = function (config, logger, stats) {
  
  return {
    onrequest: function (req, res, next) {      
      try {
        _handleErrors(req, res, next)
        _logger.info('after executing the function')
      } catch (error) {
        _logger.error('Catching errors: ' + error)
        next(error)
      }
    },

    onerror_request: function(req, res, err, next) {
      _logger.error('plugin onerror_request err  ' + (err ? Object.keys(err) : err));
      next();
    },

    onerror_response: function(req, res, err, next) {
      _logger.error('plugin onerror_response err  ' + (err ? Object.keys(err) : err));
      next();
    }
  }
}

This code does not trigger the onerror hooks, it will go to catch clause and invoke next(error). If an error is thrown inside the plug-in code, will onerror hooks never be invoked?

The only situation where i was able to see the onerror_request be triggered is when I make the target endpoint unresponsive. But in this case, the err parameter comes as undefined. Why is this happen? When the err parameter will have values?

Overall, Can anyone explain to me in which situations to use these hooks?

Thanks

Ciro


0 9 414
9 REPLIES 9

The onerror_request hook is executed when the request going to the backend emits a nodejs error event and onerror_response is executed when the response coming from the backend emits an error event.

Not receiving the error in the hook looks to be a bug. I'll investigate.

So, from what you said, if some error is thrown in any of these hooks:

onrequest, ondata_request, onend_request, onclose_request, this will trigger the onrequest_error and if any of these hooks thrown an error:

onresponse, ondata_response, onend_response, onclose_response this will trigger the onresponse_error. Is that it? So, in my code above, the onrequest_error should have been triggered?

Thanks

No. Nodejs has a native http request object. That object will emit an error event if something happens over the course of an http transaction. The plugins error handling is separate.

Errors in plugins are passed as the first argument to the callback. You should see an error message in the http response from microgateway. In general if the plugin throws an error it should be handled inside the plugin if possible. If it cannot then it should be passed to the next and sent back to the user.

Got it! To sum up, these error hooks are triggered when request and response native objects emit errors in a general context for edgemicro, these are not for errors thrown inside plug-ins. For plug-ins, all errors must be handle inside the plug-in itself, which means sending a next(error).

Can I conclude this?

Thanks

Yep! That looks like a sound conclusion.

-Matt

Okay!! Thanks for answering!

hey, one more question.

In the context of my conclusion, if several plugins provide error handlers each, which error handlers are executed, under which condition & in which order (if several are executed)?

Tks

They should be executed like other plugin handlers. In order of declaration in your configuration sequence for request handlers, and reverse for response handlers.

Thanks @Matthew Dobson ,so i believe all plugins should handle errors, when an error happened in a specific one in the chain of plugins, because of the next(error), it will stop the execution and send an error to the user...right?