Extending a middy middleware function in nodejs

0 votes

I'd like to enhance the httpErrorhandler middleware and create a wrapper around it in my nodejs lambda code.

Currently, I have something similar to this.

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

I'd like to make a customHttpErrorHandler that acts as a wrapper around httpErrorHandler.

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

Is that even possible? What should I provide in customHttpErrorHandler? There isn't any new functionality that needs to be added. The control should be passed to the normal httpErrorHandler via this new customHandler.

Below is an example of a customHttpErrorHandler (pseudo code).

connectHttpErrorHandler = (logger) => httpErrorHandler(logger)
May 30, 2022 in Node-js by Neha
• 9,020 points
1,060 views

1 answer to this question.

0 votes

Your pseudocode comes close to being correct. httpErrorHandler takes an options object in Middy >=2.0.0. Documentation can be found at https://github.com/middyjs/middy/tree/main/packages/http-error-handler.

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

const logger = LoggingFactory.getLogger().log

module.exports.handler = middy(handler)
  .use(customHttpErrorHandler(logger))
answered May 30, 2022 by Vaani
• 7,070 points

Related Questions In Node-js

0 votes
2 answers

Error:'TypeError: is not a function' in Node.js

You are exporting module.exports.redir = redir; That means that ...READ MORE

answered Oct 21, 2020 in Node-js by anonymous
• 140 points
17,288 views
0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,840 points
9,085 views
0 votes
1 answer

How to split and modify a string in NodeJS?

Hello @kartik, Use split and map function: var str = "123, 124, 234,252"; var ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,840 points
1,197 views
0 votes
1 answer

How to check if a collection exists in Mongodb native nodejs driver?

Hello @kartik, The collectionNames method of the native driver's Db object accepts ...READ MORE

answered Nov 27, 2020 in Node-js by Niroj
• 82,840 points
14,252 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,927 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,455 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,434 views
0 votes
1 answer

Installing angularjs with NPM and NODEJS brings an error in ubuntu 16.04

Rename your angular folder to angular-test or ...READ MORE

answered May 30, 2022 in Node-js by Vaani
• 7,070 points
1,312 views
0 votes
1 answer

nodejs (twit) how do you follow a users LIVE twitter stream?

In the following argument, you must use ...READ MORE

answered May 30, 2022 in Node-js by Vaani
• 7,070 points
955 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP