public async Task Invoke(HttpContext context) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; var ex = context.Features.Get <IExceptionHandlerFeature>()?.Error; if (ex == null) { return; } ErrorFormat err; if (this.env.IsDevelopment()) { err = new ErrorFormat(ex.Message, ex.StackTrace); } else { err = new ErrorFormat(DefaultErrorMessage, ex.Message); } using (var writer = new StreamWriter(context.Response.Body)) { this.serializer.Serialize(writer, err); await writer.FlushAsync().ConfigureAwait(false); } }
/// <summary> /// Called within exception catch blocks to format the exception.source property. /// Mostly for Trace.Writes. /// </summary> /// <param name="memberInfo">Current method info via reflection</param> /// <param name="errorFormat">Enum of ErrorFormat determines if Message or Source</param> /// <param name="errorMessage">exception.Message property</param> /// <param name="errorSource">exception.Source property</param> /// <returns>fomatted string</returns> public static string FormatError(MemberInfo memberInfo, ErrorFormat errorFormat, String errorMessage, string errorSource) { if (null == memberInfo) throw new ArgumentNullException("memberInfo"); if (string.IsNullOrEmpty(errorMessage)) { errorMessage = string.Empty; } switch (errorFormat) { case ErrorFormat.Message: return string.Format(CultureInfo.CurrentCulture, ErrorMessageFormat, memberInfo.ReflectedType.ToString(), memberInfo.ToString(), errorMessage, errorSource); default: // default to source return string.Format(CultureInfo.CurrentCulture, ErrorSourceFormat, memberInfo.ReflectedType.ToString(), memberInfo.ToString(), errorSource); } }