diff --git a/CHANGELOG.md b/CHANGELOG.md index 511b8c384..eae31865b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,11 @@ ## Improvements: +* Enhance BoDi error handling to provide the name of the interface being registered when that interface has already been resolved (#324) + ## Bug fixes: -*Contributors of this release (in alphabetical order):* @obligaron +*Contributors of this release (in alphabetical order):* @clrudolphi, @obligaron # v2.2.1 - 2024-11-08 diff --git a/Reqnroll/BoDi/ObjectContainer.cs b/Reqnroll/BoDi/ObjectContainer.cs index 9f7df16c6..4b14d39b8 100644 --- a/Reqnroll/BoDi/ObjectContainer.cs +++ b/Reqnroll/BoDi/ObjectContainer.cs @@ -364,7 +364,7 @@ public IStrategyRegistration RegisterTypeAs(string name = nul public IStrategyRegistration RegisterTypeAs(Type implementationType, Type interfaceType, string name = null) { if (!IsValidTypeMapping(implementationType, interfaceType)) - throw new InvalidOperationException("type mapping is not valid"); + throw new InvalidOperationException($"The type mapping is not valid. The {implementationType} is not assignable to the interface {interfaceType}"); return RegisterTypeAsInternal(implementationType, interfaceType, name); } @@ -500,7 +500,7 @@ public bool IsRegistered(Type type, string name = null) private void AssertNotResolved(RegistrationKey interfaceType) { if (_resolvedKeys.Contains(interfaceType)) - throw new ObjectContainerException("An object has been resolved for this interface already.", null); + throw new ObjectContainerException($"An object has already been resolved for the interface \"{interfaceType}\".", null); } private void ClearRegistrations(RegistrationKey registrationKey) diff --git a/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs b/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs index 762dfff5d..9af8d5e2a 100644 --- a/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs +++ b/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs @@ -65,7 +65,8 @@ public void ShouldNotAllowOverrideRegistrationAfterResolve() // when Action act = () => container.RegisterTypeAs(); - act.Should().ThrowExactly(); + act.Should().ThrowExactly() + .And.Message.Should().Contain("IInterface1"); } [Fact]