Skip to content

Commit

Permalink
Return 400 Bad Request instead of 500 Internal Server Error when NuGe…
Browse files Browse the repository at this point in the history
…t.exe push encounters a validation error

Return 400 Bad Request instead of 500 Internal Server Error when
NuGet.exe push encounters a validation error
  • Loading branch information
maartenba committed Mar 30, 2016
1 parent 12e96a0 commit 5ebbf06
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
32 changes: 27 additions & 5 deletions src/NuGetGallery/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Web.Mvc;
using System.Web.UI;
using Newtonsoft.Json.Linq;
using NuGet.Frameworks;
using NuGet.Packaging;
using NuGet.Versioning;
using NuGetGallery.Configuration;
Expand Down Expand Up @@ -247,7 +248,8 @@ private async Task<ActionResult> CreatePackageInternal()
{
if (!packageRegistration.IsOwner(user))
{
return new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, Strings.ApiKeyNotAuthorized);
return new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden,
Strings.ApiKeyNotAuthorized);
}

// Check if a particular Id-Version combination already exists. We eventually need to remove this check.
Expand Down Expand Up @@ -275,7 +277,10 @@ private async Task<ActionResult> CreatePackageInternal()
Size = packageStream.Length,
};

var package = await PackageService.CreatePackageAsync(packageToPush, packageStreamMetadata, user, commitChanges: false);
var package =
await
PackageService.CreatePackageAsync(packageToPush, packageStreamMetadata, user,
commitChanges: false);
await AutoCuratePackage.ExecuteAsync(package, packageToPush, commitChanges: false);
await EntitiesContext.SaveChangesAsync();

Expand All @@ -289,15 +294,32 @@ private async Task<ActionResult> CreatePackageInternal()
return new HttpStatusCodeResult(HttpStatusCode.Created);
}
}
catch (InvalidPackageException ex)
{
return BadRequestForExceptionMessage(ex);
}
catch (InvalidDataException ex)
{
return new HttpStatusCodeWithBodyResult(
HttpStatusCode.BadRequest,
string.Format(CultureInfo.CurrentCulture, Strings.UploadPackage_InvalidPackage, ex.Message));
return BadRequestForExceptionMessage(ex);
}
catch (EntityException ex)
{
return BadRequestForExceptionMessage(ex);
}
catch (FrameworkException ex)
{
return BadRequestForExceptionMessage(ex);
}
}
}

private static ActionResult BadRequestForExceptionMessage(Exception ex)
{
return new HttpStatusCodeWithBodyResult(
HttpStatusCode.BadRequest,
string.Format(CultureInfo.CurrentCulture, Strings.UploadPackage_InvalidPackage, ex.Message));
}

[HttpDelete]
[RequireSsl]
[ApiAuthorize]
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetGallery/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/NuGetGallery/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ The {2} Team</value>
<value>You canceled your email address change request.</value>
</data>
<data name="UploadPackage_InvalidPackage" xml:space="preserve">
<value>The NuGet is invalid. The error encountered was:'{0}'. Correct the error and try again.</value>
<value>The NuGet package is invalid. The error encountered was:'{0}'. Correct the error and try again.</value>
</data>
<data name="UploadPackage_InvalidNuspec" xml:space="preserve">
<value>The NuGet package contains an invalid .nuspec file. The error encountered was:'{0}'. Correct the error and try again.</value>
Expand Down

0 comments on commit 5ebbf06

Please sign in to comment.