Class FieldErrorNode
- Direct Known Subclasses:
FieldErrorNode.Field,FieldErrorNode.InternalException,FieldErrorNode.Message,FieldErrorNode.Siblings
- get instances after an error has occurred, to access the field path and message programmatically
- create instances inside custom converters to expose the field path for nestable custom types
This class and its subclasses allow to quickly construct error information, then propagate it upwards while collecting the field path, and finally generate one or more error messages as strings or JSON from it.
An error is generated from a specific JSON value, e.g. from a JSON string whose format does not match a validation
pattern. Such an error is created with a message, but does not know the context (field path) in which it occurs,
nor does it know about other errors that may occur in other fields. The error is then wrapped in an exception
(either JsonDeserializationException or JsonSerializationException) and thrown to propagate it upwards.
Container values further up then process these exceptions and re-throw them, collecting multiple errors and
augmenting their field paths. The result is a tree structure of FieldErrorNode that contains all errors that have
occurred in the whole JSON structure. This tree structure is then usually flattened to a list of errors and
returned to the REST client.
Instances of this class and its subclasses are immutable. All grouping and nesting is done by creating new nodes around the existing nodes.
Instances of this class should be created using factory methods. The subclass constructors are private to enforce this.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA node that specifies a single field nesting level for its subtree.static final recordThis structure represents a single error, i.e. a leaf node, and contains the error message of theFieldErrorNode.Messagenode as well as the field path as a list of the field names of allFieldErrorNode.Fieldnodes on the way from the root to the message leaf node.static final classA leaf node with an internal exception.static final classA leaf node with an error message.static final classA node that groups two subtrees together to allow the tree to store multiple errors. -
Method Summary
Modifier and TypeMethodDescriptionfinal FieldErrorNodeand(FieldErrorNode other) Groups two error sets together in a single new node.static FieldErrorNodeCreates a node for a single error message without a field path.static FieldErrorNodeCreates a node for a single error message without a field path.flatten()Flattens the errors contained in this node and its subnodes as a list ofFieldErrorNode.FlattenedErrorobjects.protected abstract voidflatten(List<FieldErrorNode.FlattenedError> errors, List<String> segments) NOT PUBLIC APIfinal FieldErrorNodeAdds the nesting within a single field as a prefix to the field path of this node, returning the result as a new node.toString()This method turns the node to a string for simple output / debugging purposes.
-
Method Details
-
create
Creates a node for a single error message without a field path.- Parameters:
message- the error message- Returns:
- the newly created node
-
create
Creates a node for a single error message without a field path.- Parameters:
exception- an exception that caused the error- Returns:
- the newly created node
-
and
Groups two error sets together in a single new node. This and the other node may both contain multiple errors and possibly use field path suffixes. If this node is later used below another field path prefix, then that prefix applies to all errors from this and the other node.- Parameters:
other- the other node to group together with this one. May be null, in which case this method just returns this node -- this simplifies combining optional errors from multiple sources.- Returns:
- the node that groups both nodes
-
in
Adds the nesting within a single field as a prefix to the field path of this node, returning the result as a new node. The nesting affects all errors stored under this node.Note that if a prefix consisting of multiple nesting levels is to be added, then this method must be called multiple times in reverse order (i.e. from innermost field to outermost field).
- Parameters:
fieldName- the name of the field to add as a prefix- Returns:
- the node that has the prefix applied
-
flatten
Flattens the errors contained in this node and its subnodes as a list ofFieldErrorNode.FlattenedErrorobjects.- Returns:
- the flattened errors
-
flatten
NOT PUBLIC API- Parameters:
errors- ...segments- ...
-
toString
This method turns the node to a string for simple output / debugging purposes. It does not guarantee a specific format of the string. To process errors programmatically, useflatten()to get the contained errors in a more reliable format, or process the nodes directly.
-