We have the following scenario in our company:
There are some business objects which have business rules associated with them. One of these objects, Person, has the following rules:
- A Person must have a name (that is in coding terms not null, nor empty)
- A Person's name mustn't consist solely of spaces and/or tabs
The business object has in its constructor a parameter for name, which uses a non-nullable type to receive the name. In similar fashion there's a property which stores the name as a non nullable type.
In the constructor an exception (
MyException) is thrown when the name is empty after trimming the spaces and/or tabs.
There's also a backend which deals with these objects using REST api through JSON
Right now we are using a library (Jackson) to parse JSON data received in our backend. We have specified in our DTOs that the field for the name can't be null or be missing from the JSON object. In case of deserializing a JSON object with a missing/null name field the library throws an exception.
If a Person received in the backend throws
MyException then the backend converts that exception into an detailed JSON message to be sent to the client.
But if the received JSON of a Person doesn't have the field 'name' or is null we let the library throws its own Exception and that is the error presented to the client.
The question is: Should the exception thrown by the parsing library be treated the same as
MyException given that both are business rules?