public async Task <IActionResult> Post([FromBody] ExceptionEntity ex)
        {
            //TODO: Move this check into a custom middleware handler
            var apiKey = Request.Headers["X-ApiKey"];

            if (string.IsNullOrEmpty(apiKey))
            {
                return(Unauthorized());
            }

            var userId = await this.configuration.GetUserByApiKey(apiKey);

            if (string.IsNullOrEmpty(userId))
            {
                return(Unauthorized());
            }

            var c = await this.configuration.GetConfiguration(userId);

            var settings = new ExceptionSettings(ex.ApplicationName,
                                                 c.AzureDevOpsServicesAccountUrl,
                                                 c.TeamProject, c.TargetAreaPath, c.AssignedTo, c.PersonalAccessToken);

            var registrator = new TfsStoreWithException(settings);

            //var registrator = new TFSStoreWithBug();
            registrator.RegisterException(ex);

            return(Ok());
        }
    private static string Emphasize(string input, char[] separators, Style color, bool compact,
                                    ExceptionSettings settings)
    {
        var builder = new StringBuilder();

        var type  = input;
        var index = type.LastIndexOfAny(separators);

        if (index != -1)
        {
            if (!compact)
            {
                builder.AppendWithStyle(
                    settings.Style.NonEmphasized,
                    type.Substring(0, index + 1));
            }

            builder.AppendWithStyle(
                color,
                type.Substring(index + 1, type.Length - index - 1));
        }
        else
        {
            builder.Append(type.EscapeMarkup());
        }

        return(builder.ToString());
    }
    private static void AppendPath(StringBuilder builder, string path, ExceptionSettings settings)
    {
        void AppendPath()
        {
            var shortenPaths = (settings.Format & ExceptionFormats.ShortenPaths) != 0;

            builder.Append(Emphasize(path, new[] { '/', '\\' }, settings.Style.Path, shortenPaths, settings));
        }

        if ((settings.Format & ExceptionFormats.ShowLinks) != 0)
        {
            var hasLink = path.TryGetUri(out var uri);
            if (hasLink && uri != null)
            {
                builder.Append("[link=").Append(uri.AbsoluteUri).Append(']');
            }

            AppendPath();

            if (hasLink && uri != null)
            {
                builder.Append("[/]");
            }
        }
        else
        {
            AppendPath();
        }
    }
Example #4
0
        private static Markup GetMessage(ExceptionInfo ex, ExceptionSettings settings)
        {
            var shortenTypes = (settings.Format & ExceptionFormats.ShortenTypes) != 0;
            var type         = Emphasize(ex.Type, new[] { '.' }, settings.Style.Exception, shortenTypes, settings);
            var message      = $"[{settings.Style.Message.ToMarkup()}]{ex.Message.EscapeMarkup()}[/]";

            return(new Markup(string.Concat(type, ": ", message)));
        }
    public static IRenderable Format(Exception exception, ExceptionSettings settings)
    {
        if (exception is null)
        {
            throw new ArgumentNullException(nameof(exception));
        }

        return(GetException(exception, settings));
    }
    private static IRenderable GetException(Exception exception, ExceptionSettings settings)
    {
        if (exception is null)
        {
            throw new ArgumentNullException(nameof(exception));
        }

        return(new Rows(GetMessage(exception, settings), GetStackFrames(exception, settings)).Expand());
    }
    private static void AppendParameters(StringBuilder builder, MethodBase?method, ExceptionSettings settings)
    {
        var typeColor  = settings.Style.ParameterType.ToMarkup();
        var nameColor  = settings.Style.ParameterName.ToMarkup();
        var parameters = method?.GetParameters()
                         .Select(x => $"[{typeColor}]{GetParameterName(x).EscapeMarkup()}[/] [{nameColor}]{x.Name?.EscapeMarkup()}[/]");

        if (parameters != null)
        {
            builder.Append(string.Join(", ", parameters));
        }
    }
        private static Grid GetStackFrames(ExceptionInfo ex, ExceptionSettings settings)
        {
            var styles = settings.Style;

            var grid = new Grid();

            grid.AddColumn(new GridColumn().PadLeft(2).PadRight(0).NoWrap());
            grid.AddColumn(new GridColumn().PadLeft(1).PadRight(0));

            // Inner
            if (ex.Inner != null)
            {
                grid.AddRow(
                    Text.Empty,
                    GetException(ex.Inner, settings));
            }

            // Stack frames
            foreach (var frame in ex.Frames)
            {
                var builder = new StringBuilder();

                // Method
                var shortenMethods = (settings.Format & ExceptionFormats.ShortenMethods) != 0;
                builder.Append(Emphasize(frame.Method, new[] { '.' }, styles.Method, shortenMethods, settings));
                builder.AppendWithStyle(styles.Parenthesis, "(");
                AppendParameters(builder, frame, settings);
                builder.AppendWithStyle(styles.Parenthesis, ")");

                if (frame.Path != null)
                {
                    builder.Append(' ');
                    builder.AppendWithStyle(styles.Dimmed, "in");
                    builder.Append(' ');

                    // Path
                    AppendPath(builder, frame, settings);

                    // Line number
                    if (frame.LineNumber != null)
                    {
                        builder.AppendWithStyle(styles.Dimmed, ":");
                        builder.AppendWithStyle(styles.LineNumber, frame.LineNumber);
                    }
                }

                grid.AddRow(
                    $"[{styles.Dimmed.ToMarkup()}]at[/]",
                    builder.ToString());
            }

            return(grid);
        }
        private static IRenderable GetException(ExceptionInfo info, ExceptionSettings settings)
        {
            if (info is null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            return(new Rows(new IRenderable[]
            {
                GetMessage(info, settings),
                GetStackFrames(info, settings),
            }).Expand());
        }
Example #10
0
    /// <summary>
    /// Gets a <see cref="IRenderable"/> representation of the exception.
    /// </summary>
    /// <param name="exception">The exception to format.</param>
    /// <param name="settings">The exception settings.</param>
    /// <returns>A <see cref="IRenderable"/> representing the exception.</returns>
    public static IRenderable GetRenderable(this Exception exception, ExceptionSettings settings)
    {
        if (exception is null)
        {
            throw new ArgumentNullException(nameof(exception));
        }

        if (settings is null)
        {
            throw new ArgumentNullException(nameof(settings));
        }

        return(ExceptionFormatter.Format(exception, settings));
    }
        public static IRenderable Format(Exception exception, ExceptionSettings settings)
        {
            if (exception is null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            var info = ExceptionParser.Parse(exception.ToString());

            if (info == null)
            {
                return(new Text(exception.ToString()));
            }

            return(GetException(info, settings));
        }
Example #12
0
 public ExceptionHandler(IEmailService emailService, IOptions<ExceptionSettings> settings, IQueueProvider queueProvider)
 {
     _emailService = emailService;
     _queueProvider = queueProvider;
     _settings = settings.Value;
 }
 /// <summary>
 /// Writes an exception to the console.
 /// </summary>
 /// <param name="exception">The exception to write to the console.</param>
 /// <param name="settings">The exception settings.</param>
 public static void WriteException(Exception exception, ExceptionSettings settings)
 {
     Console.WriteException(exception, settings);
 }
    private static Grid GetStackFrames(Exception ex, ExceptionSettings settings)
    {
        var styles = settings.Style;

        var grid = new Grid();

        grid.AddColumn(new GridColumn().PadLeft(2).PadRight(0).NoWrap());
        grid.AddColumn(new GridColumn().PadLeft(1).PadRight(0));

        // Inner
        if (ex.InnerException != null)
        {
            grid.AddRow(
                Text.Empty,
                GetException(ex.InnerException, settings));
        }

        // Stack frames
        var stackTrace = new StackTrace(ex, fNeedFileInfo: true);
        var frames     = stackTrace
                         .GetFrames()
                         .FilterStackFrames()
                         .ToList();

        foreach (var frame in frames)
        {
            var builder = new StringBuilder();

            // Method
            var shortenMethods = (settings.Format & ExceptionFormats.ShortenMethods) != 0;
            var method         = frame.GetMethod();
            if (method == null)
            {
                continue;
            }

            var methodName = GetMethodName(ref method, out var isAsync);
            if (isAsync)
            {
                builder.Append("async ");
            }

            if (method is MethodInfo mi)
            {
                var returnParameter = mi.ReturnParameter;
                builder.AppendWithStyle(styles.ParameterType, GetParameterName(returnParameter).EscapeMarkup());
                builder.Append(' ');
            }

            builder.Append(Emphasize(methodName, new[] { '.' }, styles.Method, shortenMethods, settings));
            builder.AppendWithStyle(styles.Parenthesis, "(");
            AppendParameters(builder, method, settings);
            builder.AppendWithStyle(styles.Parenthesis, ")");

            var path = frame.GetFileName();
            if (path != null)
            {
                builder.Append(' ');
                builder.AppendWithStyle(styles.Dimmed, "in");
                builder.Append(' ');

                // Path
                AppendPath(builder, path, settings);

                // Line number
                var lineNumber = frame.GetFileLineNumber();
                if (lineNumber != 0)
                {
                    builder.AppendWithStyle(styles.Dimmed, ":");
                    builder.AppendWithStyle(styles.LineNumber, lineNumber);
                }
            }

            grid.AddRow(
                $"[{styles.Dimmed.ToMarkup()}]at[/]",
                builder.ToString());
        }

        return(grid);
    }
 /// <summary>
 /// Writes an exception to the console.
 /// </summary>
 /// <param name="console">The console.</param>
 /// <param name="exception">The exception to write to the console.</param>
 /// <param name="settings">The exception settings.</param>
 public static void WriteException(this IAnsiConsole console, Exception exception, ExceptionSettings settings)
 {
     console.Write(exception.GetRenderable(settings));
 }
        private static void AppendParameters(StringBuilder builder, StackFrameInfo frame, ExceptionSettings settings)
        {
            var typeColor  = settings.Style.ParameterType.ToMarkup();
            var nameColor  = settings.Style.ParameterName.ToMarkup();
            var parameters = frame.Parameters.Select(x => $"[{typeColor}]{x.Type.EscapeMarkup()}[/] [{nameColor}]{x.Name.EscapeMarkup()}[/]");

            builder.Append(string.Join(", ", parameters));
        }