Record Class StandardErrorResponse

java.lang.Object
java.lang.Record
io.github.grumpystuff.grumpyrest.response.standard.StandardErrorResponse
Record Components:
status - the HTTP status
message - the error message
fields - the field errors
All Implemented Interfaces:
Response

public record StandardErrorResponse(int status, String message, List<StandardErrorResponse.Field> fields) extends Record implements Response
Implements a standard JSON-based error response format, combined with a selectable HTTP status code.

The "message" field contains a string message, and the "fields" contains a list of field errors, each with a field path and message. This allows to send a response that mentions errors in multiple fields at the same time. If, for example, JSON validation fails for the request body, then the individual field errors can be mentioned to the client all at once.

  • Field Details

    • IO_ERROR

      public static final StandardErrorResponse IO_ERROR
      This gets responded (if even possible) when reading the request failed with a network error.
    • UNKNOWN_URL

      public static final StandardErrorResponse UNKNOWN_URL
      This response indicates that no route is known for the requested URL. It usually indicates a bug in the client, such as a typo in the URL.
    • ID_NOT_FOUND

      public static final StandardErrorResponse ID_NOT_FOUND
      This response indicates that the route is known in principle, but contains an ID that does not exist. This can happen routinely when requesting entities that are not known in advance to exist.
    • JSON_EXPECTED

      public static final StandardErrorResponse JSON_EXPECTED
      Most endpoints expect a JSON request body (if they expect a request body at all), so here is a standard response for other request content types.
    • INTERNAL_SERVER_ERROR

      public static final StandardErrorResponse INTERNAL_SERVER_ERROR
      The standard response for "something went wrong in the server", usually an uncaught exception.
  • Constructor Details

    • StandardErrorResponse

      public StandardErrorResponse(int status, String message, List<StandardErrorResponse.Field> fields)
      Compact constructor.
      Parameters:
      status - the HTTP status
      message - the error message
      fields - the field errors
    • StandardErrorResponse

      public StandardErrorResponse(int status, String message)
      Constructor without field errors.
      Parameters:
      status - the HTTP status code
      message - the error message
    • StandardErrorResponse

      public StandardErrorResponse(int status, String message, StandardErrorResponse.Field field)
      Constructor with a single field error.
      Parameters:
      status - the HTTP status code
      message - the error message
      field - the field error
  • Method Details

    • requestBodyValidationFailed

      public static StandardErrorResponse requestBodyValidationFailed(JsonDeserializationException e)
      This is an error response for failed request body validation. The error lists the fields that failed validation and their error messages.
      Parameters:
      e - the exception from the failed validation
      Returns:
      the instance of this class
    • requestBodyValidationFailed

      public static StandardErrorResponse requestBodyValidationFailed(FieldErrorNode errorNode)
      This is an error response for failed request body validation. The error lists the fields that failed validation and their error messages.
      Parameters:
      errorNode - the error node from the failed validation
      Returns:
      the instance of this class
    • requestBodyValidationFailed

      public static StandardErrorResponse requestBodyValidationFailed(List<FieldErrorNode.FlattenedError> fieldErrors)
      This is an error response for failed request body validation. The error lists the fields that failed validation and their error messages.
      Parameters:
      fieldErrors - the (flattened) field errors from the failed validation
      Returns:
      the instance of this class
    • transmit

      public void transmit(ResponseTransmitter responseTransmitter) throws IOException
      Description copied from interface: Response
      Transmits this response using the specified response transmitter.

      Note: This method only takes the response transmitter. If the implementation has to access other things from the RequestCycle to transmit itself, e.g. access request parameters on-the-fly, then these things have to be passed to the implementation by another mechanism. This interface tries to keep the dependency between the two minimal.

      Specified by:
      transmit in interface Response
      Parameters:
      responseTransmitter - the response transmitter that is used to actually send data to the client
      Throws:
      IOException - on I/O errors
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • status

      public int status()
      Returns the value of the status record component.
      Returns:
      the value of the status record component
    • message

      public String message()
      Returns the value of the message record component.
      Returns:
      the value of the message record component
    • fields

      Returns the value of the fields record component.
      Returns:
      the value of the fields record component