Class HttpResponseException

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
com.linecorp.armeria.server.HttpResponseException
All Implemented Interfaces:
Serializable

public final class HttpResponseException extends RuntimeException
A RuntimeException that is raised to send an HTTP response with the content specified by a user. This class holds an HttpResponse which would be sent back to the client who sent the corresponding request.

Note that a raised HttpResponseException may not be applied to the next decorators if the HttpResponseException is not recovered before being passed to the next decorator chain. For this reason, the thrown HttpResponseException should be converted into a normal HttpResponse using HttpResponse.recover(Function) or httpResponse(). For example:


 // Catch the HttpResponseException and convert into an HttpResponse
 try {
     throwableService();
 } catch (HttpResponseException ex) {
     return ex.httpResponse();
 }

 // Recover the HttpResponseException using HttpResponse.recover()
 HttpResponse response = ...;
 response.recover(ex -> {
     if (ex instanceof HttpResponseException) {
         return ((HttpResponseException) ex).httpResponse();
     } else {
         return HttpResponse.ofFailure(ex);
     }
 })
 

An unhandled HttpResponseException is recovered by the default ServerErrorHandler in the end. If you want to mutate the HttpResponse in the HttpResponseException, you can add a custom ServerErrorHandler.

See Also: