Skip to content

Fixed: do not intercept exceptions thrown from non-json:api requests #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2020
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers.Restricted
{
[Route("[controller]")]
[DisableRoutingConvention, Route("[controller]")]
[HttpReadOnly]
public class ReadOnlyController : ControllerBase
public class ReadOnlyController : BaseJsonApiController<Article>
{
public ReadOnlyController(
IJsonApiOptions jsonApiOptions,
ILoggerFactory loggerFactory,
IResourceService<Article> resourceService)
: base(jsonApiOptions, loggerFactory, resourceService)
{ }

[HttpGet]
public IActionResult Get() => Ok();

Expand All @@ -20,10 +31,17 @@ public class ReadOnlyController : ControllerBase
public IActionResult Delete() => Ok();
}

[Route("[controller]")]
[DisableRoutingConvention, Route("[controller]")]
[NoHttpPost]
public class NoHttpPostController : ControllerBase
public class NoHttpPostController : BaseJsonApiController<Article>
{
public NoHttpPostController(
IJsonApiOptions jsonApiOptions,
ILoggerFactory loggerFactory,
IResourceService<Article> resourceService)
: base(jsonApiOptions, loggerFactory, resourceService)
{ }

[HttpGet]
public IActionResult Get() => Ok();

Expand All @@ -37,10 +55,17 @@ public class NoHttpPostController : ControllerBase
public IActionResult Delete() => Ok();
}

[Route("[controller]")]
[DisableRoutingConvention, Route("[controller]")]
[NoHttpPatch]
public class NoHttpPatchController : ControllerBase
public class NoHttpPatchController : BaseJsonApiController<Article>
{
public NoHttpPatchController(
IJsonApiOptions jsonApiOptions,
ILoggerFactory loggerFactory,
IResourceService<Article> resourceService)
: base(jsonApiOptions, loggerFactory, resourceService)
{ }

[HttpGet]
public IActionResult Get() => Ok();

Expand All @@ -54,10 +79,17 @@ public class NoHttpPatchController : ControllerBase
public IActionResult Delete() => Ok();
}

[Route("[controller]")]
[DisableRoutingConvention, Route("[controller]")]
[NoHttpDelete]
public class NoHttpDeleteController : ControllerBase
public class NoHttpDeleteController : BaseJsonApiController<Article>
{
public NoHttpDeleteController(
IJsonApiOptions jsonApiOptions,
ILoggerFactory loggerFactory,
IResourceService<Article> resourceService)
: base(jsonApiOptions, loggerFactory, resourceService)
{ }

[HttpGet]
public IActionResult Get() => Ok();

Expand Down
14 changes: 9 additions & 5 deletions src/JsonApiDotNetCore/Middleware/DefaultExceptionFilter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using JsonApiDotNetCore.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

Expand All @@ -17,12 +18,15 @@ public DefaultExceptionFilter(IExceptionHandler exceptionHandler)

public void OnException(ExceptionContext context)
{
var errorDocument = _exceptionHandler.HandleException(context.Exception);

context.Result = new ObjectResult(errorDocument)
if (context.HttpContext.IsJsonApiRequest())
{
StatusCode = (int) errorDocument.GetErrorStatusCode()
};
var errorDocument = _exceptionHandler.HandleException(context.Exception);

context.Result = new ObjectResult(errorDocument)
{
StatusCode = (int) errorDocument.GetErrorStatusCode()
};
}
}
}
}