Skip to content

Commit

Permalink
Log full command json when sending command fails
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankBakkerNl committed Nov 7, 2023
1 parent 9691a5d commit ada5e99
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ public async Task TestTaskCompleteWithErrorResultShouldLogWarning()
_resultMessageHandler.HandleResult(task, new CommandMessage {Type = "test"});
await _resultMessageHandler.DisposeAsync().ConfigureAwait(false);
task.IsCompletedSuccessfully.Should().BeTrue();
_loggerMock.VerifyWarningWasCalled("Failed command (test) error: (null). Sent command is CommandMessage { Type = test, Id = 0 }");
_loggerMock.VerifyErrorWasCalled("""Failed command (test) error: (null). Sent command is {"id":0,"type":"test"}""");
}

[Fact]
public async Task TestTaskCompleteWithExceptionShouldLogError()
{
// TODO: Test sometimes fails in CI
var task = SomeUnSuccessfulResultThrowsException();

Check failure on line 45 in src/Client/NetDaemon.HassClient.Tests/HelperTest/ResultMessageHandlerTests.cs

View workflow job for this annotation

GitHub Actions / 🔨 Build sources (CI)

NetDaemon.HassClient.Tests.HelperTest.ResultMessageHandlerTests.TestTaskCompleteWithExceptionShouldLogError

Moq.MockException : Expected invocation on the mock at least once, but was never performed: x => x.Log<It.IsAnyType>(It.Is<LogLevel>(l => (int)l == 4), It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => state(v, t)), It.IsAny<Exception>(), It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => True)) Performed invocations: Mock<ILogger<ResultMessageHandler>:4> (x): No invocations performed.
_resultMessageHandler.HandleResult(task, new CommandMessage {Type = "test"});
_resultMessageHandler.HandleResult(task, new CallServiceCommand {Type = "test", Service = "light.turn_on", ServiceData = new {brightness = 2.3 }});
await _resultMessageHandler.DisposeAsync().ConfigureAwait(false);
task.IsCompletedSuccessfully.Should().BeFalse();
task.IsFaulted.Should().BeTrue();
_loggerMock.VerifyErrorWasCalled("Exception waiting for result message Sent command is CommandMessage { Type = test, Id = 0 }");
_loggerMock.VerifyErrorWasCalled("""Exception waiting for result message Sent command is {"domain":"","service":"light.turn_on","service_data":{"brightness":2.3},"target":null,"id":0,"type":"test"}""");
}

[Fact]
Expand All @@ -59,7 +59,7 @@ public async Task TestTaskCompleteWithTimeoutShouldLogWarning()
_resultMessageHandler.HandleResult(task, new CommandMessage {Type = "test"});
await _resultMessageHandler.DisposeAsync().ConfigureAwait(false);
task.IsCompletedSuccessfully.Should().BeTrue();
_loggerMock.VerifyWarningWasCalled("Command (test) did not get response in timely fashion. Sent command is CommandMessage { Type = test, Id = 0 }");
_loggerMock.VerifyWarningWasCalled("""Command (test) did not get response in timely fashion. Sent command is {"id":0,"type":"test"}""");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

public record CommandMessage : HassMessageBase
{
private static readonly JsonSerializerOptions NonIndentingJsonSerializerOptions = new() { WriteIndented = false };

[JsonPropertyName("id")] public int Id { get; set; }
}

public string GetJsonString() => JsonSerializer.Serialize(this, GetType(), NonIndentingJsonSerializerOptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ async Task Wrap()
// We have a timeout
logger.LogWarning(
"Command ({CommandType}) did not get response in timely fashion. Sent command is {CommandMessage}",
command.Type, command);
command.Type, command.GetJsonString());
}
// We wait for the task even if there was a timeout so we make sure
// we catch the original error
var result = await task.ConfigureAwait(false);
if (!result.Success ?? false)
{
logger.LogWarning(
logger.LogError(
"Failed command ({CommandType}) error: {ErrorResult}. Sent command is {CommandMessage}",
command.Type, result.Error, command);
command.Type, result.Error, command.GetJsonString());
}
}
catch (Exception e)
{
logger.LogError(e, "Exception waiting for result message Sent command is {CommandMessage}", command);
logger.LogError(e, "Exception waiting for result message Sent command is {CommandMessage}", command.GetJsonString());
}
finally
{
Expand Down

0 comments on commit ada5e99

Please sign in to comment.