Skip to content

Commit

Permalink
New attribute WebDavAnyExceptionFilterAttribute
Browse files Browse the repository at this point in the history
Ensures that - in the sample server - all exceptions are returned as multi-status responses.

Incorporates the changes from #38. Maybe we need to return the error element too?
  • Loading branch information
fubar-coder committed Nov 22, 2021
1 parent 108491b commit 5e5011e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FubarDev.WebDavServer.AspNetCore;
using FubarDev.WebDavServer.AspNetCore.Filters;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -8,6 +9,7 @@ namespace FubarDev.WebDavServer.Sample.AspNetCore.Controllers
{
[Route("_dav/{*path}")]
[Authorize]
[WebDavAnyExceptionFilter]
public class WebDavController : WebDavControllerBase
{
public WebDavController(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// <copyright file="WebDavAnyExceptionFilterAttribute.cs" company="Fubar Development Junker">
// Copyright (c) Fubar Development Junker. All rights reserved.
// </copyright>

using System.Threading.Tasks;

using FubarDev.WebDavServer.Model;

using Microsoft.AspNetCore.Mvc.Filters;

namespace FubarDev.WebDavServer.AspNetCore.Filters
{
/// <summary>
/// Attribute to handle exception filtering for WebDAV requests.
/// </summary>
public class WebDavAnyExceptionFilterAttribute : WebDavExceptionFilterAttribute
{
/// <inheritdoc />
public override async Task OnExceptionAsync(ExceptionContext context)
{
await base.OnExceptionAsync(context);

if (context.ExceptionHandled)
{
return;
}

// Return an internal server error status for all other exceptions
context.Result = BuildResultForStatusCode(
context,
WebDavStatusCode.InternalServerError);
context.ExceptionHandled = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

namespace FubarDev.WebDavServer.AspNetCore.Filters
{
/// <summary>
/// Attribute to handle exception filtering for WebDAV requests.
/// </summary>
public class WebDavExceptionFilterAttribute : ExceptionFilterAttribute
{
/// <inheritdoc />
Expand All @@ -33,11 +36,11 @@ public override void OnException(ExceptionContext context)
context.ExceptionHandled = true;
return;
case WebDavException webDavException:
context.Result = BuildResultForStatusCode(context, webDavException.StatusCode, webDavException.Message);
context.Result = BuildResultForStatusCode(context, webDavException.StatusCode);
context.ExceptionHandled = true;
return;
case UnauthorizedAccessException unauthorizedAccessException:
context.Result = BuildResultForStatusCode(context, WebDavStatusCode.Forbidden, unauthorizedAccessException.Message);
case UnauthorizedAccessException:
context.Result = BuildResultForStatusCode(context, WebDavStatusCode.Forbidden);
context.ExceptionHandled = true;
return;
}
Expand All @@ -50,10 +53,10 @@ public override void OnException(ExceptionContext context)
context.Exception.Message);
}

private IActionResult BuildResultForStatusCode(
protected static IActionResult BuildResultForStatusCode(
ExceptionContext context,
WebDavStatusCode statusCode,
string optionalMessage)
string? optionalMessage = null)
{
switch (statusCode)
{
Expand All @@ -74,7 +77,10 @@ private IActionResult BuildResultForStatusCode(
ItemsElementName = new[] { ItemsChoiceType2.status, },
Items = new object[]
{
new Status(context.HttpContext.Request.Protocol, statusCode, optionalMessage).ToString(),
new Status(
context.HttpContext.Request.Protocol,
statusCode,
optionalMessage ?? context.Exception.Message).ToString(),
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions src/FubarDev.WebDavServer/Model/WebDavStatusCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public enum WebDavStatusCode
/// </summary>
FailedDependency = 424,

/// <summary>
/// The <c>Internal Server Error</c> (500) status code
/// </summary>
InternalServerError = HttpStatusCode.InternalServerError,

/// <summary>
/// The <c>Not Implemented</c> (501) status code
/// </summary>
Expand Down

0 comments on commit 5e5011e

Please sign in to comment.