I am currently thinking about some design choices regarding exception handling.
My current architecture looks a little like this:
You can see that I have a UI where a try-catch middleware is cathing exceptions like this:
Pseudocode:
catch(CoreUserNotFoundException e)
{
return 'You provided an invalid user id'
}
catch(CoreToLongException e)
{
return 'The name is too long'
}
catch(Exception e)
{
logger.log(e)
return 'Something went wrong'
}
So only some messages will be returned to the user. All other exceptions are just logged.
Lets say someone wants to add an entity with a user id that has to be validated.
But how do I know weather the user exists or not?
This is done with my business logic. It provides an interface "IUserService" to retrieve a user. The business logic does not care how the app is doing this.
It could get the user via Graph API of Microsoft, or via Auth0 or via Facebook or whatever.
My business logic should work with all kinds of user services.
So in my picture I have a MicrosoftGraph Adapter, that implements the IUserService. This adapter is using my own Graph SDK - this SDK uses the original SDK of Microsoft, but I like my own wrapper and use it in many apps.
In my current design the "MS Graph SDK" throws an Exception if a user does not exist for an id. This exception is bubbling through my own Graph SDK and caught in the Graph Adapter of my app.
The Graph adapter then transforms this Exception in a CoreUserNotFoundException - the one I am showing to the user:
Pseudocode:
try {
user = _userService.getUserById(userId);
} catch(GraphUserNotFoundException e)
throw new CoreUserNotFoundException("You provided a bad user id", e);
}
Of course there are alternative ways:
I could also define an Exception in my Own Graph SDK and re-use this exception through all my projects.
But to show the user a nice message, my UI needed to have a dependency on "Own Graph SDK" because I want to do:
catch(MyOwnGraphSDKUserNotFoundException e)
{
return 'You provided an invalid user id'
}
catch(Exception e)
{
logger.log(e)
return 'Something went wrong'
}
What is probably a bad practice?
In this case I would have to change the UI-code if the Adapter changes, so I am not doing it.
My questions to you:
Was my design choice correct? Or does it seem unprofessional to you? I am still learning about clean architectures, hexagonal architectures etc., but didnt read too much about Exception handling in this topic.
In my current design the "MS Graph SDK" throws an Exception if a user does not exist for an id. This exception is bubbling through my own Graph SDK and caught in the Graph Adapter of my app.
hell. why? What's the point of the wrapper then?