public static void WriteErrorToResponse(this IHttpResponse httpRes, IHttpRequest httpReq,
            string contentType, string operationName, string errorMessage, Exception ex, int statusCode)
        {
            var errorDto = ex.ToErrorResponse();
            if (HandleCustomErrorHandler(httpRes, httpReq, contentType, statusCode, errorDto)) return;

            if (httpRes.ContentType == null || httpRes.ContentType == ContentType.Html)
            {
                httpRes.ContentType = contentType;
            }
            if (EndpointHost.Config.AppendUtf8CharsetOnContentTypes.Contains(contentType))
            {
                httpRes.ContentType += ContentType.Utf8Suffix;
            }

            httpRes.StatusCode = statusCode;
            var serializationCtx = new SerializationContext(contentType);

            var serializer = EndpointHost.AppHost.ContentTypeFilters.GetResponseSerializer(contentType);
            if (serializer != null)
            {
                serializer(serializationCtx, errorDto, httpRes);
            }
            
            httpRes.EndHttpRequest(skipHeaders: true);
        }
        public static Task WriteErrorToResponse(this IResponse httpRes, IRequest httpReq,
            string contentType, string operationName, string errorMessage, Exception ex, int statusCode)
        {
            var errorDto = ex.ToErrorResponse();
            if (HandleCustomErrorHandler(httpRes, httpReq, contentType, statusCode, errorDto)) 
                return EmptyTask;

            if (httpRes.ContentType == null || httpRes.ContentType == MimeTypes.Html)
            {
                httpRes.ContentType = contentType;
            }
            if (HostContext.Config.AppendUtf8CharsetOnContentTypes.Contains(contentType))
            {
                httpRes.ContentType += ContentFormat.Utf8Suffix;
            }

            httpRes.StatusCode = statusCode;
            var serializer = HostContext.ContentTypes.GetResponseSerializer(contentType);
            if (serializer != null)
            {
                serializer(httpReq, errorDto, httpRes);
            }
            
            httpRes.EndHttpHandlerRequest(skipHeaders: true);

            return EmptyTask;
        }
        public static Task WriteErrorToResponse(this IResponse httpRes, IRequest httpReq,
            string contentType, string operationName, string errorMessage, Exception ex, int statusCode)
        {
            var errorDto = ex.ToErrorResponse();
            HostContext.OnExceptionTypeFilter(ex, errorDto.ResponseStatus);

            if (HandleCustomErrorHandler(httpRes, httpReq, contentType, statusCode, errorDto))
                return TypeConstants.EmptyTask;

            if (httpRes.ContentType == null || httpRes.ContentType == MimeTypes.Html)
            {
                httpRes.ContentType = contentType;
            }
            if (HostContext.Config.AppendUtf8CharsetOnContentTypes.Contains(contentType))
            {
                httpRes.ContentType += ContentFormat.Utf8Suffix;
            }

            var hold = httpRes.StatusDescription;
            var hasDefaultStatusDescription = hold == null || hold == "OK";

            httpRes.StatusCode = statusCode;

            httpRes.StatusDescription = hasDefaultStatusDescription
                ? (errorMessage ?? HttpStatus.GetStatusDescription(statusCode))
                : hold;

            var serializer = HostContext.ContentTypes.GetResponseSerializer(contentType);
            if (serializer != null)
            {
                serializer(httpReq, errorDto, httpRes);
            }

            httpRes.EndHttpHandlerRequest(skipHeaders: true);

            return TypeConstants.EmptyTask;
        }
 /// <summary>
 /// When HTTP Headers have already been written and only the Body can be written
 /// </summary>
 public static Task WriteErrorBody(this IResponse httpRes, Exception ex)
 {
     var req = httpRes.Request;
     var errorDto = ex.ToErrorResponse();
     HostContext.AppHost.OnExceptionTypeFilter(ex, errorDto.ResponseStatus);
     var serializer = HostContext.ContentTypes.GetResponseSerializer(MimeTypes.Html);
     serializer?.Invoke(req, errorDto, httpRes);
     httpRes.EndHttpHandlerRequest(skipHeaders: true);
     return TypeConstants.EmptyTask;
 }