diff --git a/Flawless.Client/UIHelper.cs b/Flawless.Client/UIHelper.cs index 9583e51..b510bcf 100644 --- a/Flawless.Client/UIHelper.cs +++ b/Flawless.Client/UIHelper.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Reactive.Disposables; using System.Threading.Tasks; using Avalonia.Controls.ApplicationLifetimes; @@ -6,6 +7,8 @@ using Avalonia.Controls.Notifications; using Avalonia.VisualTree; using Flawless.Client.ViewModels.ModalBox; using Flawless.Client.Views.ModalBox; +using Newtonsoft.Json; +using Refit; using Ursa.Controls; using Notification = Ursa.Controls.Notification; using WindowNotificationManager = Ursa.Controls.WindowNotificationManager; @@ -14,12 +17,21 @@ namespace Flawless.Client; public static class UIHelper { + [Serializable] + private struct FailureData + { + public string? Message; + + public string? Failure; + } private static WindowNotificationManager _notificationManager = null!; private static LoadingContainer _loadingContainer = null!; private static int _loadingReferenceCount = 0; + + private static JsonSerializer _serializer = JsonSerializer.Create(); public static WindowNotificationManager Notify { @@ -71,18 +83,47 @@ public static class UIHelper - public static void NotifyError(Exception ex) + public static void NotifyError(Exception ex, string? rewriteTitle = null) { try { - var content = ex.ToString(); + Console.Error.Write(ex); + + string content; + string title; + if (ex is ApiException aex) + { + title = rewriteTitle ?? $"API Error: {aex.StatusCode.ToString()}"; + if (string.IsNullOrEmpty(aex.Content)) + { + content = aex.Message; + } + else + { + try + { + var result = _serializer.Deserialize(new JsonTextReader(new StringReader(aex.Content))); + content = result.Message ?? aex.Message; + } + catch (Exception _) + { + content = aex.Content; + } + } + } + else + { + title = rewriteTitle ?? $"Error: {ex.GetType().Name}"; + content = ex.ToString(); + } + if (content.Length > 100) content = content.Substring(0, 100) + "..."; - var nf = new Notification(ex.GetType().Name, content, NotificationType.Error); + var nf = new Notification(title, content, NotificationType.Error); Notify.Show(nf); } catch (Exception e) { - Console.WriteLine("Can not notify error to users: " + e); + Console.Error.Write("Can not notify error to users: " + e); } } diff --git a/Flawless.Client/Views/SettingView.axaml b/Flawless.Client/Views/SettingView.axaml index e1552d2..a0eae50 100644 --- a/Flawless.Client/Views/SettingView.axaml +++ b/Flawless.Client/Views/SettingView.axaml @@ -81,8 +81,20 @@ - - + + + +