Idempotency and Error Handling #1566
Closed
brianhyder
started this conversation in
Show and tell
Replies: 1 comment
-
Thank you for taking the time to write your experience with the utility and share it with the rest of the community. With your permission we'll reuse/reword part of your solution for a new section dedicated to Idempotency that we will add to the Middy docs. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey, all.
As of the 1.10 build, all of our idempotency use cases are passing. A huge THANK YOU for getting this functionality squared away.
For this discussion, I wanted to walk through a couple of items to get your feedback. We had 2 use cases that the out-of-the-box
makeHandlerIdempotent
function did not cover:Our error handling philosophy is to create custom errors for different situations: 400 validation, 500 server/unhandled, etc. Those errors have properties attached to them so that our generic error handling middleware can properly pick them up and format them.
To satisfy requirement 2, we wrapped the
makeHandlerIdempotent
and theonError
portion of the middleware. The default behavior is to delete any idempotent record on error. To ensure that the error response gets persisted, we needed the same behavior as if there was a successful response.To satisfy requirement 1, we needed to capture the specific error that was thrown. You all made that easy as you created a custom error that is thrown in this situation. We simply added code to the
onError
handler we used to override the default behavior. This was the only case where we did not want to persist the outcome as it would override the values of the first request that was used with that idempotent key.To satisfy requirement 3, we simply added the additional metadata we needed so that our generic error handler would set the proper response. This also meant that we needed to inject our generic error handler so that the error could be handled. As outlined below, the positioning of the error handlers in the middleware chain matters.
Putting it all together our implementation looks a bit like this:
lambda handler initialization:
Wrapped
makeHandlerIdempotent
function:It is important to note that the position of the error handler matters.
To mitigate each of these cases, we intentionally did 3 things:
makeHandlerIdempotent
function as configuration so that it could handle the case where a subsequent request with a different body errors with a properly formatted response.Anyway, feel free to send any feedback our way. Thank you for getting this built and ready. We'll be on the lookout for the first official release.
Beta Was this translation helpful? Give feedback.
All reactions