Proper Error handling
-
greetings guys. im building an android application and i have some second thoughts on some error handling case. I have a method that gets data from the internet by calling this method:
public static string StoredDatesList
{
get => Preferences.Get(nameof(StoredDatesList), string.Empty);
set => Preferences.Set(nameof(StoredDatesList), value);
}
public static async Task GetDraws(Uri url, string date)
{
Dictionary StoredDates = new Dictionary();
StoredDates = JsonConvert.DeserializeObject>(StoredDatesList);
var contents = string.Empty;
HttpClient client = new HttpClient();if (StoredDates != null) if (StoredDates.ContainsKey(date)) { contents = StoredDates\[date\]; } else { var current = Connectivity.NetworkAccess; if (current != NetworkAccess.Internet) return null; client = new HttpClient(); contents = await client.GetStringAsync(url); var res2 = JsonConvert.DeserializeObject(contents.ToString()); if (180 == res2.content.Count) { StoredDates.Add(date, contents); StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented); } } else { StoredDates = new Dictionary(); contents = await client.GetStringAsync(url); var res2 = JsonConvert.DeserializeObject(contents.ToString()); if (180 == res2.content.Count) { StoredDates.Add(date, contents); StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented); } } return contents; }
the if statement
current != NetworkAccess.Internet)
checks if internet is available, when internet is not available i return null and i check if the data is null and im displaying a message(error, internet is not available etc). I find this approach very bad and im trying to think how is the proper way to handle this. i cannot show a message t
-
greetings guys. im building an android application and i have some second thoughts on some error handling case. I have a method that gets data from the internet by calling this method:
public static string StoredDatesList
{
get => Preferences.Get(nameof(StoredDatesList), string.Empty);
set => Preferences.Set(nameof(StoredDatesList), value);
}
public static async Task GetDraws(Uri url, string date)
{
Dictionary StoredDates = new Dictionary();
StoredDates = JsonConvert.DeserializeObject>(StoredDatesList);
var contents = string.Empty;
HttpClient client = new HttpClient();if (StoredDates != null) if (StoredDates.ContainsKey(date)) { contents = StoredDates\[date\]; } else { var current = Connectivity.NetworkAccess; if (current != NetworkAccess.Internet) return null; client = new HttpClient(); contents = await client.GetStringAsync(url); var res2 = JsonConvert.DeserializeObject(contents.ToString()); if (180 == res2.content.Count) { StoredDates.Add(date, contents); StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented); } } else { StoredDates = new Dictionary(); contents = await client.GetStringAsync(url); var res2 = JsonConvert.DeserializeObject(contents.ToString()); if (180 == res2.content.Count) { StoredDates.Add(date, contents); StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented); } } return contents; }
the if statement
current != NetworkAccess.Internet)
checks if internet is available, when internet is not available i return null and i check if the data is null and im displaying a message(error, internet is not available etc). I find this approach very bad and im trying to think how is the proper way to handle this. i cannot show a message t
If your app can continue without the data, and the user can do nothing about it, there is no point in telling them. It's called: graceful degradation.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
If your app can continue without the data, and the user can do nothing about it, there is no point in telling them. It's called: graceful degradation.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
No, i meant that not all the times data is needed. When it does, it will throw an error if you dont have
-
greetings guys. im building an android application and i have some second thoughts on some error handling case. I have a method that gets data from the internet by calling this method:
public static string StoredDatesList
{
get => Preferences.Get(nameof(StoredDatesList), string.Empty);
set => Preferences.Set(nameof(StoredDatesList), value);
}
public static async Task GetDraws(Uri url, string date)
{
Dictionary StoredDates = new Dictionary();
StoredDates = JsonConvert.DeserializeObject>(StoredDatesList);
var contents = string.Empty;
HttpClient client = new HttpClient();if (StoredDates != null) if (StoredDates.ContainsKey(date)) { contents = StoredDates\[date\]; } else { var current = Connectivity.NetworkAccess; if (current != NetworkAccess.Internet) return null; client = new HttpClient(); contents = await client.GetStringAsync(url); var res2 = JsonConvert.DeserializeObject(contents.ToString()); if (180 == res2.content.Count) { StoredDates.Add(date, contents); StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented); } } else { StoredDates = new Dictionary(); contents = await client.GetStringAsync(url); var res2 = JsonConvert.DeserializeObject(contents.ToString()); if (180 == res2.content.Count) { StoredDates.Add(date, contents); StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented); } } return contents; }
the if statement
current != NetworkAccess.Internet)
checks if internet is available, when internet is not available i return null and i check if the data is null and im displaying a message(error, internet is not available etc). I find this approach very bad and im trying to think how is the proper way to handle this. i cannot show a message t
Slightly off topic perhaps, but you are repeating the same block of code twice and could refactor all of this into a single function and just call that function twice:
contents = await client.GetStringAsync(url); var res2 = JsonConvert.DeserializeObject(contents.ToString()); if (180 == res2.content.Count) { StoredDates.Add(date, contents); StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented); }
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
-
Slightly off topic perhaps, but you are repeating the same block of code twice and could refactor all of this into a single function and just call that function twice:
contents = await client.GetStringAsync(url); var res2 = JsonConvert.DeserializeObject(contents.ToString()); if (180 == res2.content.Count) { StoredDates.Add(date, contents); StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented); }
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
Oh thank you. Any improvement is highly welcome, i have refactor it to:
public static string StoredDatesList
{
get => Preferences.Get(nameof(StoredDatesList), string.Empty);
set => Preferences.Set(nameof(StoredDatesList), value);
}
public static async Task GetDraws(Uri url, string date)
{
var StoredDates = JsonConvert.DeserializeObject>(StoredDatesList);
var contents = string.Empty;
var current = Connectivity.NetworkAccess;
var client = new HttpClient();if (StoredDates != null) if (StoredDates.ContainsKey(date)) { contents = StoredDates\[date\]; } else { if (current != NetworkAccess.Internet) return Helpers.Settings.Common\_Error\_NoInternetConnection; contents = await DownloadResults(url, date, StoredDates, contents, client); } else { if (current != NetworkAccess.Internet) return Helpers.Settings.Common\_Error\_NoInternetConnection; StoredDates = new Dictionary(); contents = await DownloadResults(url, date, StoredDates, contents, client); } return contents; } private static async Task DownloadResults(Uri url, string date, Dictionary StoredDates, string contents, HttpClient client) { contents = await client.GetStringAsync(url); var res2 = JsonConvert.DeserializeObject(contents.ToString()); if (180 == res2.content.Count) { StoredDates.Add(date, contents); StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented); } return contents; }
...
public const string Common_Error_NoInternetConnection = "Error_NoInternetConnection";So i would check every time if the return sting is equal to Common_Error_NoInternetConnection, Does this sounds like a solid idea?