From 58df6930b279791e6cad547718d4182bd91aa22c Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 12 Nov 2019 19:34:20 +0900 Subject: [PATCH] Get error message from server --- osu.Game/Online/API/APIRequest.cs | 23 +++++++++++++++++++ .../BeatmapSet/Buttons/FavouriteButton.cs | 13 ++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/osu.Game/Online/API/APIRequest.cs b/osu.Game/Online/API/APIRequest.cs index 4f613d5c3c..ea0d50511f 100644 --- a/osu.Game/Online/API/APIRequest.cs +++ b/osu.Game/Online/API/APIRequest.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using Newtonsoft.Json; using osu.Framework.IO.Network; using osu.Framework.Logging; @@ -112,6 +113,22 @@ namespace osu.Game.Online.API cancelled = true; WebRequest?.Abort(); + string responseString = WebRequest?.ResponseString; + + if (!string.IsNullOrEmpty(responseString)) + { + try + { + // attempt to decode a displayable error string. + var error = JsonConvert.DeserializeObject(responseString); + if (error != null) + e = new Exception(error.ErrorMessage, e); + } + catch + { + } + } + Logger.Log($@"Failing request {this} ({e})", LoggingTarget.Network); pendingFailure = () => Failure?.Invoke(e); checkAndScheduleFailure(); @@ -129,6 +146,12 @@ namespace osu.Game.Online.API pendingFailure = null; return true; } + + private class DisplayableError + { + [JsonProperty("error")] + public string ErrorMessage; + } } public delegate void APIFailureHandler(Exception e); diff --git a/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs b/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs index e2bc1ee008..f059e06214 100644 --- a/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs +++ b/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs @@ -71,15 +71,12 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons request.Success += () => favourited.Value = !favourited.Value; request.Failure += exception => { - if (exception.Message == "UnprocessableEntity") + notifications.Post(new SimpleNotification { - notifications.Post(new SimpleNotification - { - Text = @"You have too many favourited beatmaps! Please unfavourite some before trying again.", - Icon = FontAwesome.Solid.Times, - }); - loading.Hide(); - } + Text = exception.Message, + Icon = FontAwesome.Solid.Times, + }); + loading.Hide(); }; api.Queue(request); };