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
8 changes: 8 additions & 0 deletions prop-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

docker run --rm --name jsonapi-dotnet-core-testing \
-e POSTGRES_DB=JsonApiDotNetCoreExample \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
postgres
28 changes: 2 additions & 26 deletions src/JsonApiDotNetCore/Controllers/JsonApiControllerMixin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,12 @@ protected IActionResult Forbidden()

protected IActionResult Error(Error error)
{
var errorCollection = new ErrorCollection
{
Errors = new List<Error> { error }
};

return new ObjectResult(errorCollection)
{
StatusCode = error.StatusCode
};
return error.AsActionResult();
}

protected IActionResult Errors(ErrorCollection errors)
{
return new ObjectResult(errors)
{
StatusCode = GetErrorStatusCode(errors)
};
}

private int GetErrorStatusCode(ErrorCollection errors)
{
var statusCodes = errors.Errors
.Select(e => e.StatusCode)
.Distinct()
.ToList();

if (statusCodes.Count == 1)
return statusCodes[0];

return int.Parse(statusCodes.Max().ToString()[0] + "00");
return errors.AsActionResult();
}
}
}
12 changes: 12 additions & 0 deletions src/JsonApiDotNetCore/Internal/Error.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
using JsonApiDotNetCore.Configuration;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc;

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

public bool ShouldSerializeMeta() => (JsonApiOptions.DisableErrorStackTraces == false);
public bool ShouldSerializeSource() => (JsonApiOptions.DisableErrorSource == false);

public IActionResult AsActionResult()
{
var errorCollection = new ErrorCollection
{
Errors = new List<Error> { this }
};

return errorCollection.AsActionResult();
}
}

public class ErrorMeta
Expand Down
23 changes: 23 additions & 0 deletions src/JsonApiDotNetCore/Internal/ErrorCollection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.Mvc;

namespace JsonApiDotNetCore.Internal
{
Expand All @@ -25,5 +27,26 @@ public string GetJson()
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
}

public int GetErrorStatusCode()
{
var statusCodes = Errors
.Select(e => e.StatusCode)
.Distinct()
.ToList();

if (statusCodes.Count == 1)
return statusCodes[0];

return int.Parse(statusCodes.Max().ToString()[0] + "00");
}

public IActionResult AsActionResult()
{
return new ObjectResult(this)
{
StatusCode = GetErrorStatusCode()
};
}
}
}
11 changes: 1 addition & 10 deletions src/JsonApiDotNetCore/Internal/JsonApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,7 @@ public JsonApiException(int statusCode, string message, Exception innerException

public int GetStatusCode()
{
if (_errors.Errors.Select(a => a.StatusCode).Distinct().Count() == 1)
return _errors.Errors[0].StatusCode;

if (_errors.Errors.FirstOrDefault(e => e.StatusCode >= 500) != null)
return 500;

if (_errors.Errors.FirstOrDefault(e => e.StatusCode >= 400) != null)
return 400;

return 500;
return _errors.GetErrorStatusCode();
}

private ErrorMeta GetMeta() => ErrorMeta.FromException(this);
Expand Down