Skip to content
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

Exception handling middleware #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

yshchohaleu
Copy link
Collaborator

No description provided.

@rurku
Copy link
Collaborator

rurku commented Sep 23, 2017

I'm against including this stolen exception handling configuration code into this library because

  • If we just copy it like this then we need to maintain it
  • we need to document it
  • it does more than we actually need
  • the current solution limits what the user can do - user can only return a different message and status code, but not a different response format or response headers

I suggest that we do it like this

  • remove the stolen code❗️
  • in WacWebApiExceptionHandlerOptions add a delegate that can be used in Startup to add extra exception handling logic. It would be used for exceptions not derived from WebApiException.
    The delegate would look like this Func<ExceptionHandlingContext> and ExceptionHandlingContext would contain
public HttpContext HttpContext { get; set;}
public Exception Exception { get; set;}
// If Handled is set to true then the middleware will not rethrow the exception.
public bool Handled { get; set;}

{
/// <summary>
/// A middleware to handle exceptions in Web Api project and generate HTTP response based on exception type.
/// By default this middleware will handle all exceptions inherited from <see cref="WebApiException"/>.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all exceptions derived from

But it's not true anyway, because it also handles unhandled exceptions.

_options = options ?? new WacWebApiExceptionHandlerOptions();
}

public async Task Invoke(HttpContext httpContext, ILogger<WacWebApiExceptionHandlerMiddleware> logger)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the parameter logger is not used anywhere

}
}

private void SendErrorResposnse(object data, HttpStatusCode httpStatusCode, HttpContext httpContext)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in method name

/// </summary>
public class WacWebApiExceptionHandlerMiddleware
{
private const string UnhandledExceptionMessage = "Internal server error";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to put this property in the WacWebApiExceptionHandlerOptions class, so the user can change the text, if he wants to. (public string UnhandledExceptionMessage = "Internal server error";)

Copy link
Owner

@HerbertBodner HerbertBodner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I tend to agree with Jacek, because the whole code is rather complicated to understand and therefore hard to maintain. And also I don´t really see the use case of some options. In particular, I don´t see the value of having HandlingInput class.

On the other hand, the way Jacek suggests is on my opinion too simple.
I´d go for some middle-ground solution, where you keep the On<TException>(Func<TException, TResult> handler) method (maybe with the condition parameter if it is simple to do).

}
}

public bool ContainsHandler<TException>() where TException : Exception
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that this method seems to return always false, because handlerExceptionType is always of type Exception.
I guess, that it should return true, if you specify a WebApiException handler in the startup as follows:

                    .On<WebApiException>(e => WacHandling.Handled(new WebApiExceptionDto
                    {
                        Message = "test my own handling",
                        HttpStatusCode = (HttpStatusCode)0,
                        ErrorReference = Guid.NewGuid().ToString()
                    }))

else
{
var exceptionDto = result.Result;
SendErrorResposnse(exceptionDto, exceptionDto.HttpStatusCode, httpContext);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created my own class MyExceptionDto, which does not derive from WebApiExceptionDto

public class MyExceptionDto {
        public string MyProperty { get; set; }
    }

and added it In the startup of the example project like this:

.On<BadRequestException>(e => WacHandling.Handled(new MyExceptionDto
                {
                    MyProperty = "MyTest",
                }))

This resulted in a NullReferenceException on this line. Somehow the exceptionDto gets lost somewhere and is null here.

return this;
}

public WacHandlingConfiguration<TParameter, TResult> FinalizeWith(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering where you would implement Logging of exceptions.
Is FinalizeWith the method, where you would do that?
If not, what is an example, where this method would be used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants