diff --git a/src/VirtoCommerce.Build/Cloud/Client/VirtoCloudClient.cs b/src/VirtoCommerce.Build/Cloud/Client/VirtoCloudClient.cs index c937660..43ad886 100644 --- a/src/VirtoCommerce.Build/Cloud/Client/VirtoCloudClient.cs +++ b/src/VirtoCommerce.Build/Cloud/Client/VirtoCloudClient.cs @@ -3,7 +3,10 @@ using System.Net.Http; using System.Text; using System.Threading.Tasks; +using Cloud.Models; +using Newtonsoft.Json; using Nuke.Common; +using Nuke.Common.IO; using Nuke.Common.Utilities; using VirtoCloud.Client.Model; @@ -22,9 +25,11 @@ public VirtoCloudClient(string baseUrl, string token) public async Task UpdateEnvironmentAsync(string manifest, string appProject) { - var content = new Dictionary(); - content.Add("manifest", manifest); - content.Add("appProject", appProject); + var content = new Dictionary + { + { "manifest", manifest }, + { "appProject", appProject } + }; var response = await _client.SendAsync(new HttpRequestMessage { Method = HttpMethod.Put, @@ -32,12 +37,17 @@ public async Task UpdateEnvironmentAsync(string manifest, string appProj Content = new FormUrlEncodedContent(content) }); + var responseContent = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { - Assert.Fail($"{response.ReasonPhrase}: {await response.Content.ReadAsStringAsync()}"); + var error = VirtoCloudError.FromStringResponse(responseContent); + Assert.Fail(error.GetErrorMessage()); } - return await response.Content.ReadAsStringAsync(); + Serilog.Log.Information(responseContent); + Serilog.Log.Information("Environment updated successfully"); + + return responseContent; } public async Task UpdateEnvironmentAsync(CloudEnvironment environment) @@ -47,7 +57,9 @@ public async Task UpdateEnvironmentAsync(CloudEnvironment environment) var response = await _client.PutAsync(new Uri("api/saas/environments", UriKind.Relative), content); if (!response.IsSuccessStatusCode) { - Assert.Fail($"{response.ReasonPhrase}: {await response.Content.ReadAsStringAsync()}"); + var responseContent = await response.Content.ReadAsStringAsync(); + var error = VirtoCloudError.FromStringResponse(responseContent); + Assert.Fail(error.GetErrorMessage()); } } diff --git a/src/VirtoCommerce.Build/Cloud/Models/VirtoCloudError.cs b/src/VirtoCommerce.Build/Cloud/Models/VirtoCloudError.cs new file mode 100644 index 0000000..a837c13 --- /dev/null +++ b/src/VirtoCommerce.Build/Cloud/Models/VirtoCloudError.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Newtonsoft.Json; +using Nuke.Common; + +namespace Cloud.Models +{ + public class VirtoCloudError + { + public string Message { get; set; } + public List Errors { get; set; } + + public string GetErrorMessage() + { + var message = Message; + if (Errors?.Count > 0) + { + message += Environment.NewLine + string.Join(Environment.NewLine, Errors); + } + + return message; + } + + public static VirtoCloudError FromStringResponse(string response) + { + try + { + var cloudError = JsonConvert.DeserializeObject(response); + var message = cloudError?.Message ?? response; + var errors = cloudError?.Errors ?? new List(); + return new VirtoCloudError { + Message = message, + Errors = errors + }; + } + catch (JsonReaderException) + { + return new VirtoCloudError + { + Message = response + }; + } + } + } +}