Skip to content

Commit

Permalink
Reduce the verbosity of activator exception messages; reverts autofac…
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Mar 3, 2019
1 parent 831973f commit a66856e
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 271 deletions.
9 changes: 0 additions & 9 deletions src/Autofac/Autofac.csproj
Expand Up @@ -112,11 +112,6 @@
<AutoGen>True</AutoGen>
<DependentUpon>ContainerResources.resx</DependentUpon>
</Compile>
<Compile Update="Core\DependencyResolutionExceptionResources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>DependencyResolutionExceptionResources.resx</DependentUpon>
</Compile>
<Compile Update="Core\Lifetime\LifetimeScopeResources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand Down Expand Up @@ -309,10 +304,6 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>ContainerResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="Core\DependencyResolutionExceptionResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>DependencyResolutionExceptionResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="Core\Lifetime\LifetimeScopeResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>LifetimeScopeResources.Designer.cs</LastGenOutput>
Expand Down
23 changes: 0 additions & 23 deletions src/Autofac/Core/DependencyResolutionException.cs
Expand Up @@ -24,7 +24,6 @@
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Globalization;

namespace Autofac.Core
{
Expand Down Expand Up @@ -57,27 +56,5 @@ public DependencyResolutionException(string message, Exception innerException)
: base(message, innerException)
{
}

/// <summary>
/// Gets a message that describes the current exception.
/// </summary>
/// <value>
/// The error message that explains the reason for the exception, or an empty string("").
/// </value>
public override string Message
{
get
{
// Issue 343: Including the inner exception message with the
// main message for easier debugging.
var message = base.Message;
if (InnerException == null)
return message;

var inner = InnerException.Message;
message = string.Format(CultureInfo.CurrentCulture, DependencyResolutionExceptionResources.MessageNestingFormat, message, inner);
return message;
}
}
}
}

This file was deleted.

123 changes: 0 additions & 123 deletions src/Autofac/Core/DependencyResolutionExceptionResources.resx

This file was deleted.

43 changes: 43 additions & 0 deletions src/Autofac/Core/Resolving/ActivatorExtensions.cs
@@ -0,0 +1,43 @@
// This software is part of the Autofac IoC container
// Copyright © 2011 Autofac Contributors
// https://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using Autofac.Core.Activators.Delegate;

namespace Autofac.Core.Resolving
{
internal static class ActivatorExtensions
{
// This shorthand name for the activator is used in exception messages; for activator types
// where the limit type generally describes the activator exactly, we use that; for delegate
// activators, a variation on the type name is used to indicate this.
public static string DisplayName(this IInstanceActivator activator)
{
var fullName = activator.LimitType.FullName ?? "";
return activator is DelegateActivator ?
$"λ:{fullName}" :
fullName;
}
}
}
2 changes: 1 addition & 1 deletion src/Autofac/Core/Resolving/CircularDependencyDetector.cs
Expand Up @@ -52,7 +52,7 @@ private static string CreateDependencyGraphTo(IComponentRegistration registratio

private static string Display(IComponentRegistration registration)
{
return registration.Activator.LimitType.FullName ?? string.Empty;
return registration.Activator.DisplayName();
}

public static void CheckForCircularDependency(IComponentRegistration registration, Stack<InstanceLookup> activationStack, int callDepth)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -121,7 +121,7 @@
<value>The activation has already been executed: {0}</value>
</data>
<data name="ErrorDuringActivation" xml:space="preserve">
<value>An error occurred during the activation of a particular registration. See the inner exception for details. Registration: {0}</value>
<value>An exception was thrown while activating {0}.</value>
</data>
<data name="UnableToLocateLifetimeScope" xml:space="preserve">
<value>Unable to resolve the type '{0}' because the lifetime scope it belongs in can't be located. The following services are exposed by this registration:
Expand Down
22 changes: 21 additions & 1 deletion src/Autofac/Core/Resolving/InstanceLookup.cs
Expand Up @@ -30,6 +30,8 @@
using System.Linq;
using System.Text;
using Autofac.Builder;
using Autofac.Core.Activators;
using Autofac.Core.Activators.Delegate;
using Autofac.Features.Decorators;

namespace Autofac.Core.Resolving
Expand All @@ -42,6 +44,7 @@ internal class InstanceLookup : IComponentContext, IInstanceLookup
private readonly ISharingLifetimeScope _activationScope;
private object _newInstance;
private bool _executed;
private const string ActivatorChainExceptionData = "ActivatorChain";

public InstanceLookup(
IComponentRegistration registration,
Expand Down Expand Up @@ -130,7 +133,7 @@ private object Activate(IEnumerable<Parameter> parameters, out object decoratorT
}
catch (Exception ex)
{
throw new DependencyResolutionException(String.Format(CultureInfo.CurrentCulture, ComponentActivationResources.ErrorDuringActivation, this.ComponentRegistration), ex);
throw PropagateActivationException(this.ComponentRegistration.Activator, ex);
}

if (ComponentRegistration.Ownership == InstanceOwnership.OwnedByLifetimeScope)
Expand All @@ -148,6 +151,23 @@ private object Activate(IEnumerable<Parameter> parameters, out object decoratorT
return _newInstance;
}

private static DependencyResolutionException PropagateActivationException(IInstanceActivator activator, Exception exception)
{
var activatorChain = activator.DisplayName();
var innerException = exception;

if (exception.Data.Contains(ActivatorChainExceptionData) &&
exception.Data[ActivatorChainExceptionData] is string innerChain)
{
activatorChain = activatorChain + " -> " + innerChain;
innerException = exception.InnerException;
}

var result = new DependencyResolutionException(String.Format(CultureInfo.CurrentCulture, ComponentActivationResources.ErrorDuringActivation, activatorChain), innerException);
result.Data[ActivatorChainExceptionData] = activatorChain;
return result;
}

public void Complete()
{
if (!NewInstanceActivated) return;
Expand Down

0 comments on commit a66856e

Please sign in to comment.