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

    Fields
    Modifier and Type
    Field
    Description
    private final ErrorResponseCreator
     
    private final com.fasterxml.jackson.databind.ObjectMapper
     
    private final ServerProperties
     

    Fields inherited from class org.apache.catalina.valves.ValveBase

    asyncSupported, container, containerLog, next, sm

    Fields 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private Throwable
    getCause(Throwable throwable)
    Gets the root cause of the given throwable.
    Prepares the JSON body of the error response based on the thrown exception.
    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.
    private void
    setErrorResponse(org.apache.catalina.connector.Response response, Throwable throwable)
    Sets the JSON error response in the servlet response.
    private static void
    setHeaders(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, setShowServerInfo

    Methods inherited from class org.apache.catalina.valves.ValveBase

    backgroundProcess, getContainer, getDomainInternal, getNext, getObjectNameKeyProperties, initInternal, isAsyncSupported, setAsyncSupported, setContainer, setNext, startInternal, stopInternal, toString

    Methods inherited from class org.apache.catalina.util.LifecycleMBeanBase

    destroyInternal, getDomain, getObjectName, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister, unregister

    Methods inherited from class org.apache.catalina.util.LifecycleBase

    addLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, getThrowOnFailure, init, removeLifecycleListener, setState, setState, setThrowOnFailure, start, stop

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • objectMapper

      private final com.fasterxml.jackson.databind.ObjectMapper objectMapper
    • errorResponseCreator

      private final ErrorResponseCreator errorResponseCreator
    • serverProperties

      private final ServerProperties 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:
      report in class org.apache.catalina.valves.ErrorReportValve
      Parameters:
      request - The incoming servlet request.
      response - The outgoing servlet response.
      throwable - The thrown exception to be handled.
    • setErrorResponse

      private void setErrorResponse(org.apache.catalina.connector.Response response, Throwable throwable)
      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

      private ErrorResponse prepareBody(Throwable throwable)
      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

      private Throwable getCause(Throwable throwable)
      Gets the root cause of the given throwable.
      Parameters:
      throwable - The thrown exception.
      Returns:
      The root cause of the exception.