Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending a middy middleware function in nodejs

In my lambda code written in nodejs, I want to extend the httpErrorhandler middleware and create a wrapper around it.

Currently I have something like below.

module.exports.handler=middy(handle).use(httpErrorHandler(LoggingFactory.getLogger().log))

I want to create a customHttpErrorHandler which is nothing but just a wrapper outside httpErrorHandler.

module.exports.handler=middy(handle).use(customHttpErrorHandler(LoggingFactory.getLogger().log))

Is this possible? What do I need to put inside customHttpErrorHandler ? There is no additional functionality to be implemented. This new customHandler should just pass the control to standard httpErrorHandler.

customHttpErrorHandler might look something like below (pseudo code).

connectHttpErrorHandler = (logger) => httpErrorHandler(logger)
like image 222
Naxi Avatar asked Feb 18 '26 07:02

Naxi


1 Answers

Hi core maintainer of Middy here. Your pseudo code is almost correct. In Middy >=2.0.0, httpErrorHandler takes an options object. See https://github.com/middyjs/middy/tree/main/packages/http-error-handler for documentation.

const connectHttpErrorHandler = (logger) => httpErrorHandler({logger})

const logger = LoggingFactory.getLogger().log

module.exports.handler = middy(handler)
  .use(customHttpErrorHandler(logger))
like image 76
will Farrell Avatar answered Feb 20 '26 03:02

will Farrell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!