public async Task HandleAsync(HttpContext context, Exception ex)
        {
            if (_apiExtensionOptions?.Value?.DisableGlobalErrorHandling == true)
            {
                return;
            }

            var error = _mapper?.Resolve(ex);

            if (error == null)
            {
                return;
            }
            if (!string.IsNullOrWhiteSpace(error.Title) || !string.IsNullOrWhiteSpace(error.Code) || error.Type != null || error.ExtraParameters?.Any() == true)
            {
                context.Response.Clear();
                context.Response.ContentType = "application/problem+json";
                if (error.Status != default(int))
                {
                    context.Response.StatusCode = error.Status;
                }
                var json = JsonConvert.SerializeObject(error, _options?.Value?.SerializerSettings ?? new JsonSerializerSettings());
                await context.Response.WriteAsync(json);
            }
            else if (error.Status != default(int))
            {
                context.Response.StatusCode = error.Status;
            }
            LogException(error, ex);
        }
示例#2
0
        public async Task HandleAsync(HttpContext context, Exception ex)
        {
            var error = _exceptionMapper.Resolve(ex);

            context.Response.StatusCode = error.Status;
            await context.Response.WriteAsync(JsonConvert.SerializeObject((object)new { error }));
        }
        public async Task HandleAsync(HttpContext context, Exception ex)
        {
            var error = _mapper?.Resolve(ex);

            if (error == null)
            {
                return;
            }
            context.Response.Clear();
            context.Response.StatusCode  = error.Status;
            context.Response.ContentType = "application/json";
            var json = JsonConvert.SerializeObject(error, _options?.Value?.SerializerSettings ?? new JsonSerializerSettings());
            await context.Response.WriteAsync(json);

            LogException(error, ex);
        }
示例#4
0
 protected virtual IActionResult InternalServerError(Exception ex, string message, params object[] args)
 {
     if (ex == null)
     {
         Logger.LogError(message, args);
         var error = new ServerErrorException(string.Format(message, args));
         return(Content(new AjaxResult {
             state = ResultType.error.ToString(), message = _exceptionMapper.Resolve(error).ToString()
         }.ToJson()));
     }
     else
     {
         var errorMessage = String.Format(message, args);
         Logger.LogError("{0} : {1}", errorMessage, ExceptionHelper.GetAllToStrings(ex));
         var error = new ServerErrorException(string.Format("{0} : {1}", errorMessage, ex.Message));
         return(Content(new AjaxResult {
             state = ResultType.error.ToString(), message = _exceptionMapper.Resolve(error).ToString()
         }.ToJson()));
     }
 }
        public async Task HandleAsync(HttpContext context, Exception ex)
        {
            var error = _mapper?.Resolve(ex);

            if (error == null)
            {
                return;
            }

            context.Response.StatusCode = error.Status != 0 ? error.Status : context.Response.StatusCode;

            if (HasMeaningFullProperty(error))
            {
                context.Response.ContentType = "application/problem+json";

                var json = JsonConvert.SerializeObject(error, _options?.Value?.SerializerSettings ?? new JsonSerializerSettings());
                await context.Response.WriteAsync(json);
            }

            LogException(error, ex);
        }