Class FormFieldException

  • All Implemented Interfaces:
    java.io.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:
    Serialized Form
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)  
      java.lang.String getFieldName()
      Returns the fully-qualified name (JavaScript syntax) of the form field causing this error.
      int hashCode()  
      FormFieldException propagate​(java.lang.String name)
      Returns self with name prepended, for propagating exceptions up the stack.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Throwable

        addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • FormFieldException

        public FormFieldException​(@Detainted
                                  java.lang.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
                                  java.lang.String userMessage,
                                  java.lang.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
                                  java.lang.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​(java.lang.String field,
                                  @Detainted
                                  java.lang.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 Detail

      • getFieldName

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

        @CheckReturnValue
        public FormFieldException propagate​(java.lang.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
                              java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Throwable