Reputation: 31
What method can you suggest to handle error messages from API to UI?
In other projects I have worked on, we sent error codes to the UI, then the UI reads the corresponding message from a file, which could be just another class or resx. They say is useful for localization.
I have read somewhere that it's a good practice to throw exceptions. I am just worried that the program would crash if the error was not caught at the UI side.
I'm using WPF for the UI, and will be using WCF later on for the services. It's a windows application.
Upvotes: 0
Views: 1188
Reputation: 5128
I'd suggest throwing normal .NET exceptions in case there's an error on the API side. The big disadvantage of using error codes is that depending on how the layers of your application are organized (DAL -> Business Logic -> API -> UI), it might be very problematic to propagate and handle them.
With exceptions, you can handle them easily at any level (by using try-catch
construct) and either re-throw (e.g. when you only need to log an exception and there's a code further down the stack that is supposed to handle it on its own) or "swallow" them (not generally recommended, though).
As for how to handle errors on the UI level, I'd let them "flow" all the way up and then "decorate" them (e.g. by showing a modal dialog with some details and troubleshooting tips). For example, if you're working with Web APIs, you can just return appropriate HTTP statuses (401, 403, 500) from your API and handle them on the client by having an error hook, like $.ajaxError
which will show some sort of modal dialog. Here you're not inventing your own way of reporting errors but rather leveraging what's already defined by HTTP specs (and therefore, it's very easy to understand the behavior as well as to respond to it).
I'm not that familiar with WPF, but I think there should be something similar to Application_Error
in WinForms - an event handler where you can examine the error and "decorate" it by showing some nice modal dialog to the user.
Hope this helps.
Upvotes: 1