Class Problem

java.lang.Object
nz.org.riskscape.problem.Problem
Direct Known Subclasses:
Unchecked

public class Problem extends Object

Class used for describing errors and other issues with user defined input. Problems are hierarchical, which is used to 'target' where issues are found, e.g. a problem with your function that is in your model that is in your project might have three levels, e.g. There was a problem in your project... -> There was a problem with your model... -> The function 'hazard-calculator' does not exist

This code is going through a 'transition' phase as it moves away from free form messages to something more structured and linked to the i18n system. Over time, we should: * purge all the code related to default messages * use annotations to apply structure to the various args, e.g. an 'expected' and 'actual' argument

See Problems for tips on constructing Problems.

  • Field Details

  • Constructor Details

  • Method Details

    • info

      @Deprecated public static Problem info(String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • warning

      @Deprecated public static Problem warning(String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • warning

      @Deprecated public static Problem warning(Throwable t, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • error

      @Deprecated public static Problem error(String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • error

      @Deprecated public static Problem error(Throwable t, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • fatal

      @Deprecated public static Problem fatal(Throwable t, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • composite

      @Deprecated public static Problem composite(List<Problem> children, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead

      Builds a Problem that contains the child Problems.

      The level of the the returned Problem is the most severe from those of the children.

      Parameters:
      children - the problems the new Problem should wrap
    • composite

      @Deprecated public static Problem composite(Problem.Severity severity, List<Problem> children, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead

      Builds a Problem that contains the child Problems, forcing a particular severity for the created parent.

      Parameters:
      children - the problems the new Problem should wrap
    • error

      public static Problem error(ProblemCode code, Object... args)

      Create an problem with Problem.Severity.ERROR with a particular ProblemCode and message arguments.

    • max

      public static Problem.Severity max(Collection<? extends Problem> children)
    • hasErrors

      public static boolean hasErrors(Collection<? extends Problem> problems)
    • throwIfErrors

      public static <T extends Throwable> void throwIfErrors(List<Problem> problems, Function<List<Problem>,T> supplier) throws T
      Throws:
      T extends Throwable
    • withChildren

      public Problem withChildren(Problem... moreChildren)
    • withChildren

      public Problem withChildren(List<? extends Problem> moreChildren)

      Copy this Problem but add in more children.

    • setChildren

      public Problem setChildren(List<Problem> newChildren)

      Copy this Problem but replace the children.

    • withSeverity

      public Problem withSeverity(Problem.Severity sev)

      Copy this Problem but with a new severity

    • withException

      public Problem withException(Throwable t)

      Copy this Problem but with a new exception

    • affecting

      public Problem affecting(Class<?> clazz, String name)

      Copy this Problem but add details of the specific thing that the problem affects. E.g. the specific Parameter or pipeline Step affected, etc. Note that there are several variants as in some cases we may know the class and name but not have an object, whereas other cases may have an object but no name.

    • affecting

      public Problem affecting(Object thing)
    • isError

      public boolean isError()
    • isFatal

      public boolean isFatal()
    • hasChildren

      public boolean hasChildren()
    • getMessage

      public String getMessage()
    • getDefaultMessage

      public String getDefaultMessage()
    • getAffectedName

      public String getAffectedName()
      Returns:
      the name of the thing affected by this Problem (or an empty String if there is no such context associated with it)
    • getAffectedObject

      public Optional<Object> getAffectedObject()
      Returns:
      the thing affected by this Problem, e.g. the Parameter object. Returns Optional.empty() if there is no specific object affected.
    • getAffectedClass

      public Class<?> getAffectedClass()
      Returns:
      the class of thing affected by this Problem, e.g. Parameter.class. Returns Object.class if this is unknown.
    • affects

      public boolean affects(Class<?> superclass)
      Returns:
      true if the Problem affects the given class or superclass
    • getAffected

      public <T> Optional<T> getAffected(Class<T> classOfThing)

      With Problems, there is usually something this is most affected by the Problem, e.g. a Parameter or a File or a Bookmark. This checks if this Problem affects the class given, and if so, returns that thing. E.g. 'I know this Problem affects a File, gimme the File'.

    • filterAffected

      public static List<Problem> filterAffected(Collection<? extends Problem> problems, Class<?> contextClass)

      Filters the list of Problems (and their children) based on the affected class, e.g. find any/all problems that affect a Parameter, Step, etc. A flattened list is returned, i.e. it may be a mix of parents and children, regardless of where they occur in the problem hierarchy.

      Returns:
      a list of Problems that affect the given class. Or an empty list, if no Problems affect the given class.
    • debugString

      public static String debugString(Problem origin)
      Returns:
      a string that gives some idea of the structure of this problem and its children, but without requiring any i18n resources to give actual user-facing error messages
    • debugString

      public static String debugString(List<Problem> problems)

      Variation of debugString(Problem) that dumps out a list of problems

    • getChildren

      public List<Problem> getChildren()
    • toString

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

      public Throwable findAnyException()

      Finds any exception info related to this problem, including exceptions that may have been caught by child/nested Problems

    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getSeverity

      public Problem.Severity getSeverity()
    • getArguments

      public Object[] getArguments()
    • getException

      public Throwable getException()
    • getCode

      public ProblemCode getCode()