From a13af24ef580e511508cd7471ef9d86c65420d8b Mon Sep 17 00:00:00 2001 From: Dominion Date: Fri, 14 Apr 2023 12:15:47 -0400 Subject: [PATCH] More improvements to Result formatting --- .../Extensions/ResultExtensions.cs | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/Tgstation.Server.Host/Extensions/ResultExtensions.cs b/src/Tgstation.Server.Host/Extensions/ResultExtensions.cs index 315972920a..e91128f515 100644 --- a/src/Tgstation.Server.Host/Extensions/ResultExtensions.cs +++ b/src/Tgstation.Server.Host/Extensions/ResultExtensions.cs @@ -1,6 +1,10 @@ using System; +using System.Collections.Generic; using System.Text; +using Remora.Discord.API.Abstractions.Objects; +using Remora.Discord.API.Objects; +using Remora.Rest.Results; using Remora.Results; namespace Tgstation.Server.Host.Extensions @@ -28,6 +32,39 @@ namespace Tgstation.Server.Host.Extensions if (result.Error != null) { stringBuilder.Append(result.Error.Message); + if (result.Error is RestResultError restError) + { + stringBuilder.Append(" ("); + if (restError.Error != null) + { + stringBuilder.Append(restError.Error.Code); + stringBuilder.Append(": "); + stringBuilder.Append(restError.Error.Message); + stringBuilder.Append('|'); + } + + stringBuilder.Append(restError.Message); + if ((restError.Error?.Errors.HasValue ?? false) && restError.Error.Errors.Value.Count > 0) + { + stringBuilder.Append(" ("); + foreach (var error in restError.Error.Errors.Value) + { + stringBuilder.Append(error.Key); + stringBuilder.Append(':'); + if (error.Value.IsT0) + { + FormatErrorDetails(error.Value.AsT0, stringBuilder); + } + else + FormatErrorDetails(error.Value.AsT1, stringBuilder); + stringBuilder.Append(','); + } + + stringBuilder.Remove(stringBuilder.Length - 1, 1); + } + + stringBuilder.Append(')'); + } } if (result.Inner != null) @@ -41,5 +78,61 @@ namespace Tgstation.Server.Host.Extensions return stringBuilder.ToString(); } + + /// + /// Formats given into a given . + /// + /// The . + /// The to mutate. + static void FormatErrorDetails(IPropertyErrorDetails propertyErrorDetails, StringBuilder stringBuilder) + { + if (propertyErrorDetails == null) + return; + + FormatErrorDetails(propertyErrorDetails.Errors, stringBuilder); + + if (propertyErrorDetails.Errors != null && propertyErrorDetails.MemberErrors != null) + { + stringBuilder.Append(','); + } + + if (propertyErrorDetails.MemberErrors != null) + { + stringBuilder.Append('{'); + foreach (var error in propertyErrorDetails.MemberErrors) + { + stringBuilder.Append(error.Key); + stringBuilder.Append(':'); + FormatErrorDetails(error.Value, stringBuilder); + stringBuilder.Append(','); + } + + stringBuilder.Remove(stringBuilder.Length - 1, 1); + stringBuilder.Append('}'); + } + } + + /// + /// Formats given into a given . + /// + /// The of . + /// The to mutate. + static void FormatErrorDetails(IEnumerable errorDetails, StringBuilder stringBuilder) + { + if (errorDetails == null) + return; + + stringBuilder.Append('['); + foreach (var error in errorDetails) + { + stringBuilder.Append(error.Code); + stringBuilder.Append(':'); + stringBuilder.Append(error.Message); + stringBuilder.Append(','); + } + + stringBuilder.Remove(stringBuilder.Length - 1, 1); + stringBuilder.Append(']'); + } } }