Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No exception logged when JSON request conversion fails in an annotated service #2041

Closed
trustin opened this issue Sep 4, 2019 · 6 comments · Fixed by #2131
Closed

No exception logged when JSON request conversion fails in an annotated service #2041

trustin opened this issue Sep 4, 2019 · 6 comments · Fixed by #2131
Labels
Milestone

Comments

@trustin
Copy link
Collaborator

trustin commented Sep 4, 2019

For example:

public class MyService {
    @Post("/post")
    void post(MyValue value) {
        ...
    }
}

public class MyValue {
    final String value;
    public MyValue(String value) {
        this.value = value;
    }
}   

When an HTTP client posts a JSON document:

curl -X POST \
  http://localhost:8080/post \
  -H 'Content-Type: application/json' \
  -H 'Host: localhost:8080' \
  -d '{ "value": "foo" }'

.. a 400 Bad Request response is sent by the server, but no log message is written, making it hard to know what is wrong. The problem goes away if a user annotates MyValue.<init> with @JsonCreator and the parameter value with @JsonProperty("value").

@trustin trustin added the defect label Sep 4, 2019
@trustin trustin added this to the 0.91.0 milestone Sep 4, 2019
@minwoox minwoox modified the milestones: 0.91.0, 0.92.0 Sep 6, 2019
@minwoox minwoox modified the milestones: 0.92.0, 0.93.0 Sep 20, 2019
@trustin trustin modified the milestones: 0.93.0, 0.94.0 Sep 25, 2019
@sivaalli
Copy link
Contributor

Is it possible for me to take a look at this and possibly fix it by adding a log statement?

@trustin
Copy link
Collaborator Author

trustin commented Sep 27, 2019

@sivaalli Sure! Thanks a lot for looking into it. 🙇

@sivaalli
Copy link
Contributor

sivaalli commented Sep 28, 2019

Hi @trustin , while making a change I got a doubt and wanted to get it clarified. If a ExceptionHandlerFunction is declared and the conversion fails(at runtime), then the function will be able to catch exception and handle it. If we log it in JacksonRequestConverterFunction and the ExceptionHandlerFunction that the user provided also logs it, then log appears twice. Can you please let me know what do you think.

public class Armeria {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Server build = new ServerBuilder()
                .port(8080, SessionProtocol.HTTP)
                .annotatedService("/", new MyService()).build();
        build.start().get();

    }

    private static class MyService {

        @ExceptionHandler(LoggingExceptionHandlerFunction.class)
        @Post("/a")
        public HttpResponse a(MyValue value){
            System.out.println(value.value);
            return HttpResponse.of(200);
        }
    }

    public static class MyValue{
        @JsonProperty("value")
        public String value;
    }

    public static class LoggingExceptionHandlerFunction implements ExceptionHandlerFunction{

        @Override
        public HttpResponse handleException(ServiceRequestContext ctx, HttpRequest req, Throwable cause) {
            System.out.println(cause);
            return HttpResponse.of(400);
        }
    }
}

@trustin
Copy link
Collaborator Author

trustin commented Sep 30, 2019

That is an excellent question, @sivaalli. IIRC, we already have a fallback exception handler that performs some logging: com.linecorp.armeria.internal.annotation.DefaultExceptionHandler.

Because JacksonRequestConverterFunction wraps the Jackson exception with an IllegalArgumentException, it is automatically and silently translated into 400 Bad Request by DefaultExceptionHandler.

What we could do in my opinion is to make JacksonRequestConverterFunction translate the Jackson exception into an IllegalArgumentException only for JSON parse errors. What do you think?

@sivaalli
Copy link
Contributor

Thank you @trustin for your reply. Your plan sounds good to me. Currently JacksonRequestConverterFunction catches JsonProcessingException(a generic Jackson exception) and re-throws IllegalArgumentException. I will see if I can target subtype of JsonProcessingException that is thrown for JSON parse errors.

And since we already have aDefaultExceptionHandler, I will also add a log statement too in there. So that we know why JSON parse error happened(solves the original intent of this issue) and when user provides his/her implementation, then it is given priority over default implementation and will let user handle exception and(or) log it(also solves logging exception twice). Do you think this is a good idea?

@trustin
Copy link
Collaborator Author

trustin commented Oct 1, 2019

Yeah, I think it's good. If other users report about the increased warning logs, we could reconsider it.

trustin pushed a commit that referenced this issue Oct 2, 2019
Motivation:

When a JSON document cannot be converted into Java object, an `IllegalArgumentException` is thrown. And this is converted to 400 status code. But it would be nice to have a log statement in `DefaultExceptionHandler` to log what is wrong with the request

Closes #2041
eugene70 pushed a commit to eugene70/armeria that referenced this issue Oct 16, 2019
…e#2131)

Motivation:

When a JSON document cannot be converted into Java object, an `IllegalArgumentException` is thrown. And this is converted to 400 status code. But it would be nice to have a log statement in `DefaultExceptionHandler` to log what is wrong with the request

Closes line#2041
fmguerreiro pushed a commit to fmguerreiro/armeria that referenced this issue Sep 19, 2020
…e#2131)

Motivation:

When a JSON document cannot be converted into Java object, an `IllegalArgumentException` is thrown. And this is converted to 400 status code. But it would be nice to have a log statement in `DefaultExceptionHandler` to log what is wrong with the request

Closes line#2041
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants