2021-02-17 - Middy error handling

From Izara Wiki
Jump to navigation Jump to search

NPM module - izara-middleware

Middy instructions (https://github.com/middyjs/middy) are pretty sparse and unclear, through testing:

Observations

If throw within middlewares

  • the onError functions will be called in the same order as they are attached to Middy, not in reverse order like after functions are
  • if onError is an async function it will be awaited before processing passes to next middleware's onError
  • onError functions will be called for all middlewares, even those that have not yet passed begin function
  • because onError for all middlewares will be called, we cannot assume begin has already executed when processing onError function

async onError

  • do not call next(), it will in most cases result in remaining middleware onError's to be executed twice
  • return from async onError
  • can switch the Lambda to not throwing an error by any async onError function returning a falsy value
  • can stop remaining onError functions from being executed by returning a rejected promise, in an async onError function this might mean throwing an error (not tested)
  • if we want to Lambda to throw an error (or another middleware to choose whether to Lambda throws an error) we should return a truthy value

non-async onError

  • must end function using next(), next(handler.error), or Lambda's callback function
  • if return falsy seems to timeout, truthy (not sure) might throw error, but is not correct code
  • next() is the same as 'handling' the error, Lambda will not throw an error, all remaining onError's will execute, the Lambda response and error objects are null (nothing returned to calling resource)
  • next(handler.error) passes processing on to next onError, if all onError's pass on like this the Lambda will throw an error after all onError's executed
  • if an onError middleware invokes Lambda's callback we can set the response and error objects that the Lambda returns. remaining onError functions will be processed
  • the only was to return a response from the onError flow is to use the Lambda's callback