0

I am learning to use express for node.js backend development.

I have a task to implement a retry mechanism using middleware, such that if a certain error occurs in previous middleware it will reprocess the entire request, and such that retrying will not run more than a maximum number of times. Something like so:

router.patch(some_uri,
             preprocessing,
             mainHandler,
             retryOnError({maxCount: 3, interval: 2})
)
// retry.ts

export function retryOnError({maxCount, interval}) {
     return (err, req, res, next) => {
          // On specific error send request back to preprocessing
     }
}

where maxCount is the maximum times to retry interval is an interval to wait between each retry.

I am not sure what I would actually need to put on retryOnError, though

My first idea was to post the request back with response.redirect('back'). However, in this case I have no way to limit the count of retries, since no extra information is sent back.

How can I implement this?

Note that the retry must be implemented as an independent middleware.

  • 1
    The first problem with your initial code block is that if `mainHandler` sends an error response, then `retryOnError` will probably never get called. A solution needs to be enabled INSIDE of `mainHandler` first. – jfriend00 Nov 09 '22 at 18:02

0 Answers0