The BestSellers sample in MvvmCross handles errors using two primary techniques:
1. BaseViewModel Classes for Shared Error Handling: The BestSellers sample utilizes a `BaseViewModel` class to encapsulate shared code, including error handling mechanisms. This approach allows for centralized management of common error handling logic across different view models. For instance, the `GeneralAsyncLoad` method is defined in the `BaseViewModel` and is used by various view models to load data asynchronously. This method can include error handling logic that is shared across the application.
2. App-Level Error Router: The sample also employs an app-level "error router" to manage how errors are propagated from view models to the user interface. This involves using an injected object, typically an `IErrorReporter`, which is initialized as a singleton during app construction. Each platform-specific UI project subscribes to events from this singleton using an `IErrorSource` interface. This setup enables platform-specific error display mechanisms, such as UIAlertViews on iOS or Toasts on Android, allowing each platform to handle errors in a way that is native to its environment.
In practice, when an error occurs during an asynchronous operation, such as loading data from a web service, the error is caught and reported using the `IErrorReporter`. This allows the application to display user-friendly error messages or take alternative actions like retrying the operation or loading offline data.
For example, if a `CategoryListViewModel` encounters an error while loading categories, it can use the `GeneralAsyncLoad` method from the `BaseViewModel` to handle the error. The error is then reported through the `IErrorReporter`, which triggers the appropriate UI notification based on the platform.
This approach ensures that error handling is both centralized and flexible, allowing for easy adaptation to different platforms and scenarios within the MvvmCross framework.
Citations:[1] http://slodge.blogspot.com/2012/05/one-pattern-for-error-handling-in.html
[2] https://leaware.com/creating-cross-platform-xamarin-applications-using-the-mvvmcross-framework/
[3] https://asktom.oracle.com/ords/f?p=100%3A11%3A%3A%3A%3A%3AP11_QUESTION_ID%3A1422998100346727312
[4] https://stackoverflow.com/questions/10411352/what-is-the-suggested-way-to-handle-exceptions-during-in-a-mvvmcross-application
[5] https://github.com/MvvmCross/MvvmCross-Samples/blob/master/OldSamples/BestSellers/BestSellers.Touch/AppDelegate.cs
[6] https://discourse.getdbt.com/t/how-to-handle-test-failures/3824
[7] https://www.telerik.com/blogs/blazor-best-practices-handling-errors
[8] https://stackoverflow.com/questions/15697523/running-sample-projects-in-mvvmcross-v3-hot-tuna
[9] https://visualstudiomagazine.com/articles/2018/05/08/build-a-xamarin.forms-application.aspx