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.63k stars 1.56k forks source link

Intercept Bad Requests #1112

Open aguglie opened 5 years ago

aguglie commented 5 years ago

Hi, I'm trying to intercept and log malformed requests like:

curl 'http://127.0.0.1:4567/servle t_withspace'
<h1>Bad Message 400</h1><pre>reason: Illegal character SPACE=' '</pre>%   

But I cannot find any why to log this event; I've already tried with after and exception without any result:

       after((request, response) -> {
            if (response.status() != 200) {
                logger.error("Response is not 200.\n" +
                        "Request: " + requestToString(request) + "\n" +
                        "Response:" + responseToString(response));
            }
        });

        exception(Exception.class, (exception, request, response) -> {
            logger.error("Exception: " +
                    "Request: " + requestToString(request) + "\n" +
                    "Response:" + responseToString(response));
        });

I'm using sparkJava 2.9.0