From 4f4953206e22fdd5e2c421b55af7335cf06b797f Mon Sep 17 00:00:00 2001 From: Federico Maccaroni Date: Thu, 12 Jan 2023 10:31:27 -0300 Subject: [PATCH] [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() --- src/Core/Exceptions/ApiException.cs | 2 ++ src/Core/Models/Response/ErrorResponse.cs | 27 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) 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; }