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

Fix exception handling in IsIdentifiable service #1806

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ protected override void ProcessMessageImpl(IMessageHeader header, ExtractedFileS
}
catch (Exception e)
{
SendVerificationMessage(statusMessage, header, tag, VerifiedFileStatus.ErrorWontRetry, $"Exception while classifying {statusMessage.GetType().Name}:\n{e}. File could not be scanned.");
return;
if (e is ArithmeticException)
{
SendVerificationMessage(statusMessage, header, tag, VerifiedFileStatus.ErrorWontRetry, $"Exception while classifying {statusMessage.GetType().Name}:\n{e}");
return;
}

throw;
}

foreach (Failure f in failures)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using IsIdentifiable.Failures;
using IsIdentifiable.Failures;
using IsIdentifiable.Reporting;
using Microservices.IsIdentifiable.Service;
using Moq;
Expand Down Expand Up @@ -252,6 +252,52 @@ public void ProcessMessage_ClassifierArithmeticException_SendsErrorWontRetry()
Assert.True(_response.Report.StartsWith("Exception while classifying ExtractedFileStatusMessage:\nSystem.ArithmeticException: divide by zero"));
}

[Test]
public void ProcessMessage_ClassifierUnhandledException_CallsFatal()
{
// Arrange

var mockClassifier = new Mock<IClassifier>(MockBehavior.Strict);
mockClassifier.Setup(x => x.Classify(It.IsAny<IFileInfo>())).Throws(new Exception("whee"));

var consumer = GetNewIsIdentifiableQueueConsumer(null, mockClassifier.Object);

// Act

consumer.TestMessage(_extractedFileStatusMessage);

// Assert

TestTimelineAwaiter.Await(() => _fatalArgs != null, "Expected Fatal to be called");
Assert.AreEqual("ProcessMessageImpl threw unhandled exception", _fatalArgs?.Message);
Assert.AreEqual("whee", _fatalArgs?.Exception?.Message);
Assert.AreEqual(0, consumer.NackCount);
Assert.AreEqual(0, consumer.AckCount);
}

[Test]
public void ProcessMessage_ClassifierInvalidFile_CallsFatal()
{
// Arrange

var mockClassifier = new Mock<IClassifier>(MockBehavior.Strict);
mockClassifier.Setup(x => x.Classify(It.IsAny<IFileInfo>())).Throws(new ApplicationException("File does not contain valid preamble and header"));

var consumer = GetNewIsIdentifiableQueueConsumer(null, mockClassifier.Object);

// Act

consumer.TestMessage(_extractedFileStatusMessage);

// Assert

TestTimelineAwaiter.Await(() => _fatalArgs != null, "Expected Fatal to be called");
Assert.AreEqual("ProcessMessageImpl threw unhandled exception", _fatalArgs?.Message);
Assert.AreEqual("File does not contain valid preamble and header", _fatalArgs?.Exception?.Message);
Assert.AreEqual(0, consumer.NackCount);
Assert.AreEqual(0, consumer.AckCount);
}

#endregion
}
}
Loading