diff --git a/src/Core/Exceptions/ApiException.cs b/src/Core/Exceptions/ApiException.cs index 493900137..9d0a774b4 100644 --- a/src/Core/Exceptions/ApiException.cs +++ b/src/Core/Exceptions/ApiException.cs @@ -16,5 +16,7 @@ namespace Bit.Core.Exceptions } public ErrorResponse Error { get; set; } + + public override string Message => Error?.GetFullMessage() ?? base.Message; } } diff --git a/src/Core/Models/Response/ErrorResponse.cs b/src/Core/Models/Response/ErrorResponse.cs index 71cde4317..7d43ed1cc 100644 --- a/src/Core/Models/Response/ErrorResponse.cs +++ b/src/Core/Models/Response/ErrorResponse.cs @@ -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 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; }