From 86742755e8216ed1a02907b05f6c0d522bfe672c Mon Sep 17 00:00:00 2001 From: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com> Date: Sat, 16 Apr 2022 13:54:44 +0000 Subject: [PATCH] Don't wrap default type activator exception if it's not related to constructor --- CliFx/Infrastructure/DefaultTypeActivator.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CliFx/Infrastructure/DefaultTypeActivator.cs b/CliFx/Infrastructure/DefaultTypeActivator.cs index 0edc320..d541c99 100644 --- a/CliFx/Infrastructure/DefaultTypeActivator.cs +++ b/CliFx/Infrastructure/DefaultTypeActivator.cs @@ -16,14 +16,15 @@ public class DefaultTypeActivator : ITypeActivator { return Activator.CreateInstance(type); } - catch (Exception ex) + // Only catch MemberAccessException because the constructor can throw for its own reasons too + catch (MemberAccessException ex) { throw CliFxException.InternalError( - $"Failed to create an instance of type `{type.FullName}`." + + $"Failed to create an instance of type `{type.FullName}`, could not access the constructor." + Environment.NewLine + "Default type activator is only capable of instantiating a type if it has a public parameterless constructor." + Environment.NewLine + - "To fix this, either add a parameterless constructor to the type or configure a custom activator on the application.", + "To fix this, either add a parameterless constructor to the type or configure a custom activator for the application.", ex ); }