Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 531f079

Browse files
Refactor Errors for AsActionResult
1 parent 3a11e9e commit 531f079

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

‎src/JsonApiDotNetCore/Controllers/JsonApiControllerMixin.cs‎

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,12 @@ protected IActionResult Forbidden()
1414

1515
protected IActionResult Error(Error error)
1616
{
17-
var errorCollection = new ErrorCollection
18-
{
19-
Errors = new List<Error> { error }
20-
};
21-
22-
return new ObjectResult(errorCollection)
23-
{
24-
StatusCode = error.StatusCode
25-
};
17+
return error.AsActionResult();
2618
}
2719

2820
protected IActionResult Errors(ErrorCollection errors)
2921
{
30-
return new ObjectResult(errors)
31-
{
32-
StatusCode = GetErrorStatusCode(errors)
33-
};
34-
}
35-
36-
private int GetErrorStatusCode(ErrorCollection errors)
37-
{
38-
var statusCodes = errors.Errors
39-
.Select(e => e.StatusCode)
40-
.Distinct()
41-
.ToList();
42-
43-
if (statusCodes.Count == 1)
44-
return statusCodes[0];
45-
46-
return int.Parse(statusCodes.Max().ToString()[0] + "00");
22+
return errors.AsActionResult();
4723
}
4824
}
4925
}

‎src/JsonApiDotNetCore/Internal/Error.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Collections.Generic;
34
using JsonApiDotNetCore.Configuration;
45
using Newtonsoft.Json;
6+
using Microsoft.AspNetCore.Mvc;
57

68
namespace JsonApiDotNetCore.Internal
79
{
@@ -66,6 +68,16 @@ public Error(int status, string title, string detail, ErrorMeta meta = null, obj
6668

6769
public bool ShouldSerializeMeta() => (JsonApiOptions.DisableErrorStackTraces == false);
6870
public bool ShouldSerializeSource() => (JsonApiOptions.DisableErrorSource == false);
71+
72+
public IActionResult AsActionResult()
73+
{
74+
var errorCollection = new ErrorCollection
75+
{
76+
Errors = new List<Error> { this }
77+
};
78+
79+
return errorCollection.AsActionResult();
80+
}
6981
}
7082

7183
public class ErrorMeta

‎src/JsonApiDotNetCore/Internal/ErrorCollection.cs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using Newtonsoft.Json;
34
using Newtonsoft.Json.Serialization;
5+
using Microsoft.AspNetCore.Mvc;
46

57
namespace JsonApiDotNetCore.Internal
68
{
@@ -25,5 +27,26 @@ public string GetJson()
2527
ContractResolver = new CamelCasePropertyNamesContractResolver()
2628
});
2729
}
30+
31+
public int GetErrorStatusCode()
32+
{
33+
var statusCodes = Errors
34+
.Select(e => e.StatusCode)
35+
.Distinct()
36+
.ToList();
37+
38+
if (statusCodes.Count == 1)
39+
return statusCodes[0];
40+
41+
return int.Parse(statusCodes.Max().ToString()[0] + "00");
42+
}
43+
44+
public IActionResult AsActionResult()
45+
{
46+
return new ObjectResult(this)
47+
{
48+
StatusCode = GetErrorStatusCode()
49+
};
50+
}
2851
}
2952
}

‎src/JsonApiDotNetCore/Internal/JsonApiException.cs‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,7 @@ public JsonApiException(int statusCode, string message, Exception innerException
4141

4242
public int GetStatusCode()
4343
{
44-
if (_errors.Errors.Select(a => a.StatusCode).Distinct().Count() == 1)
45-
return _errors.Errors[0].StatusCode;
46-
47-
if (_errors.Errors.FirstOrDefault(e => e.StatusCode >= 500) != null)
48-
return 500;
49-
50-
if (_errors.Errors.FirstOrDefault(e => e.StatusCode >= 400) != null)
51-
return 400;
52-
53-
return 500;
44+
return _errors.GetErrorStatusCode();
5445
}
5546

5647
private ErrorMeta GetMeta() => ErrorMeta.FromException(this);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /