コード例 #1
0
        public Task HandleAsync(ExceptionContext context)
        {
            var category = _exceptionCategorizer.Categorizer(context.Exception);
            dynamic response = new ExpandoObject();

            response.System = new Dictionary<string, string>();
            response.System["Tracking Id"] = Guid.NewGuid().ToString();
            response.System["Timestamp"] = DateTimeOffset.Now.ToString();
            response.System["Message"] = category.ErrorMessage;
            response.System["Execution"] = "Global";

            if(context.Context.Request != null)
            {
                response.System["Execution"] = "Request";

                response.Developer = new Dictionary<string, string>();
                response.Developer["RequestMethod"] = context.Context.Request.Method;
                response.Developer["Uri"] = $"{context.Context.Request.Scheme}:{context.Context.Request.Host}{context.Context.Request.Path}";
                response.Developer["ExceptionType"] = context.Exception.GetType().FullName;
                response.Developer["StackTrace"] = context.Exception.StackTrace.Trim();
            }

            context.Context.Items["exception.category"] = category;
            context.Context.Items["exception.response"] = response;

            return Task.FromResult(0);
        }
コード例 #2
0
        /// <summary>
        /// Intercepts the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        /// <exception cref="System.AggregateException"></exception>
        async Task IExceptionInterceptManager.InterceptAsync(HttpContext context)
        {
            var feature = context.Features.Get<IExceptionHandlerFeature>();
            var exceptionContext = new ExceptionContext(context, feature?.Error);
            var handlerExecutionExceptions = new List<Exception>();

            try
            {
                foreach (var handlers in _exceptionIntercepts)
                {
                    try
                    {
                        await handlers.HandleAsync(exceptionContext);
                    }
                    catch (Exception ex)
                    {
                        handlerExecutionExceptions.Add(ex);
                    }
                }
            }
            catch(Exception ex)
            {
                handlerExecutionExceptions.Add(ex);
            }
            finally
            {
                if (handlerExecutionExceptions.Any())
                {
                    throw new AggregateException(handlerExecutionExceptions);
                }
            }
        }
コード例 #3
0
        public async Task HandleAsync(ExceptionContext context)
        {
            var category = (ExceptionCategory)context.Context.Items["exception.category"];
            dynamic response = context.Context.Items["exception.response"];
            dynamic finalResponse = category.DeveloperMode ? response : response.System;

            context.Context.Response.StatusCode = (int)category.HttpStatus;
            context.Context.Response.ContentType = "application/json";
            await context.Context.Response.WriteAsync((string)JsonConvert.SerializeObject(finalResponse));
        }
コード例 #4
0
        public Task HandleAsync(ExceptionContext context)
        {
            var category = (ExceptionCategory)context.Context.Items["exception.category"];
            if (category.Category == ExceptionCategoryType.Unhandled)
            {
                dynamic response = context.Context.Items["exception.response"];

                // log whatever to the JIRA for production issue tracking
            }

            return Task.FromResult(0);
        }
コード例 #5
0
        public Task HandleAsync(ExceptionContext context)
        {
            var category = (ExceptionCategory)context.Context.Items["exception.category"];
            if (category.Category == ExceptionCategoryType.Unhandled)
            {
                dynamic response = context.Context.Items["exception.response"];

                // log whatever to the Database
                // Note: Application Insights may be a more attractive analytical logger than rolling your own.
            }

            return Task.FromResult(0);
        }
コード例 #6
0
        public async Task HandleAsync(ExceptionContext context) 
        {
            // sample response
            var response = new
            {
                TicketId = Guid.NewGuid(),
                Timestamp = DateTime.UtcNow.ToString(),
                Message = "You are unauthorized to access this resource."
            };

            context.Context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
            context.Context.Response.ContentType = "application/json";
            await context.Context.Response.WriteAsync(JsonConvert.SerializeObject(response));
        }
コード例 #7
0
 public Task HandleAsync(ExceptionContext context)
 {
     _logger.LogDebug($"Logging '{context.Exception?.Message}' from '{this.GetType().FullName}'.");
     return Task.FromResult(0);
 }
コード例 #8
0
 public Task HandleAsync(ExceptionContext context)
 {
     return Task.FromResult(0);
 }