Class Errors

All Implemented Interfaces:
Serializable, Cloneable, Iterable<Errors.ErrorMessage>, Collection<Errors.ErrorMessage>, Deque<Errors.ErrorMessage>, List<Errors.ErrorMessage>, Queue<Errors.ErrorMessage>, SequencedCollection<Errors.ErrorMessage>

public final class Errors extends LinkedList<Errors.ErrorMessage>
Errors utility used to file processing messages (e.g. validation, provider, resource building errors, hint).

Use collector() to collect message through methods Errors.Collector.hint(String), Errors.Collector.warn(String), and Errors.Collector.fatal(String) and their counterparts with source object (e.g. Errors.Collector.fatal(Object, String). Once all messages are processed, return validation result from Errors.Collector.collect(). The consumer of validation messages can then check result with isValid() to get simple boolean, checkValid() to throw exception if not valid and other methods for fine-grained access. This class also extends a LinkedList of ErrorMessages to give you full access to all reported messages.

Example:


 Errors.Collector collector = Errors.collector();
 if (field == null) {
     collector.fatal(this, "Field 'field' is null");
 }
 doOtherValidations(collector);

 Errors errors = collector.collect();
 if (errors.isValid()) {
   return Response.VALID;
 } else {
   return Response.builder().errors(errors).build();
 }
 
See Also:
  • Method Details

    • collector

      public static Errors.Collector collector()
      Create a new message collector.
      Returns:
      message collector to add messages to
    • hasFatal

      public boolean hasFatal()
      Check if a fatal message is part of these messages.
      Returns:
      true if there is at least one message with severity Severity.FATAL
    • hasWarning

      public boolean hasWarning()
      Check if a warning message is part of these messages.
      Returns:
      true if there is at least one message with severity Severity.WARN
    • hasHint

      public boolean hasHint()
      Check if a hint message is part of these messages.
      Returns:
      true if there is at least one message with severity Severity.HINT
    • log

      public boolean log(System.Logger logger)
      Log supplied errors and return a status flag indicating whether the result is OK or not (will return true for valid, false if Severity.FATAL is present).
      Parameters:
      logger - Util logger to log messages into
      Returns:
      true if there are no fatal issues present in the collection, false otherwise.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<Errors.ErrorMessage>
    • isValid

      public boolean isValid()
      Check if these messages are a valid result.
      Returns:
      true if there is NO message in Severity.FATAL severity
    • checkValid

      public void checkValid() throws Errors.ErrorMessagesException
      Check if these messages are a valid result, throws exception if not.
      Throws:
      Errors.ErrorMessagesException - in case there is at least one Severity.FATAL severity message