Suggestion: more comprehensive error reporting

Jul 9, 2008 at 9:54 PM

First off - Unity is sweeetttttt :)  Great job!!!

The stacktrace is getting lost resulting in an error that can send newbies (like me) down a rabbit trail.  I am refactoring an application which which relies on a factory and numerous concrete classes which have been retrofitted for Unity.   Initially I intrepeted errors that I was recieving to be configuration errors, later I learned that they were actually null object references dangling during the conversion. e.g., a reference that I missed resulted in the following error:

Resolution of the dependency failed, type = "Form1", name = "". Exception message is: The current build operation (build key Build Key[TestHarness.Form1, null]) failed: The value for the property "Presenter" could not be resolved. (Strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2)

After updating the ResolutionFailedException.cs CreateMessage() as follows:

        private static string CreateMessage(Type typeRequested, string nameRequested, Exception innerException)
        {
            return string.Format(
                CultureInfo.CurrentCulture,
                Resources.ResolutionFailed,
                typeRequested.Name,
                nameRequested,
                GetInnerExceptionMessage(innerException));
        }

        /// <summary>
        /// Recursive routine to collect all inner exception errors
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        private static string GetInnerExceptionMessage(Exception ex)
        {
            StringBuilder errorMessage = new StringBuilder(ex.InnerException.Message);
            errorMessage.Append(ex.InnerException.StackTrace);
            errorMessage.Append("\r\n");

            if (ex.InnerException.InnerException != null)
                errorMessage.Append(GetInnerExceptionMessage(ex.InnerException.InnerException));

            return errorMessage.ToString();
        }

I received the following error message which allows me to isolate issues more quickly:

Resolution of the dependency failed, type = "Form1", name = "". Exception message is: The value for the property "Presenter" could not be resolved.   at Microsoft.Practices.ObjectBuilder2.DynamicMethodPropertySetterStrategy.ThrowOnFailedPropertyValueResolution(Exception inner, String propertyName) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Property\DynamicMethodPropertySetterStrategy.cs:line 82
   at BuildUp_TestHarness.Form1(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicMethodBuildPlan.cs:line 37
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:line 40
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 92
The value for the property "ImageViewer" could not be resolved.   at Microsoft.Practices.ObjectBuilder2.DynamicMethodPropertySetterStrategy.ThrowOnFailedPropertyValueResolution(Exception inner, String propertyName) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Property\DynamicMethodPropertySetterStrategy.cs:line 82
   at BuildUp_TestHarness.TestHarnessPresenter(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicMethodBuildPlan.cs:line 37
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:line 40
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 92
Object reference not set to an instance of an object.   at TestHarness.TestHarnessPresenter._imageProcessorService_OnWriteMessage(Object sender, LoggerEventArgs e) in C:\UEApp\ImageProcessor\TestHarness\TestHarnessPresenter.cs:line 148
   at ImageProcessor.Library.ImageProcessorService._logger_OnWriteMessage(Object sender, LoggerEventArgs e) in C:\UEApp\ImageProcessor\ImageProcessor.Library\ImageProcessorService.cs:line 59
   at ImageProcessor.Library.TraceLogger.Write(String message, TraceLevel level) in C:\UEApp\ImageProcessor\ImageProcessor.Library\TraceLogger.cs:line 93
   at ImageProcessor.Library.TraceLogger.WriteVerbose(String message) in C:\UEApp\ImageProcessor\ImageProcessor.Library\TraceLogger.cs:line 129
   at LTLibrary.LTRasterImageViewer.LTRasterImageViewer.SetPercentage() in C:\UEApp\ImageProcessor\LTLibrary\LTRasterImageViewer\LTRasterImageViewer.cs:line 457
   at LTLibrary.LTRasterImageViewer.LTRasterImageViewer..ctor(ILogger logger) in C:\UEApp\ImageProcessor\LTLibrary\LTRasterImageViewer\LTRasterImageViewer.cs:line 59
   at BuildUp_LTLibrary.LTRasterImageViewer.LTRasterImageViewer(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicMethodBuildPlan.cs:line 37
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:line 40
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in C:\VisualStudio2008\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 92