Class ApiError

java.lang.Object
com.broadleafcommerce.common.error.ApiError
Direct Known Subclasses:
ApiValidationError

public class ApiError extends Object
Response object that represents some error state from the API. Intended use is to wrap this within a ResponseEntity to return from an ExceptionHandler.
Author:
Phillip Verheyden (phillipuniverse)
  • Constructor Details

    • ApiError

      public ApiError(String type, String title, @NonNull org.springframework.http.HttpStatusCode status)
      Initializes a new error
      Parameters:
      type - a machine-readable representation of this error
      title - a human-readable representation of the error
      status - HTTP status
    • ApiError

      public ApiError(String type, String title, @NonNull org.springframework.http.HttpStatus status)
    • ApiError

      public ApiError(String type, String title, String exception, OffsetDateTime timestamp, org.springframework.http.HttpStatusCode status, int statusCode, List<GlobalError> globalErrors)
  • Method Details

    • toResponseEntity

      public org.springframework.http.ResponseEntity<ApiError> toResponseEntity()
    • addError

      public void addError(GlobalError error)
      Gives more information to a client about what went wrong in their request
      Parameters:
      error - the error to add to this response
    • getStatus

      public org.springframework.http.HttpStatus getStatus()
    • getType

      public String getType()
      A machine-readable representation of the type of error being returned
    • getTitle

      public String getTitle()
      A human-readable representation of the type of error being returned
    • getException

      public String getException()
      Optional exception class name
    • getTimestamp

      public OffsetDateTime getTimestamp()
      When the error occurred, defaults to LocalDateTime.now()
    • getStatusCode

      public int getStatusCode()
      Numerical representation of status
    • getGlobalErrors

      public List<GlobalError> getGlobalErrors()
      Optionally gives additional information to the client about what was wrong with their request
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • withType

      public ApiError withType(String type)
      A machine-readable representation of the type of error being returned
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • withTitle

      public ApiError withTitle(String title)
      A human-readable representation of the type of error being returned
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • withException

      public ApiError withException(String exception)
      Optional exception class name
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • withTimestamp

      public ApiError withTimestamp(OffsetDateTime timestamp)
      When the error occurred, defaults to LocalDateTime.now()
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • withStatus

      public ApiError withStatus(org.springframework.http.HttpStatusCode status)
      Should always match exactly the status in the response header. Makes it easier to parse out and log
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • withStatusCode

      public ApiError withStatusCode(int statusCode)
      Numerical representation of status
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • withGlobalErrors

      public ApiError withGlobalErrors(List<GlobalError> globalErrors)
      Optionally gives additional information to the client about what was wrong with their request
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
      See Also:
    • setException

      public void setException(String exception)
      Optional exception class name
    • setTimestamp

      public void setTimestamp(OffsetDateTime timestamp)
      When the error occurred, defaults to LocalDateTime.now()