Skip to content

Error Handling

GoPotency allows you to fully customize the error responses returned when something goes wrong (e.g., a missing key, a request mismatch, or a storage error).

By default, GoPotency returns plain text error messages. You can change this behavior by providing an ErrorHandler in the idempotency.Config struct.

This is useful for returning consistent JSON error objects across your API.

manager, err := idempotency.NewManager(idempotency.Config{
Storage: store,
ErrorHandler: func(err error) (int, any) {
// Return a 400 Bad Request with a JSON body
return http.StatusBadRequest, map[string]string{
"error": "idempotency_error",
"message": err.Error(),
}
},
})

The ErrorHandler is a function with the following signature:

func(err error) (statusCode int, body any)
  • statusCode: The HTTP status code that will be sent to the client.
  • body: The data that will be serialized as the response body. If you are using Gin, this can be a gin.H or a struct. If using standard HTTP, it will be marshaled to JSON.