-
-
Notifications
You must be signed in to change notification settings - Fork 213
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
Add exception message to logging when mapping fails due to an exception. #1248
Conversation
@@ -224,7 +224,7 @@ private async Task InvokeInternalAsync(IContext ctx) | |||
} | |||
catch (Exception ex) | |||
{ | |||
_options.Logger.Error("HttpStatusCode set to 404 : No matching mapping found", ex); | |||
_options.Logger.Error("HttpStatusCode set to 404 : 'No matching mapping found', due to exception '{0}'", ex); |
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.
There is also Error method with an exception. Would that also work for you?
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 that's already being used.
But WireMock.Net.Xunit.TestOutputHelperWireMockLogger.Error(string formatString, Exception exception)
expects the formatString to have a placeholder for the exception message.
So it could also be fixed there. And in WireMock.Logging.WireMockConsoleLogger.Error(string formatString, Exception 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.
OK I see, I did not detect this...
However, the reason that I did not expose the exception is because of security.
The best way to detect if a mapping is working correct or incorrect is looking at the logging.
https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-Tips
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.
Yes, that eventually told me it was a perfect match.
But that was immediately followed by a "HttpStatusCode set to 404 : No matching mapping found".
And then you're in limbo.
WireMock says it has a matching mapping and WireMock says no matching mapping was found. Nothing else is logged.
I finally figured out what was going on by creating and injecting a custom IWireMockLogger that does log exception messages.
Not logging the exception message for security reasons sounds strange to me. How would anyone know that an exception has occurred then? Also, your loggers do log all the exception messages when the type is AggregateException.
Anyway, even changing it to something like
_options.Logger.Error("Mapping failed due to unhandled exception. HttpStatusCode set to 404 : No matching mapping found", ex);
may help someone in the future a little bit.
Or maybe an exception occuring after a matching mapping has been found should not return a 404 with a "No matching mapping found" message. A 500 "something went wrong during the mapping" is probably more helpful.
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.
Maybe I miss the context, but this line (from the consolelogger.cs) does log the exception?
_options.Logger.Error("HttpStatusCode set to 404 : No matching mapping found", ex);
Example:
Error("HttpStatusCode set to 404 : No matching mapping found", new ArgumentNullException("abc"));
return;
void Error(string message, Exception exception)
{
Console.WriteLine(Format("Error", $"{message} {{0}}", exception));
if (exception is AggregateException ae)
{
ae.Handle(ex =>
{
Console.WriteLine(Format("Error", "Exception {0}", ex));
return true;
});
}
}
static string Format(string level, string formatString, params object[] args)
{
var message = args.Length > 0 ? string.Format(formatString, args) : formatString;
return $"{DateTime.UtcNow} [{level}] : {message}";
}
Logging is:
1/29/2025 3:50:30 PM [Error] : HttpStatusCode set to 404 : No matching mapping found System.ArgumentNullException: Value cannot be null. (Parameter 'abc')
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.
But the TestOutputHelperWireMockLogger.cs could have an issue ?
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.
Aha, that clears up some of the confusion.
I'm not sure why, but when I asked Rider to show me the implementations of IWireMockLogger it "helpfully" included the decompiled version of a wiremock 1.5.1 nuget package I had on disk. There the WireMockConsoleLogger had the implementation without the placeholder for the exception message.
So we're left with just fixing these?
- WireMock.Net.TUnit.TUnitWireMockLogger
- WireMock.Net.Xunit.TestOutputHelperWireMockLogger
- WireMock.Net.WebApplication.WireMockService.Logger
I've amended my PR.
Thank you for your patience in this :)
I spent way too much time in the debugger trying to figure out why the mapping was not found.
It was found, but an exception occurred during the mapping, leading to a misleading message.
As an aside, the exception occurred because I called
.WithMapping()
before I called.AddProtoDefinition()
...