helidon-io / helidon

Java libraries for writing microservices
https://helidon.io
Apache License 2.0
3.5k stars 566 forks source link

[4.x] Undesirable exception: io.helidon.http.RequestException: No entity #8827

Closed hrstoyanov closed 3 months ago

hrstoyanov commented 4 months ago

This is a continuation from the discussion of #8710

The below code can fail :

ServerRequest req = ...;
var params = eq.content().as(Parameters.class);

io.helidon.http.RequestException: No entity
    at io.helidon.http@4.0.8/io.helidon.http.RequestException$Builder.build(RequestException.java:139)
    at io.helidon.webserver@4.0.8/io.helidon.webserver.http.ErrorHandlers.unhandledError(ErrorHandlers.java:203)
    at io.helidon.webserver@4.0.8/io.helidon.webserver.http.ErrorHandlers.lambda$handleError$1(ErrorHandlers.java:182)

Can we make sure it does not, so devs can write clean functional fluent code? Can we return a Parameters "null object" that will have no params ? See discussion for issue #8710

Environment Details

tomas-langer commented 3 months ago

Would this resolve your issue:

Optional<Parameterse> params = eq.content().asOptional(Parameters.class);

Solve your problem? You could then use functional style to resolve the case when the optional is empty (which would be if there is no entity).

hrstoyanov commented 3 months ago

@tomas-langer - works for me!