Class ErrorEnvelope

java.lang.Object
is.codion.framework.json.db.ErrorEnvelope

public final class ErrorEnvelope extends Object
The body of a JSON error response, the wire form of a server side exception.

Carries a closed ErrorKind, a message, a correlation id linking the response to a server log entry, and a kind specific detail node for the kinds whose exception cannot be reconstructed from a message alone.

A database exception based on an error type carries the name of the errorType and the errorDetail, if any, with which the client puts the message together in its own language, the message being in the language of the server.

Neither a stack trace nor a cause chain is ever carried, in either direction. A client reconstructing an exception from an envelope maps the kind() to a known constructor; nothing on the wire names a class.

 {
   "kind" : "CONFLICT_REFERENTIAL",
   "message" : "This record is referenced by records in other tables, delete those first: EMP_DEPT_FK",
   "correlationId" : "b1f4c8e2-3a1d-4f0b-9c5e-7d2a6f8b1c3e",
   "detail" : { "operation" : "DELETE" },
   "errorType" : "CHILD_EXISTS",
   "errorDetail" : "EMP_DEPT_FK"
 }
See Also:
  • Field Details

  • Constructor Details

    • ErrorEnvelope

      public ErrorEnvelope(String kind, String message, String correlationId, @Nullable com.fasterxml.jackson.databind.JsonNode detail, @Nullable String errorType, @Nullable String errorDetail)
      Parameters:
      kind - the ErrorKind name, a string rather than the enum so an unknown kind does not fail to parse
      message - the error message
      correlationId - identifies the server log entry describing this error
      detail - kind specific detail, null if the kind carries none
      errorType - the name of the ErrorType a database exception is based on, a string rather than the enum so an unknown error type does not fail to parse, null if it is not based on one
      errorDetail - the error detail of a database exception based on an error type, null if none
  • Method Details

    • kind

      public String kind()
      Returns:
      the ErrorKind name, see errorKind()
    • message

      public String message()
      Returns:
      the error message
    • correlationId

      public String correlationId()
      Returns:
      the id identifying the server log entry describing this error
    • detail

      public @Nullable com.fasterxml.jackson.databind.JsonNode detail()
      Returns:
      the kind specific detail, or null if the kind carries none
    • errorType

      public @Nullable String errorType()
      Returns the name of the error type a database exception is based on. With it the client puts the message together in its own language, message() being in the language of the server. A client not knowing the error type, one older than the server, or one not written in Java and not localizing, uses message().
      Returns:
      the name of the ErrorType, null if the exception is not based on one
      See Also:
    • errorDetail

      public @Nullable String errorDetail()
      Returns:
      the error detail, the name of a constraint, column or table for example, null if none
      See Also:
    • errorKind

      public Optional<ErrorKind> errorKind()
      Returns:
      the ErrorKind, an empty Optional in case this envelope carries a kind unknown here, which a client older than the server encounters
    • toString

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

      public String toJson() throws com.fasterxml.jackson.core.JsonProcessingException
      Returns:
      this envelope as a JSON string
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - in case of an error
    • fromJson

      public static ErrorEnvelope fromJson(byte[] json) throws IOException
      Parameters:
      json - the JSON to parse
      Returns:
      the ErrorEnvelope the given JSON represents
      Throws:
      IOException - in case the given JSON is not an error envelope