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

Add exception message to logging when mapping fails due to an exception. #1248

Merged
merged 4 commits into from
Jan 30, 2025

Conversation

JvE-iO
Copy link
Contributor

@JvE-iO JvE-iO commented Jan 29, 2025

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()...

@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

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).

Copy link
Collaborator

@StefH StefH Jan 29, 2025

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

Copy link
Contributor Author

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.

Copy link
Collaborator

@StefH StefH Jan 29, 2025

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')

Copy link
Collaborator

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 ?

Copy link
Contributor Author

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 :)

@StefH StefH merged commit 29bf9b4 into WireMock-Net:master Jan 30, 2025
6 checks passed
@JvE-iO JvE-iO deleted the patch-1 branch February 4, 2025 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants