< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.WebErrorHandler
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/WebErrorHandler.cs
Line coverage
47%
Covered lines: 29
Uncovered lines: 32
Coverable lines: 61
Total lines: 175
Line coverage: 47.5%
Branch coverage
34%
Covered branches: 16
Total branches: 46
Branch coverage: 34.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
HandleError(...)100%11100%
ProvideFault(...)22.22%363618.91%
CreateHtmlResponse(...)87.5%88100%
GetResponseProperty(...)50%2260%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/WebErrorHandler.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.IO;
 6using System.Net;
 7using System.Runtime.Serialization;
 8using System.Runtime.Serialization.Json;
 9using System.Xml.Linq;
 10using System.Xml.Serialization;
 11using CoreWCF.Channels;
 12using CoreWCF.Description;
 13using CoreWCF.Runtime;
 14using CoreWCF.Web;
 15
 16namespace CoreWCF.Dispatcher
 17{
 18    internal class WebErrorHandler : IErrorHandler
 19    {
 20        private const string HtmlMediaType = "text/html";
 21
 22        private readonly WebHttpBehavior _webHttpBehavior;
 23        private readonly ContractDescription _contractDescription;
 24        private readonly bool _includeExceptionDetailInFaults;
 25
 3726        public WebErrorHandler(WebHttpBehavior webHttpBehavior, ContractDescription contractDescription, bool includeExc
 27        {
 3728            _webHttpBehavior = webHttpBehavior;
 3729            _contractDescription = contractDescription;
 3730            _includeExceptionDetailInFaults = includeExceptionDetailInFaults;
 3731        }
 32
 433        public bool HandleError(Exception error) => false;
 34
 35        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
 36        {
 437            if (version != MessageVersion.None || error == null)
 38            {
 039                return;
 40            }
 41
 42            // If the exception is not derived from FaultException and the fault message is already present
 43            //   then only another error handler could have provided the fault so we should not replace it
 444            FaultException errorAsFaultException = error as FaultException;
 445            if (errorAsFaultException == null && fault != null)
 46            {
 047                return;
 48            }
 49
 50            try
 51            {
 452                if (error is IWebFaultException webFaultException)
 53                {
 054                    WebOperationContext context = WebOperationContext.Current;
 055                    context.OutgoingResponse.StatusCode = webFaultException.StatusCode;
 056                    if (OperationContext.Current.IncomingMessageProperties.TryGetValue(WebHttpDispatchOperationSelector.
 57                    {
 058                        OperationDescription description = _contractDescription.Operations.Find(operationName);
 059                        bool isXmlSerializerFaultFormat = WebHttpBehavior.IsXmlSerializerFaultFormat(description);
 060                        if (isXmlSerializerFaultFormat && WebOperationContext.Current.OutgoingResponse.Format == WebMess
 61                        {
 062                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.F
 63                        }
 64
 065                        WebMessageFormat? nullableFormat = !isXmlSerializerFaultFormat ? context.OutgoingResponse.Format
 066                        WebMessageFormat format = nullableFormat.HasValue ? nullableFormat.Value : _webHttpBehavior.GetR
 067                        if (webFaultException.DetailObject != null)
 68                        {
 69                            switch (format)
 70                            {
 71                                case WebMessageFormat.Json:
 072                                    fault = context.CreateJsonResponse(webFaultException.DetailObject, new DataContractJ
 073                                    break;
 74                                case WebMessageFormat.Xml:
 075                                    if (isXmlSerializerFaultFormat)
 76                                    {
 077                                        fault = context.CreateXmlResponse(webFaultException.DetailObject, new XmlSeriali
 78                                    }
 79                                    else
 80                                    {
 081                                        fault = context.CreateXmlResponse(webFaultException.DetailObject, new DataContra
 82                                    }
 083                                    break;
 84                            }
 85                        }
 86                        else
 87                        {
 088                            if (OperationContext.Current.OutgoingMessageProperties.TryGetValue(HttpResponseMessageProper
 089                                property != null)
 90                            {
 091                                property.SuppressEntityBody = true;
 92                            }
 93
 094                            if (format == WebMessageFormat.Json)
 95                            {
 096                                fault.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.Jso
 97                            }
 98                        }
 99                    }
 100                    else
 101                    {
 0102                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Opera
 103                    }
 104                }
 105                else
 106                {
 4107                    fault = CreateHtmlResponse(error);
 108                }
 109
 4110            }
 0111            catch (Exception ex)
 112            {
 0113                if (Fx.IsFatal(ex))
 114                {
 0115                    throw;
 116                }
 117
 118                //if (DiagnosticUtility.ShouldTraceWarning)
 119                //{
 120                //    DiagnosticUtility.TraceHandledException(new InvalidOperationException(SR.Format(SR.HelpPageFailedT
 121                //}
 122
 0123                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
 0124                fault = CreateHtmlResponse(ex);
 0125            }
 4126        }
 127
 128        private Message CreateHtmlResponse(Exception error)
 129        {
 130            // Note: WebOperationContext may not be present in case of an invalid HTTP request
 4131            Uri helpUri = null;
 4132            if (WebOperationContext.Current != null)
 133            {
 4134                helpUri = _webHttpBehavior.HelpUri != null ? UriTemplate.RewriteUri(_webHttpBehavior.HelpUri, WebOperati
 135            }
 136
 137            StreamBodyWriter bodyWriter;
 4138            if (_includeExceptionDetailInFaults)
 139            {
 6140                bodyWriter = ActionOfStreamBodyWriter.CreateStreamBodyWriter(s => HelpHtmlBuilder.CreateServerErrorPage(
 141            }
 142            else
 143            {
 2144                bodyWriter = ActionOfStreamBodyWriter.CreateStreamBodyWriter(s => HelpHtmlBuilder.CreateServerErrorPage(
 145            }
 4146            Message response = new HttpStreamMessage(bodyWriter);
 4147            response.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
 148
 4149            HttpResponseMessageProperty responseProperty = GetResponseProperty(WebOperationContext.Current, response);
 4150            if (responseProperty.StatusCode == HttpStatusCode.OK)
 151            {
 4152                responseProperty.StatusCode = HttpStatusCode.BadRequest;
 153            }
 154
 4155            responseProperty.Headers[HttpResponseHeader.ContentType] = HtmlMediaType;
 4156            return response;
 157        }
 158
 159        private static HttpResponseMessageProperty GetResponseProperty(WebOperationContext currentContext, Message respo
 160        {
 161            HttpResponseMessageProperty responseProperty;
 4162            if (currentContext != null)
 163            {
 4164                responseProperty = currentContext.OutgoingResponse.MessageProperty;
 165            }
 166            else
 167            {
 0168                responseProperty = new HttpResponseMessageProperty();
 0169                response.Properties.Add(HttpResponseMessageProperty.Name, responseProperty);
 170            }
 171
 4172            return responseProperty;
 173        }
 174    }
 175}