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

Detect empty V2 server responses at ServerApiCall level instead of ResponseUtil level #1358

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d71b847
detect count of 0 from V2 server response and error handle there inst…
anamnavi Aug 11, 2023
24cba30
fix error Ids of tests
anamnavi Aug 11, 2023
9b59e87
fix errors
anamnavi Aug 11, 2023
24499ac
update NuGet.Common
anamnavi Aug 11, 2023
f16de43
add inlineCount parameter to request Url
anamnavi Aug 11, 2023
f4d21cf
remove prior V2 empty response handling from InstallHelper
anamnavi Aug 12, 2023
0bf3101
Merge branch 'master' of https://github.com/anamnavi/PowerShellGet in…
anamnavi Aug 12, 2023
b05640d
use PackageNotFound errorIds for find non globbing scenarios
anamnavi Aug 12, 2023
d72e4ca
fix error messaging for install when package could not be installed f…
anamnavi Aug 12, 2023
cae88df
fix error Ids of tests
anamnavi Aug 12, 2023
9d55bbb
fix error id for tests
anamnavi Aug 12, 2023
19b0be3
fix error Ids of LocalServerApiCalls
anamnavi Aug 12, 2023
86167cb
fix error Ids of LocalServerApiCalls
anamnavi Aug 12, 2023
9a7feec
add period at end of error msg
anamnavi Aug 14, 2023
0a2ad5a
set response string to empty when count is 0
anamnavi Aug 14, 2023
84f8988
remove unncessary import and console.writeline statement
anamnavi Aug 14, 2023
bf015da
add period at end of error message
anamnavi Aug 14, 2023
ce4c07d
Update src/code/V2ServerAPICalls.cs
anamnavi Aug 14, 2023
611ec12
Update src/code/V2ServerAPICalls.cs
anamnavi Aug 14, 2023
fba9898
Update src/code/V2ServerAPICalls.cs
anamnavi Aug 14, 2023
46b8f08
Update src/code/V2ServerAPICalls.cs
anamnavi Aug 14, 2023
9ad9749
Update src/code/V2ServerAPICalls.cs
anamnavi Aug 14, 2023
9daa039
Update src/code/V2ServerAPICalls.cs
anamnavi Aug 14, 2023
2dfa31e
Update src/code/V2ServerAPICalls.cs
anamnavi Aug 14, 2023
4b7d78c
Update src/code/V2ServerAPICalls.cs
anamnavi Aug 14, 2023
0dd0a03
Merge branch 'master' of https://github.com/anamnavi/PowerShellGet in…
anamnavi Aug 14, 2023
88296e8
Merge branch 'fix-v2server-emptyresponses-errorhandling' of https://g…
anamnavi Aug 14, 2023
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
102 changes: 26 additions & 76 deletions src/code/FindHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,21 +628,13 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R

foreach (PSResourceResult currentResult in currentResponseUtil.ConvertToPSResourceResult(responseResults: responses))
{
// This currently catches for V2ServerApiCalls where the package was not found
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
if (shouldReportErrorForEachRepo)
{
_cmdletPassedIn.WriteError(new ErrorRecord(
new ResourceNotFoundException($"Package '{pkgName}' could not be found in repository '{repository.Name}", currentResult.exception),
"FindAllConvertToPSResourceFailure",
ErrorCategory.ObjectNotFound,
this));
}
else
{
_cmdletPassedIn.WriteVerbose($"Package '{pkgName}' could not be found in repository '{repository.Name}");
}
_cmdletPassedIn.WriteError(new ErrorRecord(
currentResult.exception,
"FindAllConvertToPSResourceFailure",
ErrorCategory.InvalidResult,
this));

continue;
}
Expand Down Expand Up @@ -688,24 +680,13 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R

foreach (PSResourceResult currentResult in currentResponseUtil.ConvertToPSResourceResult(responses))
{
// This currently catches for V2ServerApiCalls where the package was not found
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
string message = _tag.Length == 0 ? $"Package with name '{pkgName}' could not be found in repository '{repository.Name}'." : $"Package with name '{pkgName}' and tags '{tagsAsString}' could not be found in repository '{repository.Name}'.";
errRecord = new ErrorRecord(
new ResourceNotFoundException(message, currentResult.exception),
"FindNameGlobbingConvertToPSResourceFailure",
ErrorCategory.ObjectNotFound,
this);

if (shouldReportErrorForEachRepo)
{
_cmdletPassedIn.WriteError(errRecord);
}
else
{
_cmdletPassedIn.WriteVerbose(message);
}
_cmdletPassedIn.WriteError(new ErrorRecord(
currentResult.exception,
"FindNameGlobbingConvertToPSResourceFailure",
ErrorCategory.InvalidResult,
this));

continue;
}
Expand Down Expand Up @@ -746,24 +727,13 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
}

PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
// This currently catches for V2ServerApiCalls where the package was not found
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
string message = _tag.Length == 0 ? $"Package with name '{pkgName}' could not be found in repository '{repository.Name}'." : $"Package with name '{pkgName}' and tags '{tagsAsString}' could not be found in repository '{repository.Name}'.";
errRecord = new ErrorRecord(
new ResourceNotFoundException(message, currentResult.exception),
"FindNameConvertToPSResourceFailure",
ErrorCategory.ObjectNotFound,
this);

if (shouldReportErrorForEachRepo)
{
_cmdletPassedIn.WriteError(errRecord);
}
else
{
_cmdletPassedIn.WriteVerbose(message);
}
_cmdletPassedIn.WriteError(new ErrorRecord(
currentResult.exception,
"FindNameConvertToPSResourceFailure",
ErrorCategory.ObjectNotFound,
this));

continue;
}
Expand Down Expand Up @@ -815,23 +785,13 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
}

PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
// This currently catches for V2ServerApiCalls where the package was not found
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
errRecord = new ErrorRecord(
new ResourceNotFoundException($"Package with name '{pkgName}', version '{_version}', and tags '{tagsAsString}' could not be found in repository '{repository.Name}'", currentResult.exception),
"FindVersionConvertToPSResourceFailure",
ErrorCategory.ObjectNotFound,
this);

if (shouldReportErrorForEachRepo)
{
_cmdletPassedIn.WriteError(errRecord);
}
else
{
_cmdletPassedIn.WriteVerbose(errRecord.Exception.Message);
}
_cmdletPassedIn.WriteError(new ErrorRecord(
currentResult.exception,
"FindVersionConvertToPSResourceFailure",
ErrorCategory.ObjectNotFound,
this));

continue;
}
Expand Down Expand Up @@ -862,7 +822,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
}
else
{
var exMessage = "-Tag cannot be specified when using version range.";
var exMessage = "-Tag parameter cannot be specified when using version range.";
var ex = new ArgumentException(exMessage);
var WildcardError = new ErrorRecord(ex, "InvalidWildCardUsage", ErrorCategory.InvalidOperation, null);
_cmdletPassedIn.WriteError(WildcardError);
Expand All @@ -888,21 +848,11 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
// This currently catches for V2ServerApiCalls where the package was not found
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
errRecord = new ErrorRecord(
new ResourceNotFoundException($"Package with name '{pkgName}' and version '{_version}' could not be found in repository '{repository.Name}'", currentResult.exception),
"FindVersionGlobbingConvertToPSResourceFailure",
ErrorCategory.ObjectNotFound,
this);


if (shouldReportErrorForEachRepo)
{
_cmdletPassedIn.WriteError(errRecord);
}
else
{
_cmdletPassedIn.WriteVerbose(errRecord.Exception.Message);
}
_cmdletPassedIn.WriteError(new ErrorRecord(
currentResult.exception,
"FindVersionGlobbingConvertToPSResourceFailure",
ErrorCategory.ObjectNotFound,
this));

continue;
}
Expand Down
40 changes: 21 additions & 19 deletions src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,41 +187,42 @@ private List<PSResourceInfo> ProcessRepositories(

var findHelper = new FindHelper(_cancellationToken, _cmdletPassedIn, _networkCredential);
List<PSResourceInfo> allPkgsInstalled = new List<PSResourceInfo>();
List<string> repositoryNamesToSearch = new List<string>();
bool sourceTrusted = false;

// Loop through all the repositories provided (in priority order) until there no more packages to install.
for (int i=0; i < listOfRepositories.Count && _pkgNamesToInstall.Count > 0; i++)
for (int i = 0; i < listOfRepositories.Count && _pkgNamesToInstall.Count > 0; i++)
{
PSRepositoryInfo repo = listOfRepositories[i];
sourceTrusted = repo.Trusted || trustRepository;
PSRepositoryInfo currentRepository = listOfRepositories[i];
string repoName = currentRepository.Name;
sourceTrusted = currentRepository.Trusted || trustRepository;

_networkCredential = Utils.SetNetworkCredential(repo, _networkCredential, _cmdletPassedIn);
ServerApiCall currentServer = ServerFactory.GetServer(repo, _networkCredential);
_networkCredential = Utils.SetNetworkCredential(currentRepository, _networkCredential, _cmdletPassedIn);
ServerApiCall currentServer = ServerFactory.GetServer(currentRepository, _networkCredential);

if (currentServer == null)
{
// this indicates that PSRepositoryInfo.APIVersion = PSRepositoryInfo.APIVersion.unknown
_cmdletPassedIn.WriteError(new ErrorRecord(
new PSInvalidOperationException($"Repository '{repo.Name}' is not a known repository type that is supported. Please file an issue for support at https://github.com/PowerShell/PSResourceGet/issues"),
new PSInvalidOperationException($"Repository '{currentRepository.Name}' is not a known repository type that is supported. Please file an issue for support at https://github.com/PowerShell/PSResourceGet/issues"),
"RepositoryApiVersionUnknown",
ErrorCategory.InvalidArgument,
this));

continue;
}

ResponseUtil currentResponseUtil = ResponseUtilFactory.GetResponseUtil(repo);
ResponseUtil currentResponseUtil = ResponseUtilFactory.GetResponseUtil(currentRepository);
bool installDepsForRepo = skipDependencyCheck;

// If no more packages to install, then return
if (_pkgNamesToInstall.Count == 0) {
return allPkgsInstalled;
}

string repoName = repo.Name;
_cmdletPassedIn.WriteVerbose(string.Format("Attempting to search for packages in '{0}'", repoName));

if (repo.Trusted == false && !trustRepository && !_force)
if (currentRepository.Trusted == false && !trustRepository && !_force)
{
_cmdletPassedIn.WriteVerbose("Checking if untrusted repository should be used");

Expand All @@ -238,13 +239,14 @@ private List<PSResourceInfo> ProcessRepositories(
continue;
}

if ((repo.ApiVersion == PSRepositoryInfo.APIVersion.v3) && (!installDepsForRepo))
repositoryNamesToSearch.Add(repoName);
if ((currentRepository.ApiVersion == PSRepositoryInfo.APIVersion.v3) && (!installDepsForRepo))
{
_cmdletPassedIn.WriteWarning("Installing dependencies is not currently supported for V3 server protocol repositories. The package will be installed without installing dependencies.");
installDepsForRepo = true;
}

var installedPkgs = InstallPackages(_pkgNamesToInstall.ToArray(), repo, currentServer, currentResponseUtil, scope, skipDependencyCheck, findHelper);
var installedPkgs = InstallPackages(_pkgNamesToInstall.ToArray(), currentRepository, currentServer, currentResponseUtil, scope, skipDependencyCheck, findHelper);
foreach (var pkg in installedPkgs)
{
_pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase));
Expand All @@ -253,9 +255,11 @@ private List<PSResourceInfo> ProcessRepositories(
allPkgsInstalled.AddRange(installedPkgs);
}

if (_pkgNamesToInstall.Count > 0) {
if (_pkgNamesToInstall.Count > 0)
{
string repositoryWording = repositoryNamesToSearch.Count > 1 ? "registered repositories" : "repository";
_cmdletPassedIn.WriteError(new ErrorRecord(
new ResourceNotFoundException($"Package(s) '{string.Join(", ", _pkgNamesToInstall)}' could not be installed from an."),
new ResourceNotFoundException($"Package(s) '{string.Join(", ", _pkgNamesToInstall)}' could not be installed from {repositoryWording} '{String.Join(", ", repositoryNamesToSearch)}'."),
"InstallPackageFailure",
ErrorCategory.InvalidData,
this));
Expand Down Expand Up @@ -478,7 +482,6 @@ private List<PSResourceInfo> InstallPackages(
// At this point parent package is installed to temp path.
if (errRecord != null)
{
// TODO: Anam working on fix, this may need to be updated
if (errRecord.FullyQualifiedErrorId.Equals("PackageNotFound"))
{
_cmdletPassedIn.WriteVerbose(errRecord.Exception.Message);
Expand Down Expand Up @@ -652,12 +655,11 @@ private Hashtable InstallPackage(
{
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
// V2Server API calls will return non-empty response when package is not found but fail at conversion time
errRecord = new ErrorRecord(
new InvalidOrEmptyResponse($"Package '{pkgNameToInstall}' could not be installed", currentResult.exception),
"InstallPackageFailure",
ErrorCategory.InvalidData,
this);
currentResult.exception,
"FindConvertToPSResourceFailure",
ErrorCategory.ObjectNotFound,
this);
}
else if (searchVersionType == VersionType.VersionRange)
{
Expand Down
Loading