ServiceStack.HttpResponseExtensionsInternal.WriteErrorToResponse C# (CSharp) Method

WriteErrorToResponse() public static method

public static WriteErrorToResponse ( this httpRes, IRequest httpReq, string contentType, string operationName, string errorMessage, Exception ex, int statusCode ) : System.Threading.Tasks.Task
httpRes this
httpReq IRequest
contentType string
operationName string
errorMessage string
ex System.Exception
statusCode int
return System.Threading.Tasks.Task
        public static Task WriteErrorToResponse(this IResponse httpRes, IRequest httpReq,
            string contentType, string operationName, string errorMessage, Exception ex, int statusCode)
        {
            if (ex == null)
                ex = new Exception(errorMessage);

            var errorDto = ex.ToErrorResponse();
            HostContext.AppHost.OnExceptionTypeFilter(ex, errorDto.ResponseStatus);

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

            if ((httpRes.ContentType == null || httpRes.ContentType == MimeTypes.Html) 
                && contentType != null && contentType != httpRes.ContentType)
            {
                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;

            httpRes.ApplyGlobalResponseHeaders();

            var serializer = HostContext.ContentTypes.GetResponseSerializer(contentType);
            serializer?.Invoke(httpReq, errorDto, httpRes);

            httpRes.EndHttpHandlerRequest(skipHeaders: true);

            return TypeConstants.EmptyTask;
        }