More improvements to Result formatting

This commit is contained in:
Dominion
2023-04-14 12:15:47 -04:00
parent d14a278085
commit a13af24ef5
@@ -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> 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();
}
/// <summary>
/// Formats given <paramref name="propertyErrorDetails"/> into a given <paramref name="stringBuilder"/>.
/// </summary>
/// <param name="propertyErrorDetails">The <see cref="IPropertyErrorDetails"/>.</param>
/// <param name="stringBuilder">The <see cref="StringBuilder"/> to mutate.</param>
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('}');
}
}
/// <summary>
/// Formats given <paramref name="errorDetails"/> into a given <paramref name="stringBuilder"/>.
/// </summary>
/// <param name="errorDetails">The <see cref="IEnumerable{T}"/> of <see cref="IErrorDetails"/>.</param>
/// <param name="stringBuilder">The <see cref="StringBuilder"/> to mutate.</param>
static void FormatErrorDetails(IEnumerable<IErrorDetails> 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(']');
}
}
}