perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.64k stars 1.56k forks source link

Halt response transformer #955

Closed saantiaguilera closed 5 years ago

saantiaguilera commented 6 years ago

Currently if we halt with a custom body we dont have any way to transform the response unless calling explicitly a transformation:

We have to do Spark.halt(HttpStatus.SC_BAD_REQUEST, responseTransformer.transform(message)); on each halt call

Could we have a feature like:

Spark.onErrorApply(responseTransformer); // Or a better name for the method

that applies to all exception messages?

Or at least be able to subscribe to events and tune it for all of the halts, like we can do with 404 or 500 or any particular exception except HaltException:

Spark.notFound(...); // Gets called
Spark.internalServerError(...); // Gets called
Spark.exception(HaltException.class, (ex, req, res) -> { // Wont get called
  res.status(ex.statusCode());
  res.body(responseTransformer.transform(ex.body()));
});