Fork me on GitHub
#reitit
<
2021-12-01
>
Yevgeni Tsodikov12:12:57

Hey, Is there a way to customize how request coercion errors are returned in the response? My use case: I want to return only the :humanized field, but I’d like to rename it to :errors . I know I can use :encode-error and set it to something like:

(fn [validation-body] {:errors (:humanized validation-body)})
But it’s a waste to create the original errors map just create a new one instead of it. Ideas?

catjam 1
Yevgeni Tsodikov12:12:33

Another question, similar to the above: Is it possible to return an empty response payload in case of request coercion errors? My use case: in some routes in prod, I want to hide the validation details, essentially retuning {:status 400}

ikitommi19:12:44

you can override the coercion errors using the error-middleware options. Don't have an example at hand, docs should cover how to. In not, doc-PR welcome.

Yevgeni Tsodikov19:12:17

What I’m currently doing is adding a field :report-request-coercion-errors? to the routes. In the :reitit.coercion/request-coercion field of the exception middleware, I’m checking if (-> request [:reitit.core/match :data :report-request-coercion-errors?]) and returning either (constantly {:status 400) or the original (reitit.ring.middleware.exception/create-coercion-handler 400) . The downside is that I have to work with :inject-match true for this work, right?

ikitommi20:12:11

you could just define the global error handler for coercion there