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

Throw an exception with a message when unable to read corrupted .nupkg.metadata #6077

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

Nigusu-Allehu
Copy link
Contributor

@Nigusu-Allehu Nigusu-Allehu commented Oct 2, 2024

Bug

Fixes: NuGet/Home#13763

Description

Previously when nuget comes across a nupkg.metadata that is corrupt, it logs a warning and rethrows an exception. The exception that is rethrown does not have a message that specifies the path of the corrupt metadata. This PR does the following

  • Remove the warning being logged. We do not need to log and then throw the exception. It will result in double logging
  • Adds a specific message to the exception that clarifies which file is causing the problem

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.

@Nigusu-Allehu Nigusu-Allehu requested a review from a team as a code owner October 2, 2024 18:35
@Nigusu-Allehu Nigusu-Allehu self-assigned this Oct 2, 2024

throw;
throw new Exception(message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Exception(message);
throw new Exception(message, ex);

It's good practise to always retain the original exception as an inner exception.


throw;
throw new Exception(message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also good practice to throw specific exceptions, not use the base Exception type. Perhaps an ArgumentException can be used. From a quick look at the SystemException docs' list of derived types, I don't see anything else that looks better, but I only had a very quick look.

@@ -53,11 +53,11 @@ public static NupkgMetadataFile Read(TextReader reader, ILogger log, string path
}
catch (Exception ex)
{
log.LogWarning(string.Format(CultureInfo.CurrentCulture,
var message = string.Format(CultureInfo.CurrentCulture,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mentioned we don't want to double log, can you please show an example of how it looked like vs how it looks like now?

The other thing I'm curious about is how it looks with your other change about failing restore gracefully.
Should this change be retested with that one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. This was more of a conclusion I reached based on the code. We have log.logWarning statement followed by an throw statement. However, after manually testing the scenario, I did not see any double logs in the console.

@jebriede jebriede marked this pull request as draft October 9, 2024 22:25
@microsoft-github-policy-service microsoft-github-policy-service bot added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Oct 17, 2024

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 7 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

@dotnet-policy-service dotnet-policy-service bot removed the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Report the path when unable to read corrupted .nupkg.metadata
4 participants