1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 20:22:55 +08:00

Make AuthenticateWithLogin throw instead of return a bool success status

This commit is contained in:
Dean Herbert 2021-10-04 15:40:00 +09:00
parent 3d71576fbe
commit 5aaafce597
2 changed files with 32 additions and 7 deletions

View File

@ -6,7 +6,7 @@ using osu.Framework.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osuTK.Graphics; using osuTK.Graphics;
namespace osu.Game.Overlays.AccountCreation namespace osu.Game.Graphics
{ {
public class ErrorTextFlowContainer : OsuTextFlowContainer public class ErrorTextFlowContainer : OsuTextFlowContainer
{ {

View File

@ -1,8 +1,10 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Net.Http; using System.Net.Http;
using Newtonsoft.Json;
using osu.Framework.Bindables; using osu.Framework.Bindables;
namespace osu.Game.Online.API namespace osu.Game.Online.API
@ -32,10 +34,10 @@ namespace osu.Game.Online.API
this.endpoint = endpoint; this.endpoint = endpoint;
} }
internal bool AuthenticateWithLogin(string username, string password) internal void AuthenticateWithLogin(string username, string password)
{ {
if (string.IsNullOrEmpty(username)) return false; if (string.IsNullOrEmpty(username)) throw new ArgumentException("Missing username.");
if (string.IsNullOrEmpty(password)) return false; if (string.IsNullOrEmpty(password)) throw new ArgumentException("Missing password.");
using (var req = new AccessTokenRequestPassword(username, password) using (var req = new AccessTokenRequestPassword(username, password)
{ {
@ -49,13 +51,27 @@ namespace osu.Game.Online.API
{ {
req.Perform(); req.Perform();
} }
catch catch (Exception ex)
{ {
return false; Token.Value = null;
var throwableException = ex;
try
{
// attempt to decode a displayable error string.
var error = JsonConvert.DeserializeObject<OAuthError>(req.GetResponseString() ?? string.Empty);
if (error != null)
throwableException = new APIException(error.Message, ex);
}
catch
{
}
throw throwableException;
} }
Token.Value = req.ResponseObject; Token.Value = req.ResponseObject;
return true;
} }
} }
@ -182,5 +198,14 @@ namespace osu.Game.Online.API
base.PrePerform(); base.PrePerform();
} }
} }
private class OAuthError
{
[JsonProperty("error")]
public string ErrorType { get; set; }
[JsonProperty("hint")]
public string Message { get; set; }
}
} }
} }