[EC-469] Improve ApiException message (#2288)

* EC-469 Improve ApiException message to have the validation errors and message provided by the ErrorResponse

* EC-469 Updated default message format for ErrorResponse GetFullMessage()
This commit is contained in:
Federico Maccaroni 2023-01-12 10:31:27 -03:00 committed by GitHub
parent f772ee7068
commit 4f4953206e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -16,5 +16,7 @@ namespace Bit.Core.Exceptions
}
public ErrorResponse Error { get; set; }
public override string Message => Error?.GetFullMessage() ?? base.Message;
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
namespace Bit.Core.Models.Response
@ -67,6 +68,32 @@ namespace Bit.Core.Models.Response
return Message;
}
public string GetFullMessage()
{
string GetDefaultMessage() => $"{(int)StatusCode} {StatusCode}. {Message}";
if (ValidationErrors is null)
{
return GetDefaultMessage();
}
var valErrors = ValidationErrors.Where(e => e.Value != null && e.Value.Any()).ToList();
if (!valErrors.Any())
{
return GetDefaultMessage();
}
string GetFullError(string key, List<string> errors)
{
return new StringBuilder()
.Append($"[{key}]: ")
.Append(string.Join("; ", errors))
.ToString();
};
return $"{(int)StatusCode} {StatusCode}. {string.Join(Environment.NewLine, valErrors.Select(ve => GetFullError(ve.Key, ve.Value)))}";
}
private class ErrorModel
{
public string Message { get; set; }