1

I am reading a lot about patterns and code structure and something that bothers me is BeanValidation's way to handle errors. I like Java and think that BeanValidation is easy to use, but it seems to me that using BeanValidation is kind of an anti pattern.

In one project I have just the normal BeanValidation which I catch with an ExceptionMapper to customize the response in my resource. Here is BeanValidation in use which in turn throws exceptions when errors occur.

In another project -- which contains lots of legacy code -- the structure is similar. The resource injects the ValidationService class which has some validation methods. In that ValidationService the checks BeanValidation is trigged manually (don't ask why, it's old code) and then some additional checks are done manually. When an error occurs it is gathered until the end of the validation method and then thrown. I should also mention that all cases in the ValidationService are defined by domain experts.

Both scenarios end in the ExceptionMapper which generates a response for the client. Both use Exceptions for that.

Now when reading all of the exceptions for control flow are anti pattern articles[1], I am wondering if it is the case here as well. And if so, is there a better solution to that? Since Java is not able to return a second value in methods like in Go, there's no good way to return an error otherwise, isn't it?

[1] like this one: https://softwareengineering.stackexchange.com/a/189225/354474

2 Answers 2

2

I'm going to take a similar view point as Bart, but expand on some specifics.

In this case you have a framework upon which your application is built. This framework comes with its own set of conventions and opinions about the right way to do things. Validations being handled as exceptions which are caught by the ExceptionMapper are likely an attempt to normalize error responses to clients. This reduces the boiler plate code required to handle error conditions in the application, which could be a considerable amount of code peppered all over the code base. To determine if something is an anti-pattern you need to analyze the bigger picture of how all these pieces fit together.

If I just think about an object whose responsibility is validating data, I would be surprised to find that it throws exceptions. I usually expect a "result" object that bundles a boolean flag indicating valid or invalid, along with a collection of validation messages in case the data is invalid. The Principal of Least Astonishment would make me think exceptions are an anti-pattern here, except when I step back and look at the bigger picture of how this framework handles error responses.

In this case I do not think exceptions are an anti-pattern. Surprising? Yes. But not an anti-pattern once you understand the trade-offs with how error responses are handled by the framework.

1
  • Thank you, that is a very good explanation. Of course you're right, I need to see the bigger picture here. :-)
    – Apollo
    May 25, 2021 at 16:53
2

The control flow referenced in the anti-pattern should be read as using exceptions for breaking out of a loop or returning from the current function to the immediate caller.

Exceptions are intended to be used if you encounter a problem that you cannot resolve at the point where you encounter it, where it is not guaranteed that the immediate caller can resolve the problem and where all processing of the request must be interrupted and rolled back to a point where you can either resolve the problem or inform the user in a nice way that their request could not be handled.

The exceptions used by the BeanValidation and ValidationService nicely fit with that intended usage of exceptions.

1
  • Thanks for the explanation. I also read Martin Fowler's article and he does not seem to agree, if I understand it correctly. (martinfowler.com/articles/replaceThrowWithNotification.html) But I guess you're right, there is not way we can resolve the issue if the input data is invalid. I'd upvote your answer but I have not enough reputation... So, feel upvoted.
    – Apollo
    May 25, 2021 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.