-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Conversation
I'm against including this stolen exception handling configuration code into this library because
I suggest that we do it like this
|
{ | ||
/// <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"/>. |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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";
)
There was a problem hiding this 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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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?
No description provided.