Class ErrorResponseCreator

java.lang.Object
com.bobocode.bring.web.servlet.error.ErrorResponseCreator

public class ErrorResponseCreator extends Object
The ErrorResponseCreator class is responsible for creating instances of ErrorResponse. It provides methods to prepare error responses with various configurations.
Since:
1.0
Author:
Blyzhnytsia Team
  • Constructor Details

    • ErrorResponseCreator

      public ErrorResponseCreator()
  • Method Details

    • prepareErrorResponse

      public ErrorResponse prepareErrorResponse(ResponseStatus responseStatus, Throwable throwable, Boolean withStackTrace)
      Prepares a customized error response based on the provided parameters.
      Parameters:
      responseStatus - The response status to be included in the error response.
      throwable - The Throwable object associated with the error, providing additional details.
      withStackTrace - A flag indicating whether to include the stack trace in the error response.
      Returns:
      An ErrorResponse object containing the prepared error details.
    • defaultErrorResponse

      public ErrorResponse defaultErrorResponse(Throwable e, Boolean withStackTrace)
      Prepares a default error response for internal server errors.
      Parameters:
      e - The Throwable object representing the internal server error.
      withStackTrace - A flag indicating whether to include the stack trace in the error response.
      Returns:
      An ErrorResponse object containing the default internal server error details.
    • getStackTrace

      private String getStackTrace(Throwable throwable, Boolean withStackTrace)
      Returns the stack trace as a string based on the specified flag.
      Parameters:
      throwable - The Throwable object from which the stack trace is extracted.
      withStackTrace - A flag indicating whether to include the stack trace.
      Returns:
      The stack trace as a string if withStackTrace is true, otherwise null.
    • prepareStackTrace

      private String prepareStackTrace(Throwable throwable)
      Prepares the stack trace as a string.
      Parameters:
      throwable - The Throwable object from which the stack trace is extracted.
      Returns:
      The stack trace as a string.
    • getMessage

      private String getMessage(ResponseStatus responseStatus, Throwable throwable)
      Retrieves the error message based on the provided ResponseStatus and Throwable.
      Parameters:
      responseStatus - The ResponseStatus object representing the error status.
      throwable - The Throwable object associated with the error.
      Returns:
      The error message, either from ResponseStatus or Throwable.