Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making use of the ValidationException class

I have a situation where one of our developers wants to put a standard in place where we throw System.ComponentModel.DataAnnotations.ValidationExceptions in all our apps moving forward. An example would be that a user enters bad data into a form and our business logic layer throws a ValidationException which is handled at the calling layer.

However, I'm worried that this exception class is being used out of context and that one day we'll using some Dynamic Data Controls which make use of this exception and it will be difficult to tell the difference between when he's making use of the ValidationException vs times that the Dynamic Controls raise the exception.

We already use a custom exception class called something like "OurCustomException" and I think it would be better to just subclass off of that and create a OurCustomValidationException class. This way exceptions of different types can be clear cut.

Any opinions?

like image 955
Will.NET Avatar asked Feb 22 '11 18:02

Will.NET


People also ask

How to use ValidationException in laravel?

Create a new validation exception from a plain array of messages. Get all of the validation error messages. Set the HTTP status code to be used for the response. Set the error bag on the exception.

What is ValidationException?

A validation exception occurs if an input value does not match the expected data type, range or pattern of the data field. For example, if a user enters an integer value in a data field that expects a DateTime value, a validation exception occurs.

What is validation exception Java?

This exception indicates that an error has occurred while performing a validate operation. The ValidationEventHandler can cause this exception to be thrown during the validate operations.


1 Answers

... it will be difficult to tell the difference between when he's making use of the ValidationException vs times that the Dynamic Controls raise the exception.

I think this is the main point you should be looking at when making that decision.

You seem to imply that the above (not being able to distinguish your own exceptions from "platform" validation exceptions) is a bad thing. That's not necessarily the case. IF you use the ValidationException exclusively to represent validation errors, then all your code can deal correctly with both your own and platform exceptions in the same way. No need to special-case platform exceptions from custom ones.

This is a win in my opinion. If you have both CustomException and ValidationException going back up to your toplevel layer for the same reasons, you'll have to repeat some logic one way or another. That's a bad thing (more maintenance, more chances of bugs creeping in).

So my opinion is that using the platform ValidationException is probably a good way to do it as long as you use it strictly for propagating validation problems.

Also think about the case where you would be giving/selling parts of your code to a third party (say it's real cool and you make a product out of it). It would probably be easier for the third party if your module throws "standard" exceptions (they can integrate it easily) rather than having to special-case all of his interface code for your module. Again, this is only valid if you stick with cases where a standard module would throw ValidationExceptions.

Lets look at it the other way around. You say:

our business logic layer throws a ValidationException

This is why I put strictly and exclusively above. You need to make sure you agree on what a validation error is. Lets look at two hypothetical problems:

  1. "abc" is not a valid number
  2. insufficient funds for this operation

For 1., the problem is simple/expected input validation error. But in my opinion, 2. is not. It's a business logic problem. You could call it a validation issue (in the transaction, before the debit, you "validate" whether there are sufficient funds available) but I'd say its semantically very different.

I would advise not to put these two types of errors in the same exception "bag" as they have very different meanings, and may (often) lead to different application flow logic. (With the above two examples, 1. should keep the user on the very same form, as would any other "typo" kind of issue, but 2. should probably get him to a page that allows him to refill his account.)

To sum it up: using a standard exception seems like a good idea to me, as long as you stick with its expected semantics.

like image 152
Mat Avatar answered Oct 12 '22 04:10

Mat