Skip to content
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());
}
}
}