Package com.bobocode.bring.web.servlet
Class JsonExceptionHandler
java.lang.Object
org.apache.catalina.util.LifecycleBase
org.apache.catalina.util.LifecycleMBeanBase
org.apache.catalina.valves.ValveBase
org.apache.catalina.valves.ErrorReportValve
com.bobocode.bring.web.servlet.JsonExceptionHandler
- All Implemented Interfaces:
MBeanRegistration,org.apache.catalina.Contained,org.apache.catalina.JmxEnabled,org.apache.catalina.Lifecycle,org.apache.catalina.Valve
public class JsonExceptionHandler
extends org.apache.catalina.valves.ErrorReportValve
Component for handling exceptions and generating JSON error responses. Extends
ErrorReportValve to intercept
and customize error handling in a Servlet container.- Since:
- 1.0
- Author:
- Blyzhnytsia Team
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.catalina.Lifecycle
org.apache.catalina.Lifecycle.SingleUse -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ErrorResponseCreatorprivate final com.fasterxml.jackson.databind.ObjectMapperprivate final ServerPropertiesFields inherited from class org.apache.catalina.valves.ValveBase
asyncSupported, container, containerLog, next, smFields inherited from interface org.apache.catalina.Lifecycle
AFTER_DESTROY_EVENT, AFTER_INIT_EVENT, AFTER_START_EVENT, AFTER_STOP_EVENT, BEFORE_DESTROY_EVENT, BEFORE_INIT_EVENT, BEFORE_START_EVENT, BEFORE_STOP_EVENT, CONFIGURE_START_EVENT, CONFIGURE_STOP_EVENT, PERIODIC_EVENT, START_EVENT, STOP_EVENT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate ThrowableGets the root cause of the given throwable.private ErrorResponseprepareBody(Throwable throwable) Prepares the JSON body of the error response based on the thrown exception.protected voidreport(org.apache.catalina.connector.Request request, org.apache.catalina.connector.Response response, Throwable throwable) Overrides the default error reporting method to intercept and customize error handling.private voidsetErrorResponse(org.apache.catalina.connector.Response response, Throwable throwable) Sets the JSON error response in the servlet response.private static voidsetHeaders(org.apache.catalina.connector.Response response, ErrorResponse errorResponse) Sets the headers in the servlet response for the JSON error response.Methods inherited from class org.apache.catalina.valves.ErrorReportValve
findErrorPage, getPartialServletStackTrace, getProperty, invoke, isShowReport, isShowServerInfo, setProperty, setShowReport, setShowServerInfoMethods inherited from class org.apache.catalina.valves.ValveBase
backgroundProcess, getContainer, getDomainInternal, getNext, getObjectNameKeyProperties, initInternal, isAsyncSupported, setAsyncSupported, setContainer, setNext, startInternal, stopInternal, toStringMethods inherited from class org.apache.catalina.util.LifecycleMBeanBase
destroyInternal, getDomain, getObjectName, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister, unregisterMethods inherited from class org.apache.catalina.util.LifecycleBase
addLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, getThrowOnFailure, init, removeLifecycleListener, setState, setState, setThrowOnFailure, start, stop
-
Field Details
-
objectMapper
private final com.fasterxml.jackson.databind.ObjectMapper objectMapper -
errorResponseCreator
-
serverProperties
-
-
Constructor Details
-
JsonExceptionHandler
public JsonExceptionHandler()
-
-
Method Details
-
report
protected void report(org.apache.catalina.connector.Request request, org.apache.catalina.connector.Response response, Throwable throwable) Overrides the default error reporting method to intercept and customize error handling.- Overrides:
reportin classorg.apache.catalina.valves.ErrorReportValve- Parameters:
request- The incoming servlet request.response- The outgoing servlet response.throwable- The thrown exception to be handled.
-
setErrorResponse
Sets the JSON error response in the servlet response.- Parameters:
response- The outgoing servlet response.throwable- The thrown exception.
-
setHeaders
private static void setHeaders(org.apache.catalina.connector.Response response, ErrorResponse errorResponse) Sets the headers in the servlet response for the JSON error response.- Parameters:
response- The outgoing servlet response.errorResponse- The JSON error response.
-
prepareBody
Prepares the JSON body of the error response based on the thrown exception.- Parameters:
throwable- The thrown exception.- Returns:
- The prepared JSON error response.
-
getCause
Gets the root cause of the given throwable.- Parameters:
throwable- The thrown exception.- Returns:
- The root cause of the exception.
-