Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Best way to manage services and exceptions

$
0
0

I have a mobile app who consumes a api service. I have a class to manage all the operations (get, post, put) who throws exceptions if the result is not 200.

The app was originaly designed to be mostly offline so my services where like this:

    public async Task<bool> GetMailList(int idUser)
    {
        try
        {
            string uri = GlobalSettings.Instance.MailEndpoint + idUser;
            List<MailDto> listaMailDto = await _requestProvider.GetAsync<List<MailDto>>(uri);                    
            StoreListMailToDB(List<MailBo> listMappead = MapperMail.MapMailList(listaMailDto));
            return true;
        }
        catch (ServiceAuthenticationException ex) 
        {
            Log.CreateErrorLog("Service.35 - ServiceAuthenticationException \n" + ex.ToString());
            DependencyService.Get<IDialogs>().ShowMessage(AppResources.errorBadUser);
            return false;
        }
        catch (HttpRequestExceptionEx ex)
        {
            Log.CreateErrorLog("Service.40 - HttpRequestExceptionEx \n" + ex.ToString());
            DependencyService.Get<IDialogs>().ShowMessage(AppResources.errorNoConection);
            return false;
        }
        catch(Exception ex)
        {
            Log.CreateErrorLog("Service.45 - Exception \n" + ex.ToString());
            DependencyService.Get<IDialogs>().ShowMessage(AppResources.error);
            return false;
        }
    }

So the service gets the data, and stores it to Sqlite. If was all ok I return true, if no I return false. And in the screen I call to Sqlite to retrieve the data.

The problem comes now because we want to change it to a mostly online app. I want to keep the structure where the services manages the exception getting the data when the function ends but I don't want to return null if sometimes was wrong.

Any idea?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>