Class FormFieldException

All Implemented Interfaces:
Serializable

@NotThreadSafe public final class FormFieldException extends FormException
Exception thrown when a form field contains a bad value.

You can safely throw FormFieldException from within your validator functions, and the field name will automatically be propagated into the exception object for you.

The way that field names work is a bit complicated, because we need to support complex nested field names like foo[3].bar. So what happens is the original exception will be thrown by a FormField validator without the field set. Then as the exception bubbles up the stack, it'll be caught by the convert method, which then prepends the name of that component. Then when the exception reaches the user, the getFieldName() method will produce the fully-qualified field name.

This propagation mechanism is also very important when writing FormField.Builder.transform(java.lang.Class<T>, java.util.function.Function<O, T>) functions, which oftentimes will not know the name of the field they're validating.

See Also:
  • Constructor Details

    • FormFieldException

      public FormFieldException(@Detainted String userMessage)
      Creates a new FormFieldException

      This exception should only be thrown from within a FormField converter function. The field name will automatically be propagated into the exception object for you.

      Parameters:
      userMessage - should be a friendly message that's safe to show to the user.
    • FormFieldException

      public FormFieldException(@Detainted String userMessage, Throwable cause)
      Creates a new FormFieldException

      This exception should only be thrown from within a FormField converter function. The field name will automatically be propagated into the exception object for you.

      Parameters:
      userMessage - should be a friendly message that's safe to show to the user.
      cause - the original cause of this exception (non-null).
    • FormFieldException

      public FormFieldException(FormField<?,?> field, @Detainted String userMessage)
      Creates a new FormFieldException for a particular form field.

      This exception should only be thrown from within a FormField MAP converter function in situations where you're performing additional manual validation.

      Parameters:
      userMessage - should be a friendly message that's safe to show to the user.
    • FormFieldException

      public FormFieldException(String field, @Detainted String userMessage)
      Creates a new FormFieldException for a particular field name.
      Parameters:
      field - name corresponding to a FormField.name()
      userMessage - friendly message that's safe to show to the user
  • Method Details

    • getFieldName

      public String getFieldName()
      Returns the fully-qualified name (JavaScript syntax) of the form field causing this error.
    • propagate

      @CheckReturnValue public FormFieldException propagate(String name)
      Returns self with name prepended, for propagating exceptions up the stack.

      This would be package-private except that it needs to be called by a test class in another package.

    • equals

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Overrides:
      toString in class Throwable