Record Class StandardErrorResponse
java.lang.Object
java.lang.Record
io.github.grumpystuff.grumpyrest.response.standard.StandardErrorResponse
- Record Components:
status- the HTTP statusmessage- the error messagefields- 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents an error for a single field. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StandardErrorResponseThis response indicates that the route is known in principle, but contains an ID that does not exist.static final StandardErrorResponseThe standard response for "something went wrong in the server", usually an uncaught exception.static final StandardErrorResponseThis gets responded (if even possible) when reading the request failed with a network error.static final StandardErrorResponseMost 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.static final StandardErrorResponseThis response indicates that no route is known for the requested URL. -
Constructor Summary
ConstructorsConstructorDescriptionStandardErrorResponse(int status, String message) Constructor without field errors.StandardErrorResponse(int status, String message, StandardErrorResponse.Field field) Constructor with a single field error.StandardErrorResponse(int status, String message, List<StandardErrorResponse.Field> fields) Compact constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.fields()Returns the value of thefieldsrecord component.final inthashCode()Returns a hash code value for this object.message()Returns the value of themessagerecord component.static StandardErrorResponseThis is an error response for failed request body validation.static StandardErrorResponserequestBodyValidationFailed(FieldErrorNode errorNode) This is an error response for failed request body validation.static StandardErrorResponserequestBodyValidationFailed(List<FieldErrorNode.FlattenedError> fieldErrors) This is an error response for failed request body validation.intstatus()Returns the value of thestatusrecord component.final StringtoString()Returns a string representation of this record class.voidtransmit(ResponseTransmitter responseTransmitter) Transmits this response using the specified response transmitter.
-
Field Details
-
IO_ERROR
This gets responded (if even possible) when reading the request failed with a network error. -
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
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
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
The standard response for "something went wrong in the server", usually an uncaught exception.
-
-
Constructor Details
-
StandardErrorResponse
Compact constructor.- Parameters:
status- the HTTP statusmessage- the error messagefields- the field errors
-
StandardErrorResponse
Constructor without field errors.- Parameters:
status- the HTTP status codemessage- the error message
-
StandardErrorResponse
Constructor with a single field error.- Parameters:
status- the HTTP status codemessage- the error messagefield- the field error
-
-
Method Details
-
requestBodyValidationFailed
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
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
Description copied from interface:ResponseTransmits 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
RequestCycleto 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:
transmitin interfaceResponse- Parameters:
responseTransmitter- the response transmitter that is used to actually send data to the client- Throws:
IOException- on I/O errors
-
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. -
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. -
equals
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 withObjects::equals(Object,Object); primitive components are compared with '=='. -
status
public int status()Returns the value of thestatusrecord component.- Returns:
- the value of the
statusrecord component
-
message
Returns the value of themessagerecord component.- Returns:
- the value of the
messagerecord component
-
fields
Returns the value of thefieldsrecord component.- Returns:
- the value of the
fieldsrecord component
-