Skip to content

Exact Error Codes for Error Collections #388

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

Merged
merged 1 commit into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Internal/JsonApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public JsonApiException(int statusCode, string message, Exception innerException

public int GetStatusCode()
{
if (_errors.Errors.Count == 1)
if (_errors.Errors.Select(a => a.StatusCode).Distinct().Count() == 1)
return _errors.Errors[0].StatusCode;

if (_errors.Errors.FirstOrDefault(e => e.StatusCode >= 500) != null)
Expand Down
31 changes: 31 additions & 0 deletions test/UnitTests/Internal/JsonApiException_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using JsonApiDotNetCore.Internal;
using Xunit;

namespace UnitTests.Internal
{
public class JsonApiException_Test
{
[Fact]
public void Can_GetStatusCode()
{
var errors = new ErrorCollection();
var exception = new JsonApiException(errors);

// Add First 422 error
errors.Add(new Error(422, "Something wrong"));
Assert.Equal(422, exception.GetStatusCode());

// Add a second 422 error
errors.Add(new Error(422, "Something else wrong"));
Assert.Equal(422, exception.GetStatusCode());

// Add 4xx error not 422
errors.Add(new Error(401, "Unauthorized"));
Assert.Equal(400, exception.GetStatusCode());
Copy link
Contributor

Choose a reason for hiding this comment

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

yeeeesssss


// Add 5xx error not 4xx
errors.Add(new Error(502, "Not good"));
Assert.Equal(500, exception.GetStatusCode());
}
}
}