Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VCI-907: Add the parsing of error messages from VirtoCloud #146

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/VirtoCommerce.Build/Cloud/Client/VirtoCloudClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,22 +25,29 @@ public VirtoCloudClient(string baseUrl, string token)

public async Task<string> UpdateEnvironmentAsync(string manifest, string appProject)
{
var content = new Dictionary<string, string>();
content.Add("manifest", manifest);
content.Add("appProject", appProject);
var content = new Dictionary<string, string>
{
{ "manifest", manifest },
{ "appProject", appProject }
};
var response = await _client.SendAsync(new HttpRequestMessage
{
Method = HttpMethod.Put,
RequestUri = new Uri("api/saas/environments/update", UriKind.Relative),
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)
Expand All @@ -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());
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/VirtoCommerce.Build/Cloud/Models/VirtoCloudError.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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<VirtoCloudError>(response);
var message = cloudError?.Message ?? response;
var errors = cloudError?.Errors ?? new List<string>();
return new VirtoCloudError {
Message = message,
Errors = errors
};
}
catch (JsonReaderException)
{
return new VirtoCloudError
{
Message = response
};
}
}
}
}
Loading