Skip to content

Commit

Permalink
Better error logging.
Browse files Browse the repository at this point in the history
Fixed a bug with Images getting zipped as string and not as a byte array.
  • Loading branch information
janstaelensskyline committed Oct 17, 2024
1 parent b60a9f8 commit a21d8e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
9 changes: 4 additions & 5 deletions CICD.Tools.CatalogUpload.Lib/CatalogMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,9 @@ public async Task<byte[]> ToCatalogZipAsync(IFileSystem fs, ISerializer serializ
{
var imageEntry = archive.CreateEntry($"Images/{fs.Path.GetFileName(imageFile)}");
using var entryStream = imageEntry.Open();
var readmeContent = fs.File.ReadAllText(imageFile); // Get the file content as a string
using var streamWriter = new StreamWriter(entryStream);
await streamWriter.WriteAsync(readmeContent).ConfigureAwait(false);
await streamWriter.FlushAsync().ConfigureAwait(false); // Ensure all content is written
var imageBytes = fs.File.ReadAllBytes(imageFile); // Get the file content as binary data
await entryStream.WriteAsync(imageBytes, 0, imageBytes.Length).ConfigureAwait(false); // Write binary data
await entryStream.FlushAsync().ConfigureAwait(false); // Ensure all content is written
}
}
}
Expand All @@ -371,7 +370,7 @@ public async Task<byte[]> ToCatalogZipAsync(IFileSystem fs, ISerializer serializ
}

private static string RecursiveFindClosestCatalogYaml(IFileSystem fs, string directory, int maxRecurse)
{
{
if (maxRecurse-- <= 0)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,21 +388,9 @@ public async Task ToCatalogZipAsync_ShouldCreateValidZipFile()
// Check Images folder and files exist
var image1Entry = zip.GetEntry("Images/image1.png");
image1Entry.Should().NotBeNull();
if (image1Entry == null) return;
using (var reader = new StreamReader(image1Entry.Open()))
{
var content = await reader.ReadToEndAsync();
content.Should().Be("Image1 content");
}

var image2Entry = zip.GetEntry("Images/image2.png");
image2Entry.Should().NotBeNull();
if (image2Entry == null) return;
using (var reader = new StreamReader(image2Entry.Open()))
{
var content = await reader.ReadToEndAsync();
content.Should().Be("Image2 content");
}
}

[TestMethod]
Expand Down Expand Up @@ -459,12 +447,6 @@ public async Task ToCatalogZipAsync_NoReadmeFile_ShouldNotIncludeReadme()
// Check Images folder and files exist
var image1Entry = zip.GetEntry("Images/image1.png");
image1Entry.Should().NotBeNull();
if (image1Entry == null) return;
using (var reader = new StreamReader(image1Entry.Open()))
{
var content = await reader.ReadToEndAsync();
content.Should().Be("Image1 content");
}
}

[TestMethod]
Expand Down
33 changes: 19 additions & 14 deletions CICD.Tools.CatalogUpload/Uploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task<int> ProcessVolatile(string pathToArtifact, string dmCatalogTo
catch (Exception e)
{
devopsMetricsMessage += "|" + "Status:Fail-" + e.Message;
logger.LogError(e, "An error occurred during processing");
logger.LogError(e, $"An error occurred during processing: {e}");
return 1;
}
finally
Expand Down Expand Up @@ -120,7 +120,7 @@ public async Task<int> ProcessWithRegistrationAsync(string dmCatalogToken, strin
catch (Exception e)
{
devopsMetricsMessage += "|" + "Status:Fail-" + e.Message;
logger.LogError(e, "An error occurred during processing");
logger.LogError(e, $"An error occurred during processing: {e}");
return 1;
}
finally
Expand Down Expand Up @@ -167,7 +167,7 @@ public async Task<int> ProcessYmlRegistrationAsync(string dmCatalogToken, string
catch (Exception e)
{
devopsMetricsMessage += "|" + "Status:Fail-" + e.Message;
logger.LogError(e, "An error occurred during registration");
logger.LogError(e, $"An error occurred during registration {e}");
return 1;
}
finally
Expand Down Expand Up @@ -209,38 +209,43 @@ private void ApplyOptionalArguments(OptionalRegistrationArguments optionalArgume

if (optionalArguments.UriSourceCode != null)
{
logger.LogDebug($"Overriding SourceCodeUri from '{metaData.SourceCodeUri}' to '{optionalArguments.UriSourceCode}'");
logger.LogDebug($"Overriding SourceCodeUri from '{metaData.SourceCodeUri}' to '{optionalArguments.UriSourceCode.Trim()}'");
metaData.SourceCodeUri = optionalArguments.UriSourceCode.Trim();
}

if (optionalArguments.OverrideVersion != null)
{
logger.LogDebug($"Overriding Version from '{metaData.Version.Value}' to '{optionalArguments.OverrideVersion}'");
metaData.Version.Value = optionalArguments.OverrideVersion.Trim();
string newValue = optionalArguments.OverrideVersion.Trim();
logger.LogDebug($"Overriding Version from '{metaData.Version.Value}' to '{newValue}'");
metaData.Version.Value = newValue;
}

if (optionalArguments.Branch != null)
{
logger.LogDebug($"Overriding Branch from '{metaData.Version.Branch}' to '{optionalArguments.Branch}'");
metaData.Version.Branch = optionalArguments.Branch.Trim();
string newValue = optionalArguments.Branch.Trim();
logger.LogDebug($"Overriding Branch from '{metaData.Version.Branch}' to '{newValue}'");
metaData.Version.Branch = newValue;
}

if (optionalArguments.CommitterMail != null)
{
logger.LogDebug($"Overriding CommitterMail from '{metaData.Version.CommitterMail}' to '{optionalArguments.CommitterMail}'");
metaData.Version.CommitterMail = optionalArguments.CommitterMail.Trim();
string newValue = optionalArguments.CommitterMail.Trim();
logger.LogDebug($"Overriding CommitterMail from '{metaData.Version.CommitterMail}' to '{newValue}'");
metaData.Version.CommitterMail = newValue;
}

if (optionalArguments.ReleaseUri != null)
{
logger.LogDebug($"Overriding ReleaseUri from '{metaData.Version.ReleaseUri}' to '{optionalArguments.ReleaseUri}'");
metaData.Version.ReleaseUri = optionalArguments.ReleaseUri.Trim();
string newValue = optionalArguments.ReleaseUri.Trim();
logger.LogDebug($"Overriding ReleaseUri from '{metaData.Version.ReleaseUri}' to '{newValue}'");
metaData.Version.ReleaseUri = newValue;
}

if (optionalArguments.CatalogIdentifier != null)
{
logger.LogDebug($"Overriding CatalogIdentifier from '{metaData.CatalogIdentifier}' to '{optionalArguments.CatalogIdentifier}'");
metaData.CatalogIdentifier = optionalArguments.CatalogIdentifier.Trim();
string newValue = optionalArguments.CatalogIdentifier.Trim();
logger.LogDebug($"Overriding CatalogIdentifier from '{metaData.CatalogIdentifier}' to '{newValue}'");
metaData.CatalogIdentifier = newValue;
}
}
}
Expand Down

0 comments on commit a21d8e2

Please sign in to comment.