Contains information about an error that occurred.
Ejemplo n.º 1
0
        /// <summary>
        /// Saves the current <paramref name="instance" /> as an <see cref="XmlElement" />, using the specified
        /// <paramref name="ownerDocument" /> to create the element.
        /// </summary>
        /// <param name="instance">The exception instance.</param>
        /// <param name="ownerDocument">The document to use to create the element.</param>
        /// <param name="problemInfo">The problem info associated with the exception.</param>
        /// <returns>An XML representation of the current exception.</returns>
        protected internal virtual XmlElement ConvertToXml(Exception instance, XmlDocument ownerDocument, ProblemInfo problemInfo = null)
        {
            XmlElement exceptionElement = ownerDocument.CreateElement("exception");
            exceptionElement.SetAttribute("type", instance.GetType().ToString());
            exceptionElement.SetAttribute("message", instance.Message);
            exceptionElement.SetAttribute("htmlDescription", instance.Message
                .Replace("<", "&lt;")
                .Replace(">", "&gt;")
                .Replace("\t", "&#160;&#160;&#160;&#160;")
                .Replace("\n", "<br/>"));

            XmlElement straceNode = (XmlElement) exceptionElement.AppendChild(ownerDocument.CreateElement("stacktrace"));
            string[] stackTrace = instance.StackTrace != null
                ? instance.StackTrace.Split(new[] { '\n' })
                : new[] { string.Empty };

            if (instance.GetType() == typeof(XmlException))
            {
                exceptionElement.SetAttribute("sourceuri", ((XmlException) instance).SourceUri);
                exceptionElement.SetAttribute("linenumber", ((XmlException) instance).LineNumber.ToString());
                exceptionElement.SetAttribute("lineposition", ((XmlException) instance).LinePosition.ToString());
            }

            foreach (string t in stackTrace)
            {
                XmlElement frameNode = (XmlElement) straceNode.AppendChild(ownerDocument.CreateElement("frame"));
                Match match;
                if ((match = Regex.Match(t, "^\\s*at (.*) in (.*):line (\\d+)[\\s\\r]*$")).Success)
                {
                    frameNode.SetAttribute("text", match.Groups[1].Value);
                    frameNode.SetAttribute("file", match.Groups[2].Value);
                    frameNode.SetAttribute("line", match.Groups[3].Value);
                }
                else
                {
                    frameNode.SetAttribute("text", Regex.Replace(t, "^\\s*at ", string.Empty));
                }
            }

            if (problemInfo != null && problemInfo.InfoBlocks.Count != 0)
            {
                foreach (KeyValuePair<string, IDictionary<string, string>> infoBlock in problemInfo.InfoBlocks)
                {
                    XmlElement blockElement = exceptionElement.AppendElement("infoblock");
                    blockElement.SetAttribute("name", infoBlock.Key);

                    foreach (string key in infoBlock.Value.Keys)
                    {
                        string value = infoBlock.Value[key];
                        XmlElement lineElement = blockElement.AppendElement("line");
                        if (key != value)
                            lineElement.SetAttribute("name", key);

                        lineElement.InnerText = value;
                    }
                }
            }

            return exceptionElement;
        }
Ejemplo n.º 2
0
        internal static SageContext InitializeConfiguration(SageContext context)
        {
            string projectConfigPathBinDir = Path.Combine(Project.AssemblyCodeBaseDirectory, ProjectConfiguration.ProjectConfigName);
            string projectConfigPathProjDir = Path.Combine(Project.AssemblyCodeBaseDirectory, "..\\" + ProjectConfiguration.ProjectConfigName);

            string projectConfigPath = projectConfigPathBinDir;
            if (File.Exists(projectConfigPathProjDir))
            {
                projectConfigPath = projectConfigPathProjDir;
            }

            var projectConfig = ProjectConfiguration.Create();

            if (!File.Exists(projectConfigPath))
            {
                log.Warn("Project configuration file not found; configuration initialized with default values");
                return new SageContext(context);
            }

            installOrder = new List<string>();
            extensions = new OrderedDictionary<string, ExtensionInfo>();

            if (File.Exists(projectConfigPath))
                projectConfig.Parse(projectConfigPath);

            if (projectConfig.Locales.Count == 0)
            {
                var defaultLocale = new LocaleInfo();
                projectConfig.Locales.Add(defaultLocale.Name, defaultLocale);
            }

            var result = projectConfig.ValidationResult;
            if (!result.Success)
            {
                initializationError = result.Exception;
                initializationProblemInfo = new ProblemInfo(ProblemType.ProjectSchemaValidationError, result.SourceFile);
            }
            else
            {
                configuration = projectConfig;

                // this will ensure the new context uses the just
                // created configuration immediately
                context = new SageContext(context);

                var extensionManager = new ExtensionManager();
                try
                {
                    extensionManager.Initialize(context);
                }
                catch (ProjectInitializationException ex)
                {
                    initializationError = ex;
                    initializationProblemInfo = new ProblemInfo(ex.Reason, ex.SourceFile);
                    if (ex.Reason == ProblemType.MissingExtensionDependency)
                    {
                        initializationProblemInfo.InfoBlocks
                            .Add("Dependencies", ex.Dependencies.ToDictionary(name => name));
                    }
                }

                if (initializationError == null)
                {
                    var missingDependencies = projectConfig.Dependencies
                        .Where(name => extensionManager.Count(ex => ex.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) == 0)
                        .ToList();

                    var extraDependencies = extensionManager
                        .Where(extension => projectConfig.Dependencies.Count(name => name.Equals(extension.Name, StringComparison.InvariantCultureIgnoreCase)) == 0)
                        .ToList();

                    if (missingDependencies.Count != 0)
                    {
                        string errorMessage =
                            string.Format("Project is missing one or more dependencies ({0}) - installation cancelled.",
                            string.Join(", ", missingDependencies));

                        initializationError = new ProjectInitializationException(errorMessage);
                        initializationProblemInfo = new ProblemInfo(ProblemType.MissingDependency);
                        initializationProblemInfo.InfoBlocks
                            .Add("Dependencies", missingDependencies.ToDictionary(name => name));
                    }

                    if (extraDependencies.Count != 0)
                    {
                        log.WarnFormat("There are additional, unreferenced extensions in the extensions directory: {0}", string.Join(",", extraDependencies));
                    }

                    foreach (var name in projectConfig.Dependencies)
                    {
                        var extension = extensionManager.First(ex => ex.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
                        installOrder.Add(extension.Config.Id);
                        Project.RelevantAssemblies.AddRange(extension.Assemblies);

                        projectConfig.RegisterExtension(extension.Config);
                        extensions.Add(extension.Config.Id, extension);
                    }

                    // fire this event at the end rather than once for each extension
                    var totalAssemblies = extensionManager.Sum(info => info.Assemblies.Count);
                    if (totalAssemblies != 0)
                    {
                        if (Project.AssembliesUpdated != null)
                        {
                            log.DebugFormat("{0} extension assemblies loaded, triggering AssembliesUpdated event", totalAssemblies);
                            Project.AssembliesUpdated(null, EventArgs.Empty);
                        }
                    }

                    installOrder.Add(projectConfig.Id);
                    projectConfig.RegisterRoutes();
                    context.LmCache.Put(ConfigWatchName, DateTime.Now, projectConfig.Files);
                }
            }

            return context;
        }
Ejemplo n.º 3
0
        internal static SageHelpException Create(Exception ex, string path = null, ProblemType suggestedProblem = ProblemType.Unknown)
        {
            ProblemInfo problem = new ProblemInfo(ProblemType.Unknown);
            if (ex is XmlException)
            {
                if (ex.Message.Contains("undeclared prefix"))
                    problem = new ProblemInfo(ProblemType.MissingNamespaceDeclaration, path);

                else if (path != null && (path.EndsWith("html") || path.EndsWith("htm")))
                    problem = new ProblemInfo(ProblemType.InvalidHtmlMarkup, path);

                else
                    problem = new ProblemInfo(ProblemType.InvalidMarkup, path);
            }
            else
            {
                var typeName = ex.GetType().Name;
                if (parsers.ContainsKey(typeName))
                    problem = parsers[typeName].Invoke(ex);
            }

            if (problem.Type == ProblemType.Unknown && suggestedProblem != ProblemType.Unknown)
                problem.Type = suggestedProblem;

            var result = new SageHelpException(problem) { Exception = ex };
            return result;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SageHelpException"/> class.
 /// </summary>
 /// <param name="problem">An object that describes this error.</param>
 /// <param name="actual">The actual exception that occurred.</param>
 public SageHelpException(ProblemInfo problem, Exception actual)
     : base(actual)
 {
     this.Problem = problem;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SageHelpException"/> class.
 /// </summary>
 /// <param name="problem">An object that describes this error.</param>
 public SageHelpException(ProblemInfo problem)
 {
     this.Problem = problem;
 }
Ejemplo n.º 6
0
        private static ProblemInfo ParseEntityExceptionError(Exception e)
        {
            var errors = e.GetType().GetProperty("EntityValidationErrors").GetValue(e ) as IEnumerable;
            var errorMessage = new StringBuilder();

            var dictionary = new Dictionary<string, string>();
            foreach (var err1 in errors)
            {
                var validationErrors = err1.GetType().GetProperty("ValidationErrors").GetValue(err1) as IEnumerable;
                foreach (var err2 in validationErrors)
                {
                    var message = err2.GetType().GetProperty("ErrorMessage").GetValue(err2) as string;
                    var property = err2.GetType().GetProperty("PropertyName").GetValue(err2) as string;

                    dictionary.Add(property, message);
                }
            }

            var problem = new ProblemInfo(ProblemType.EntityValidationError);
            problem.InfoBlocks.Add("ValidationErrors", dictionary);
            return problem;
        }
Ejemplo n.º 7
0
 protected internal override XmlElement ConvertToXml(Exception instance, XmlDocument ownerDocument, ProblemInfo problemInfo = null)
 {
     return base.ConvertToXml(instance, ownerDocument, this.Problem);
 }