Skip to content

Commit

Permalink
VCI-907: Add the parsing of error messages from VirtoCloud (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
krankenbro authored Aug 21, 2024
1 parent 5cab7a1 commit 589f1fd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
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
};
}
}
}
}

0 comments on commit 589f1fd

Please sign in to comment.