juanjoDiaz/serverless-middleware

Get underlying exception object on pos-catch middleware

Closed this issue · 2 comments

Given a serverless configuration like:

provider:
  name: aws
  runtime: nodejs18.x
  stage: ${opt:stage, 'local'}
  region: us-east-1
middleware:
   pos:
     - catch: handlers/middleware/error.handler
plugins:
  - serverless-middleware 

How can I get the error object thrown by the caller handler?

Looking at the generated code in .middleware folder I see this:

return Promise.resolve().then(wrappedHandler(handler.bind(auth_exports))).then(wrappedHandler(handler2.bind(profile_id_exports))).then(wrappedHandler(handler3.bind(list_exports))).catch(wrappedHandler(handler4.bind(error_exports)));

The .catch method is there but I am not sure how to get hold of the actual error object in the error handler function.

Hi @obaqueiro ,

That middleware block should be inside a custom block:

provider:
   name: aws
   runtime: nodejs18.x
   stage: ${opt:stage, 'local'}
   region: us-east-1

custom:
   middleware:
      pos:
         - catch: handlers/middleware/error.handler

plugins:
   - serverless-middleware 

Although that's not really the problem since you are getting the middleware correctly chained in the final handler.

To get the result from a previous middleware (either successful result or error), you can access context.prev in the middleware.

Closing since there was no response.