Skip to content

Commit 46fcc98

Browse files
authored
Merge pull request #388 from rtablada/rt/better-error-codes
Exact Error Codes for Error Collections
2 parents 23329ab + 1506270 commit 46fcc98

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/JsonApiDotNetCore/Internal/JsonApiException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public JsonApiException(int statusCode, string message, Exception innerException
4141

4242
public int GetStatusCode()
4343
{
44-
if (_errors.Errors.Count == 1)
44+
if (_errors.Errors.Select(a => a.StatusCode).Distinct().Count() == 1)
4545
return _errors.Errors[0].StatusCode;
4646

4747
if (_errors.Errors.FirstOrDefault(e => e.StatusCode >= 500) != null)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using JsonApiDotNetCore.Internal;
2+
using Xunit;
3+
4+
namespace UnitTests.Internal
5+
{
6+
public class JsonApiException_Test
7+
{
8+
[Fact]
9+
public void Can_GetStatusCode()
10+
{
11+
var errors = new ErrorCollection();
12+
var exception = new JsonApiException(errors);
13+
14+
// Add First 422 error
15+
errors.Add(new Error(422, "Something wrong"));
16+
Assert.Equal(422, exception.GetStatusCode());
17+
18+
// Add a second 422 error
19+
errors.Add(new Error(422, "Something else wrong"));
20+
Assert.Equal(422, exception.GetStatusCode());
21+
22+
// Add 4xx error not 422
23+
errors.Add(new Error(401, "Unauthorized"));
24+
Assert.Equal(400, exception.GetStatusCode());
25+
26+
// Add 5xx error not 4xx
27+
errors.Add(new Error(502, "Not good"));
28+
Assert.Equal(500, exception.GetStatusCode());
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)