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);
await StoreListMailToDB(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. I want something like this but I don't want to return nulls
public async Task<List<MailDto>> GetMailList(int idUser)
{
try
{
string uri = GlobalSettings.Instance.MailEndpoint + idUser;
List<MailDto> listaMailDto = await _requestProvider.GetAsync<List<MailDto>>(uri);
List<MailBo> listMappead = MapperMail.MapMailList(listaMailDto)
StoreListMailToDB(listMappead);
return listMappead;
}
catch (ServiceAuthenticationException ex)
{
Log.CreateErrorLog("Service.35 - ServiceAuthenticationException \n" + ex.ToString());
DependencyService.Get<IDialogs>().ShowMessage(AppResources.errorBadUser);
return null;
}
catch (HttpRequestExceptionEx ex)
{
Log.CreateErrorLog("Service.40 - HttpRequestExceptionEx \n" + ex.ToString());
DependencyService.Get<IDialogs>().ShowMessage(AppResources.errorNoConection);
return null;
}
catch(Exception ex)
{
Log.CreateErrorLog("Service.45 - Exception \n" + ex.ToString());
DependencyService.Get<IDialogs>().ShowMessage(AppResources.error);
return null;
}
}
Any idea?