Skip to content

Commit 2feec1d

Browse files
committed
feat(jsonapi-exception): add logic for getting error collectoon status
1 parent 10184ac commit 2feec1d

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/JsonApiDotNetCore/Internal/Error.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public Error(string status, string title, string detail)
2828

2929
[JsonProperty("status")]
3030
public string Status { get; set; }
31+
32+
public int StatusCode { get { return int.Parse(Status); } }
3133
}
3234
}

src/JsonApiDotNetCore/Internal/JsonApiException.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

34
namespace JsonApiDotNetCore.Internal
45
{
@@ -33,5 +34,19 @@ public ErrorCollection GetError()
3334
{
3435
return _errors;
3536
}
37+
38+
public int GetStatusCode()
39+
{
40+
if(_errors.Errors.Count == 1)
41+
return _errors.Errors[0].StatusCode;
42+
43+
if(_errors.Errors.FirstOrDefault(e => e.StatusCode >= 500) != null)
44+
return 500;
45+
46+
if(_errors.Errors.FirstOrDefault(e => e.StatusCode >= 400) != null)
47+
return 400;
48+
49+
return 500;
50+
}
3651
}
3752
}

src/JsonApiDotNetCore/Middleware/JsonApiExceptionFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void OnException(ExceptionContext context)
2323

2424
var error = jsonApiException.GetError();
2525
var result = new ObjectResult(error);
26-
result.StatusCode = Convert.ToInt16(error.Status);
26+
result.StatusCode = jsonApiException.GetStatusCode();
2727
context.Result = result;
2828
}
2929
}

0 commit comments

Comments
 (0)