protected virtual ExceptionResponse BuildResponseBody(Exception ex) { var res = new ExceptionResponse(ex.Message); if (ex is AggregateException aex) { _log?.LogDebug("AggregateException detected, use InternalException to full up Message"); res.Message = aex.InnerExceptions.FirstOrDefault()?.Message; } if (_isDetailsEnabled) { res.Details = ex.ToString(); } return(res); }
protected virtual async Task HandleResponse(HttpContext context, int status, ExceptionResponse body) { context.Response.StatusCode = status; if (context.Response.Body.CanWrite) { context.Response.ContentType = "application/json"; using (var writter = new StreamWriter(context.Response.Body)) { await writter.WriteAsync(JsonConvert.SerializeObject(body, Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, MaxDepth = 3, NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore, ContractResolver = _contractResolver })); } } else { _log?.LogWarning("Response body is not writtable stream, error message dropped."); } }