Package net.morimekta.providence.server
Class ExceptionHandler
- java.lang.Object
-
- net.morimekta.providence.server.ExceptionHandler
-
public class ExceptionHandler extends java.lang.Object
Handle exceptions for HTTP responses. Note that using this instead of a proper service handler will easily hide exceptions, or transform unknown exceptions into known exceptions outside of service implementations. But it provides a handy way of normalizing non-default responses onto HTTP status and message format.Overridable Methods
-
: Complete handling of exceptions thrown by thehandleException(Throwable, Serializer, HttpServletRequest, HttpServletResponse)
PServiceCallHandler.handleCall(PServiceCall, PService)
method or similar. Calling super on this will fall back to default exception handling, using the methods below. This will per default serialize PMessage exceptions normally, and just callHttpServletResponse.sendError(int,String)
for all the others using theThrowable.getMessage()
message. -
: Get the response exception given the specific thrown exception. This method can be used to unwrap wrapped exceptions, or transform non- message exceptions into providence message exceptions.getResponseException(Throwable)
-
: Get the HTTP status code to be used for the error response. Override to specialize, and call super to get default behavior. The default will handlestatusCodeForException(Throwable)
PApplicationException
errors, and otherwise return500 Internal Server Error
.
-
-
-
Field Summary
Fields Modifier and Type Field Description static ExceptionHandler
INSTANCE
-
Constructor Summary
Constructors Constructor Description ExceptionHandler()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.Throwable
getResponseException(java.lang.Throwable e)
Get the exception to ge handled on failed requests.void
handleException(java.lang.Throwable rex, net.morimekta.providence.serializer.Serializer responseSerializer, javax.servlet.http.HttpServletRequest httpRequest, javax.servlet.http.HttpServletResponse httpResponse)
Handle exceptions from the handle method.protected int
statusCodeForException(java.lang.Throwable exception)
With default exception handling, this can simply change the status code used for the response.
-
-
-
Field Detail
-
INSTANCE
public static final ExceptionHandler INSTANCE
-
-
Method Detail
-
handleException
public void handleException(@Nonnull java.lang.Throwable rex, @Nonnull net.morimekta.providence.serializer.Serializer responseSerializer, @Nonnull javax.servlet.http.HttpServletRequest httpRequest, @Nonnull javax.servlet.http.HttpServletResponse httpResponse) throws java.io.IOException
Handle exceptions from the handle method.- Parameters:
rex
- The response exception, which is the thrown exception or one of it's causes. SeegetResponseException(Throwable)
.responseSerializer
- The serializer to use to serialize message output.httpRequest
- The HTTP request.httpResponse
- The HTTP response.- Throws:
java.io.IOException
- If writing the response failed.
-
getResponseException
@Nonnull protected java.lang.Throwable getResponseException(java.lang.Throwable e)
Get the exception to ge handled on failed requests.- Parameters:
e
- The exception seen.- Returns:
- The exception to use as response base.
-
statusCodeForException
protected int statusCodeForException(@Nonnull java.lang.Throwable exception)
With default exception handling, this can simply change the status code used for the response.- Parameters:
exception
- The exception seen.- Returns:
- The status code to be used.
-
-