Exception handling: Improve error messaging on invalid umbraco-package.json file (#20332)

* Improve error messaging on invalid umbraco-package.json file.

* Adjust failing unit tests

---------

Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com>
This commit is contained in:
Andy Butland
2025-10-02 13:03:00 +02:00
committed by GitHub
parent cf61356b80
commit 436be6ec3f
3 changed files with 32 additions and 43 deletions

View File

@@ -186,8 +186,9 @@ public class PackageManifestReaderTests
.Setup(f => f.GetEnumerator())
.Returns(new List<IFileInfo> { CreatePackageManifestFile(content) }.GetEnumerator());
Assert.ThrowsAsync<JsonException>(() => _reader.ReadPackageManifestsAsync());
EnsureLogErrorWasCalled();
var exception = Assert.ThrowsAsync<InvalidOperationException>(() => _reader.ReadPackageManifestsAsync());
Assert.NotNull(exception);
Assert.IsInstanceOf<JsonException>(exception.InnerException);
}
[Test]
@@ -202,8 +203,9 @@ public class PackageManifestReaderTests
.Setup(f => f.GetEnumerator())
.Returns(new List<IFileInfo> { CreatePackageManifestFile(content) }.GetEnumerator());
Assert.ThrowsAsync<JsonException>(() => _reader.ReadPackageManifestsAsync());
EnsureLogErrorWasCalled();
var exception = Assert.ThrowsAsync<InvalidOperationException>(() => _reader.ReadPackageManifestsAsync());
Assert.NotNull(exception);
Assert.IsInstanceOf<JsonException>(exception.InnerException);
}
[Test]
@@ -224,8 +226,9 @@ public class PackageManifestReaderTests
.Setup(f => f.GetEnumerator())
.Returns(new List<IFileInfo> { CreatePackageManifestFile(content) }.GetEnumerator());
Assert.ThrowsAsync<JsonException>(() => _reader.ReadPackageManifestsAsync());
EnsureLogErrorWasCalled();
var exception = Assert.ThrowsAsync<InvalidOperationException>(() => _reader.ReadPackageManifestsAsync());
Assert.NotNull(exception);
Assert.IsInstanceOf<JsonException>(exception.InnerException);
}
[TestCase("This is not JSON")]
@@ -236,20 +239,11 @@ public class PackageManifestReaderTests
.Setup(f => f.GetEnumerator())
.Returns(new List<IFileInfo> { CreatePackageManifestFile(content) }.GetEnumerator());
Assert.ThrowsAsync<JsonException>(() => _reader.ReadPackageManifestsAsync());
EnsureLogErrorWasCalled();
var exception = Assert.ThrowsAsync<InvalidOperationException>(() => _reader.ReadPackageManifestsAsync());
Assert.NotNull(exception);
Assert.IsInstanceOf<JsonException>(exception.InnerException);
}
private void EnsureLogErrorWasCalled(int numberOfTimes = 1) =>
_loggerMock.Verify(
x => x.Log(
It.Is<LogLevel>(l => l == LogLevel.Error),
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => true),
It.IsAny<Exception>(),
It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => true)),
Times.Exactly(numberOfTimes));
private IFileInfo CreateDirectoryMock(string path, params IFileInfo[] children)
{
var directoryContentsMock = new Mock<IDirectoryContents>();