Announcement Announcement Module
Collapse
No announcement yet.
Transforming exceptions from CustomDateEditor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transforming exceptions from CustomDateEditor

    Hi!
    I have registered the CustomDateEditor in my Controller class to bind Strings from the form to the date field in my command object. Everything works fine as long if I enter a (string) date that the CustomDateEditor is able to transform to a date. But if I enter something that is not possible to transform to a date, the propertyEditor will throw an exception that will be shown on the page. This is what I want, my problem is only that I would like to show a user friedly message, not the "Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'jXXXXX; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "342"

    How do I make the error display a text from the resource bundle instead of the real Exception?

    /Jo

  • #2
    what if you catch the ParseException that you're likely getting with an invalid date format and rethrow an IllegalArgumentException

    Comment


    • #3
      Thanks for the answer, but where do I catch the Exception? (I guess this is what my problem really boils down too). From my point of view, the binding take place inside Spring and I don't really have any place to catch exceptions thrown. At the end, the exception is put, by Spring, in the status.errorMessages, but I would like to have my own, user friedly, message there, not the real Exception message.

      Does that make sense?

      /Jo

      Comment


      • #4
        http://static.springframework.org/sp...sResolver.html tells you all about it

        Comment


        • #5
          I found that I had to override the bind()-method to overcome this problem:

          Code:
          	protected void bind(ServletRequest request, Object command) throws Exception
          	{
          		try
          		{
          			super.bind(request, command);
          		}
          		catch (ServletRequestBindingException e)
          		{
          			logger.info("Caught ServletRequestBindingException");
          		}
          	}
          This way the ServletRequestBindingException caused by the invalid date format is caught. The TypeMismatch error that caused this can be declared in the messages.properties:

          Code:
          typeMismatch= No valid value
          I am using a MulitActionController. Not sure if the same applies to a SimpleFormController. I do not like this construction very much but haven't fount a better way to do it. Maybe there are other suggestions?

          Comment


          • #6
            Thanks for your help!
            I managed to get it to work just by adding the exception message to the messages.properties like both of you said. Couldn't believe it would be that easy, thank you Spring!

            /Jo

            Comment

            Working...
            X