Class HttpStatusException

All Implemented Interfaces:
Serializable

public final class HttpStatusException extends RuntimeException
A RuntimeException that is raised to send a simplistic HTTP response with minimal content by a Service. It is a general exception raised by a failed request or a reset stream.

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


 // Catch an HttpStatusException and convert into an HttpResponse
 try {
     throwableService();
 } catch (HttpStatusException ex) {
     return HttpResponse.of(ex.httpStatus());
 }

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

An unhandled HttpStatusException will be recovered by the default ServerErrorHandler in the end. If you want to mutate the HttpResponse made of the HttpStatusException, you can add a custom ServerErrorHandler.

See Also: