Skip to content

Commit a1241ee

Browse files
authored
Merge pull request #466 from joshhubers/feat/#356
Feat/#356
2 parents f72e8ae + 531f079 commit a1241ee

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

prop-docker.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
docker run --rm --name jsonapi-dotnet-core-testing \
4+
-e POSTGRES_DB=JsonApiDotNetCoreExample \
5+
-e POSTGRES_USER=postgres \
6+
-e POSTGRES_PASSWORD=postgres \
7+
-p 5432:5432 \
8+
postgres

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)