Skip to content

Commit

Permalink
Improved error messages + finished all control renamings due to the l…
Browse files Browse the repository at this point in the history
…ast ui change
  • Loading branch information
Yelo420 committed Jan 5, 2024
1 parent 2009124 commit 3262252
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 123 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# GameVault App Changelog

## 1.8.2
Recommended Gamevault Server Version: `v10.0.2`
Recommended Gamevault Server Version: `v10.1.0`
### Changes
- Write permissions of the root folder are now checked on selection
- The scrollbar in the download tab now covers the entire tab
- Improved error messages

## 1.8.1
Recommended Gamevault Server Version: `v10.0.2`
Expand Down
29 changes: 15 additions & 14 deletions gamevault/Helper/LoginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static LoginManager Instance
#endregion
private User? m_User { get; set; }
private LoginState m_LoginState { get; set; }
private string m_LoginMessage { get; set; }
public User? GetCurrentUser()
{
return m_User;
Expand All @@ -53,6 +54,10 @@ public LoginState GetState()
{
return m_LoginState;
}
public string GetLoginMessage()
{
return m_LoginMessage;
}
public void SwitchToOfflineMode()
{
MainWindowViewModel.Instance.OnlineState = System.Windows.Visibility.Visible;
Expand All @@ -70,17 +75,15 @@ public async Task StartupLogin()
string result = WebHelper.GetRequest(@$"{SettingsViewModel.Instance.ServerUrl}/api/users/me");
return JsonSerializer.Deserialize<User>(result);
}
catch (WebException ex)
catch (Exception ex)
{
string code = WebExceptionHelper.GetServerStatusCode(ex);
state = DetermineLoginState(code);
if (state == LoginState.Error)
m_LoginMessage = WebExceptionHelper.TryGetServerMessage(ex);
return null;
}
catch
{
state = LoginState.Error;
return null;
}
}
});
m_User = user;
m_LoginState = state;
Expand All @@ -96,17 +99,15 @@ public async Task<LoginState> ManualLogin(string username, string password)
string result = WebHelper.GetRequest(@$"{SettingsViewModel.Instance.ServerUrl}/api/users/me");
return JsonSerializer.Deserialize<User>(result);
}
catch (WebException ex)
catch (Exception ex)
{
string code = WebExceptionHelper.GetServerStatusCode(ex);
state = DetermineLoginState(code);
if (state == LoginState.Error)
m_LoginMessage = WebExceptionHelper.TryGetServerMessage(ex);
return null;
}
catch
{
state = LoginState.Error;
return null;
}
}
});
m_User = user;
m_LoginState = state;
Expand Down
23 changes: 19 additions & 4 deletions gamevault/Helper/WebExceptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace gamevault.Helper
{
internal class WebExceptionHelper
{
internal static string GetServerMessage(WebException ex)
private static string GetServerMessage(WebException ex)
{
string errMessage = string.Empty;
try
Expand All @@ -23,15 +23,30 @@ internal static string GetServerMessage(WebException ex)
catch { }
return errMessage;
}
internal static string GetServerStatusCode(WebException ex)
/// <summary>
/// Tries to get the response message of the server. Else returns exception message
/// </summary>
internal static string TryGetServerMessage(Exception ex)
{
if (ex is WebException webEx)
{
string msg = GetServerMessage(webEx);
return string.IsNullOrEmpty(msg) ? ex.Message : $"Server responded:{webEx}";
}
return ex.Message;
}
internal static string GetServerStatusCode(Exception ex)
{
if (ex is not WebException webex)
return "";

string errMessage = string.Empty;
try
{
if (ex.Response == null)
if (webex.Response == null)
return string.Empty;

var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
var resp = new StreamReader(webex.Response.GetResponseStream()).ReadToEnd();
JsonObject obj = JsonNode.Parse(resp).AsObject();
errMessage = obj["statusCode"].ToString().Replace("\n", " ").Replace("\r", "");
}
Expand Down
24 changes: 12 additions & 12 deletions gamevault/UserControls/AdminConsoleUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public async Task InitUserList()
return JsonSerializer.Deserialize<User[]>(userList);
});
}
catch (WebException ex)
catch (Exception ex)
{
string msg = WebExceptionHelper.GetServerMessage(ex);
string msg = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
}
Expand All @@ -75,9 +75,9 @@ private void PermissionRole_SelectionChanged(object sender, SelectionChangedEven
WebHelper.Put(@$"{SettingsViewModel.Instance.ServerUrl}/api/users/{selectedUser.ID}", JsonSerializer.Serialize(new User() { Role = selectedUser.Role }));
MainWindowViewModel.Instance.AppBarText = $"Successfully updated permission role of user '{selectedUser.Username}' to '{selectedUser.Role}'";
}
catch (WebException ex)
catch (Exception ex)
{
string msg = WebExceptionHelper.GetServerMessage(ex);
string msg = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
}
Expand All @@ -91,9 +91,9 @@ private void Activated_Toggled(object sender, RoutedEventArgs e)
string state = selectedUser.Activated == true ? "activated" : "deactivated";
MainWindowViewModel.Instance.AppBarText = $"Successfully {state} user '{selectedUser.Username}'";
}
catch (WebException ex)
catch (Exception ex)
{
string msg = WebExceptionHelper.GetServerMessage(ex);
string msg = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
}
Expand All @@ -120,9 +120,9 @@ await Task.Run(async () =>
await InitUserList();
}
}
catch (WebException ex)
catch (Exception ex)
{
string msg = WebExceptionHelper.GetServerMessage(ex);
string msg = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
});
Expand Down Expand Up @@ -158,10 +158,10 @@ await Task.Run(() =>
WebHelper.Put(@$"{SettingsViewModel.Instance.ServerUrl}/api/users/{selectedUser.ID}", JsonSerializer.Serialize(selectedUser));
MainWindowViewModel.Instance.AppBarText = "Sucessfully saved user changes";
}
catch (WebException ex)
catch (Exception ex)
{
error = true;
string msg = WebExceptionHelper.GetServerMessage(ex);
string msg = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
});
Expand Down Expand Up @@ -198,9 +198,9 @@ await Task.Run(() =>
WebHelper.Put(@$"{SettingsViewModel.Instance.ServerUrl}/api/files/reindex", string.Empty);
MainWindowViewModel.Instance.AppBarText = "Sucessfully reindexed games";
}
catch (WebException ex)
catch (Exception ex)
{
string msg = WebExceptionHelper.GetServerMessage(ex);
string msg = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
});
Expand Down
8 changes: 2 additions & 6 deletions gamevault/UserControls/CommunityUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private async void Users_SelectionChanged(object sender, SelectionChangedEventAr
uiSortBy.SelectedIndex = 2;
}
}
catch (Exception ex) { MainWindowViewModel.Instance.AppBarText = "Could not connect to server"; }
catch (Exception ex) { MainWindowViewModel.Instance.AppBarText = WebExceptionHelper.TryGetServerMessage(ex); }
}
private User[] BringCurrentUserToTop(User[] users)
{
Expand Down Expand Up @@ -230,13 +230,9 @@ private async void DeleteProgress_Click(object sender, System.Windows.Input.Mous
MainWindowViewModel.Instance.AppBarText = $"Successfully deleted progress";
}
}
catch (WebException webex)
{
MainWindowViewModel.Instance.AppBarText = $"Could not delete the Progress. {WebExceptionHelper.GetServerMessage(webex)}";
}
catch (Exception ex)
{
MainWindowViewModel.Instance.AppBarText = $"Could not delete the Progress. {ex.Message}";
MainWindowViewModel.Instance.AppBarText = $"Could not delete. {WebExceptionHelper.TryGetServerMessage(ex)}";
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gamevault/UserControls/DownloadsUserControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</TextBlock.Style>
</TextBlock>
<Grid Grid.Row="1">
<ScrollViewer Grid.Column="1" Margin="10,10,0,10" VerticalScrollBarVisibility="Auto">
<ScrollViewer Grid.Column="1" VerticalAlignment="Top" Margin="10,10,0,10" VerticalScrollBarVisibility="Auto">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
Expand Down
17 changes: 6 additions & 11 deletions gamevault/UserControls/DownloadsUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,15 @@ public async Task RestoreDownloadedGames()
}
return offlineCacheGames.ToArray();
}
catch (WebException exWeb)
{
string webMsg = WebExceptionHelper.GetServerMessage(exWeb);
if (webMsg == string.Empty) webMsg = "Could not connect to server";
MainWindowViewModel.Instance.AppBarText = webMsg;
}
catch (JsonException exJson)
{
MainWindowViewModel.Instance.AppBarText = exJson.Message;
}
catch (FormatException exFormat)
{
MainWindowViewModel.Instance.AppBarText = "The offline cache is corrupted";
}
catch (Exception ex)
{
string webMsg = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = webMsg;
}
return null;
});
if (games == null)
Expand All @@ -105,7 +100,7 @@ public async Task TryStartDownload(Game game)
{
if (LoginManager.Instance.IsLoggedIn() == false)
{
MainWindowViewModel.Instance.AppBarText = "Could not connect to server";
MainWindowViewModel.Instance.AppBarText = "You are not logged in or offline";
return;
}
if (SettingsViewModel.Instance.RootPath == string.Empty)
Expand Down
23 changes: 6 additions & 17 deletions gamevault/UserControls/GameSettingsUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,9 @@ await Task.Run(() =>
success = true;
MainWindowViewModel.Instance.AppBarText = "Successfully updated image";
}
catch (WebException ex)
{
string msg = WebExceptionHelper.GetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
catch (Exception ex)
{
MainWindowViewModel.Instance.AppBarText = ex.Message;
MainWindowViewModel.Instance.AppBarText = WebExceptionHelper.TryGetServerMessage(ex);
}
});
//Update Data Context for Library. So that the images are also refreshed there directly
Expand All @@ -632,14 +627,9 @@ await Task.Run(() =>
}
}
}
catch (WebException ex)
{
string msg = WebExceptionHelper.GetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
catch (Exception ex)
{
MainWindowViewModel.Instance.AppBarText = ex.Message;
MainWindowViewModel.Instance.AppBarText = WebExceptionHelper.TryGetServerMessage(ex);
}
}
private void Image_Paste(object sender, KeyEventArgs e)
Expand Down Expand Up @@ -721,9 +711,9 @@ await Task.Run(() =>
WebHelper.Put(@$"{SettingsViewModel.Instance.ServerUrl}/api/rawg/{ViewModel.Game.ID}/recache", string.Empty);
MainWindowViewModel.Instance.AppBarText = $"Sucessfully re-cached {ViewModel.Game.Title}";
}
catch (WebException ex)
catch (Exception ex)
{
string msg = WebExceptionHelper.GetServerMessage(ex);
string msg = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
});
Expand All @@ -743,10 +733,9 @@ await Task.Run(() =>
MainWindowViewModel.Instance.AppBarText = $"Successfully re-mapped {ViewModel.Game.Title}";
}
catch (WebException ex)
catch (Exception ex)
{
string errMessage = WebExceptionHelper.GetServerMessage(ex);
if (errMessage == string.Empty) { errMessage = "Failed to re-map game"; }
string errMessage = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = errMessage;
}
});
Expand Down
9 changes: 2 additions & 7 deletions gamevault/UserControls/GameViewUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,11 @@ await Task.Run(() =>
{
WebHelper.Put(@$"{SettingsViewModel.Instance.ServerUrl}/api/progresses/user/{LoginManager.Instance.GetCurrentUser().ID}/game/{gameID}", System.Text.Json.JsonSerializer.Serialize(new Progress() { State = ViewModel.Progress.State }));
}
catch (WebException webEx)
catch (Exception ex)
{
string msg = WebExceptionHelper.GetServerMessage(webEx);
if (msg == string.Empty)
{
msg = "Could not connect to server";
}
string msg = WebExceptionHelper.TryGetServerMessage(ex);
MainWindowViewModel.Instance.AppBarText = msg;
}
catch { }
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions gamevault/UserControls/GeneralControls/TagSelector.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ await Task.Run(() =>
Selection.Genres => JsonSerializer.Deserialize<Genre_Tag[]>(result).Where(x => x.Name.Contains(debounceTimer.Data, StringComparison.OrdinalIgnoreCase)).ToArray()
};
}
catch
catch (Exception ex)
{
MainWindowViewModel.Instance.AppBarText = "Could not connect to server";
MainWindowViewModel.Instance.AppBarText = WebExceptionHelper.TryGetServerMessage(ex);
}
});
}
Expand Down
12 changes: 1 addition & 11 deletions gamevault/UserControls/InstallUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,9 @@ public async Task RestoreInstalledGames()
}
}
}
catch (WebException exWeb)
{
MainWindowViewModel.Instance.AppBarText = "Could not connect to server";
return null;
}
catch (JsonException exJson)
{
MainWindowViewModel.Instance.AppBarText = exJson.Message;
return null;
}
catch (Exception ex)
{
MainWindowViewModel.Instance.AppBarText = ex.Message;
MainWindowViewModel.Instance.AppBarText = WebExceptionHelper.TryGetServerMessage(ex);
return null;
}
}
Expand Down
Loading

0 comments on commit 3262252

Please sign in to comment.