| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using CoreWCF.Channels; |
| | | 6 | | using CoreWCF.Diagnostics; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Dispatcher |
| | | 10 | | { |
| | | 11 | | internal class ErrorBehavior |
| | | 12 | | { |
| | | 13 | | private readonly bool _debug; |
| | | 14 | | private readonly bool _isOnServer; |
| | | 15 | | private readonly MessageVersion _messageVersion; |
| | | 16 | | |
| | 1324 | 17 | | internal ErrorBehavior(ChannelDispatcher channelDispatcher) |
| | | 18 | | { |
| | 1324 | 19 | | if (channelDispatcher?.ErrorHandlers == null) |
| | | 20 | | { |
| | 0 | 21 | | Handlers = EmptyArray<IErrorHandler>.Allocate(0); |
| | | 22 | | } |
| | | 23 | | else |
| | | 24 | | { |
| | 1324 | 25 | | Handlers = EmptyArray<IErrorHandler>.ToArray(channelDispatcher.ErrorHandlers); |
| | | 26 | | } |
| | 1324 | 27 | | _debug = channelDispatcher.IncludeExceptionDetailInFaults; |
| | | 28 | | //isOnServer = channelDispatcher.IsOnServer; |
| | 1324 | 29 | | _isOnServer = true; |
| | 1324 | 30 | | _messageVersion = channelDispatcher.MessageVersion; |
| | 1324 | 31 | | } |
| | | 32 | | |
| | | 33 | | private void InitializeFault(MessageRpc rpc) |
| | | 34 | | { |
| | 163 | 35 | | Exception error = rpc.Error; |
| | 163 | 36 | | if (error is FaultException fault) |
| | | 37 | | { |
| | 103 | 38 | | MessageFault messageFault = rpc.Operation.FaultFormatter.Serialize(fault, out string action); |
| | 103 | 39 | | if (action == null) |
| | | 40 | | { |
| | 38 | 41 | | action = rpc.RequestVersion.Addressing.DefaultFaultAction; |
| | | 42 | | } |
| | | 43 | | |
| | 103 | 44 | | if (messageFault != null) |
| | | 45 | | { |
| | 103 | 46 | | rpc.FaultInfo.Fault = Message.CreateMessage(rpc.RequestVersion, messageFault, action); |
| | | 47 | | } |
| | | 48 | | } |
| | 163 | 49 | | } |
| | | 50 | | |
| | 365 | 51 | | internal IErrorHandler[] Handlers { get; } |
| | | 52 | | |
| | | 53 | | internal void ProvideMessageFault(MessageRpc rpc) |
| | | 54 | | { |
| | 2538 | 55 | | if (rpc.Error != null) |
| | | 56 | | { |
| | 163 | 57 | | ProvideMessageFaultCore(rpc); |
| | | 58 | | } |
| | 2538 | 59 | | } |
| | | 60 | | |
| | | 61 | | private void ProvideMessageFaultCore(MessageRpc rpc) |
| | | 62 | | { |
| | 163 | 63 | | if (_messageVersion != rpc.RequestVersion) |
| | | 64 | | { |
| | | 65 | | Fx.Assert("CoreWCF.Dispatcher.ErrorBehavior.ProvideMessageFaultCore(): (this.messageVersion != rpc.Reque |
| | | 66 | | } |
| | | 67 | | |
| | 163 | 68 | | InitializeFault(rpc); |
| | | 69 | | |
| | 163 | 70 | | ProvideFault(rpc.Error, rpc.Channel.GetProperty<FaultConverter>(), ref rpc.FaultInfo); |
| | | 71 | | |
| | 163 | 72 | | ProvideMessageFaultCoreCoda(rpc); |
| | 163 | 73 | | } |
| | | 74 | | |
| | | 75 | | private void ProvideFaultOfLastResort(Exception error, ref ErrorHandlerFaultInfo faultInfo) |
| | | 76 | | { |
| | 182 | 77 | | if (faultInfo.Fault == null) |
| | | 78 | | { |
| | 75 | 79 | | FaultCode code = new FaultCode(FaultCodeConstants.Codes.InternalServiceFault, FaultCodeConstants.Namespa |
| | 75 | 80 | | code = FaultCode.CreateReceiverFaultCode(code); |
| | 75 | 81 | | string action = FaultCodeConstants.Actions.NetDispatcher; |
| | | 82 | | MessageFault fault; |
| | 75 | 83 | | if (_debug) |
| | | 84 | | { |
| | 4 | 85 | | faultInfo.DefaultFaultAction = action; |
| | 4 | 86 | | fault = MessageFault.CreateFault(code, new FaultReason(error.Message), new ExceptionDetail(error)); |
| | | 87 | | } |
| | | 88 | | else |
| | | 89 | | { |
| | 71 | 90 | | string reason = _isOnServer ? SR.SFxInternalServerError : SR.SFxInternalCallbackError; |
| | 71 | 91 | | fault = MessageFault.CreateFault(code, new FaultReason(reason)); |
| | | 92 | | } |
| | 75 | 93 | | faultInfo.IsConsideredUnhandled = true; |
| | 75 | 94 | | faultInfo.Fault = Message.CreateMessage(_messageVersion, fault, action); |
| | | 95 | | } |
| | | 96 | | //if this is an InternalServiceFault coming from another service dispatcher we should treat it as unhandled |
| | 107 | 97 | | else if (error != null) |
| | | 98 | | { |
| | 107 | 99 | | if (error is FaultException e && e.Fault != null && e.Fault.Code != null && e.Fault.Code.SubCode != null |
| | 107 | 100 | | string.Compare(e.Fault.Code.SubCode.Namespace, FaultCodeConstants.Namespaces.NetDispatch, StringComp |
| | 107 | 101 | | string.Compare(e.Fault.Code.SubCode.Name, FaultCodeConstants.Codes.InternalServiceFault, StringCompa |
| | | 102 | | { |
| | 0 | 103 | | faultInfo.IsConsideredUnhandled = true; |
| | | 104 | | } |
| | | 105 | | } |
| | 107 | 106 | | } |
| | | 107 | | |
| | | 108 | | private void ProvideMessageFaultCoreCoda(MessageRpc rpc) |
| | | 109 | | { |
| | 163 | 110 | | if (rpc.FaultInfo.Fault.Headers.Action == null) |
| | | 111 | | { |
| | 32 | 112 | | rpc.FaultInfo.Fault.Headers.Action = rpc.RequestVersion.Addressing.DefaultFaultAction; |
| | | 113 | | } |
| | | 114 | | |
| | 163 | 115 | | rpc.Reply = rpc.FaultInfo.Fault; |
| | 163 | 116 | | } |
| | | 117 | | |
| | | 118 | | internal void ProvideOnlyFaultOfLastResort(ref MessageRpc rpc) |
| | | 119 | | { |
| | 0 | 120 | | ProvideFaultOfLastResort(rpc.Error, ref rpc.FaultInfo); |
| | 0 | 121 | | ProvideMessageFaultCoreCoda(rpc); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | internal void ProvideFault(Exception e, FaultConverter faultConverter, ref ErrorHandlerFaultInfo faultInfo) |
| | | 125 | | { |
| | 182 | 126 | | ProvideWellKnownFault(e, faultConverter, ref faultInfo); |
| | 372 | 127 | | for (int i = 0; i < Handlers.Length; i++) |
| | | 128 | | { |
| | 4 | 129 | | Message m = faultInfo.Fault; |
| | 4 | 130 | | Handlers[i].ProvideFault(e, _messageVersion, ref m); |
| | 4 | 131 | | faultInfo.Fault = m; |
| | | 132 | | //if (TD.FaultProviderInvokedIsEnabled()) |
| | | 133 | | //{ |
| | | 134 | | // TD.FaultProviderInvoked(handlers[i].GetType().FullName, e.Message); |
| | | 135 | | //} |
| | | 136 | | } |
| | 182 | 137 | | ProvideFaultOfLastResort(e, ref faultInfo); |
| | 182 | 138 | | } |
| | | 139 | | |
| | | 140 | | private void ProvideWellKnownFault(Exception e, FaultConverter faultConverter, ref ErrorHandlerFaultInfo faultIn |
| | | 141 | | { |
| | 182 | 142 | | if (faultConverter != null && faultConverter.TryCreateFaultMessage(e, out Message faultMessage)) |
| | | 143 | | { |
| | 0 | 144 | | faultInfo.Fault = faultMessage; |
| | 0 | 145 | | return; |
| | | 146 | | } |
| | 182 | 147 | | else if (e is NetDispatcherFaultException) |
| | | 148 | | { |
| | 0 | 149 | | NetDispatcherFaultException ndfe = e as NetDispatcherFaultException; |
| | 0 | 150 | | if (_debug) |
| | | 151 | | { |
| | 0 | 152 | | ExceptionDetail detail = new ExceptionDetail(ndfe); |
| | 0 | 153 | | faultInfo.Fault = Message.CreateMessage(_messageVersion, MessageFault.CreateFault(ndfe.Code, ndfe.Re |
| | | 154 | | } |
| | | 155 | | else |
| | | 156 | | { |
| | 0 | 157 | | faultInfo.Fault = Message.CreateMessage(_messageVersion, ndfe.CreateMessageFault(), ndfe.Action); |
| | | 158 | | } |
| | | 159 | | } |
| | 182 | 160 | | } |
| | | 161 | | |
| | | 162 | | internal void HandleError(MessageRpc rpc) |
| | | 163 | | { |
| | 2538 | 164 | | if (rpc.Error != null) |
| | | 165 | | { |
| | 163 | 166 | | HandleErrorCore(rpc); |
| | | 167 | | } |
| | 2538 | 168 | | } |
| | | 169 | | |
| | | 170 | | private void HandleErrorCore(MessageRpc rpc) |
| | | 171 | | { |
| | 163 | 172 | | bool handled = HandleErrorCommon(rpc.Error, ref rpc.FaultInfo); |
| | 163 | 173 | | if (handled) |
| | | 174 | | { |
| | 105 | 175 | | rpc.Error = null; |
| | | 176 | | } |
| | 163 | 177 | | } |
| | | 178 | | |
| | | 179 | | private bool HandleErrorCommon(Exception error, ref ErrorHandlerFaultInfo faultInfo) |
| | | 180 | | { |
| | | 181 | | bool handled; |
| | 167 | 182 | | if (faultInfo.Fault != null // there is a message |
| | 167 | 183 | | && !faultInfo.IsConsideredUnhandled) // and it's not the internal-server-error one |
| | | 184 | | { |
| | 107 | 185 | | handled = true; |
| | | 186 | | } |
| | | 187 | | else |
| | | 188 | | { |
| | 60 | 189 | | handled = false; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | try |
| | | 193 | | { |
| | | 194 | | //if (TD.ServiceExceptionIsEnabled()) |
| | | 195 | | //{ |
| | | 196 | | // TD.ServiceException(null, error.ToString(), error.GetType().FullName); |
| | | 197 | | //} |
| | 342 | 198 | | for (int i = 0; i < Handlers.Length; i++) |
| | | 199 | | { |
| | 4 | 200 | | bool handledByThis = Handlers[i].HandleError(error); |
| | 4 | 201 | | handled = handledByThis || handled; |
| | | 202 | | //if (TD.ErrorHandlerInvokedIsEnabled()) |
| | | 203 | | //{ |
| | | 204 | | // TD.ErrorHandlerInvoked(handlers[i].GetType().FullName, handledByThis, error.GetType().FullName |
| | | 205 | | //} |
| | | 206 | | } |
| | 167 | 207 | | } |
| | 0 | 208 | | catch (Exception e) |
| | | 209 | | { |
| | 0 | 210 | | if (Fx.IsFatal(e)) |
| | | 211 | | { |
| | 0 | 212 | | throw; |
| | | 213 | | } |
| | 0 | 214 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 215 | | } |
| | 167 | 216 | | return handled; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | internal bool HandleError(Exception error) |
| | | 220 | | { |
| | 0 | 221 | | ErrorHandlerFaultInfo faultInfo = new ErrorHandlerFaultInfo(_messageVersion.Addressing.DefaultFaultAction); |
| | 0 | 222 | | return HandleError(error, ref faultInfo); |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | internal bool HandleError(Exception error, ref ErrorHandlerFaultInfo faultInfo) |
| | | 226 | | { |
| | 4 | 227 | | return HandleErrorCommon(error, ref faultInfo); |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | internal static bool ShouldRethrowExceptionAsIs(Exception e) |
| | | 231 | | { |
| | 1 | 232 | | return true; |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | internal static bool ShouldRethrowClientSideExceptionAsIs(Exception e) |
| | | 236 | | { |
| | 0 | 237 | | return true; |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | // This ensures that people debugging first-chance Exceptions see this Exception, |
| | | 241 | | // and that the Exception shows up in the trace logs. |
| | | 242 | | internal static void ThrowAndCatch(Exception e, Message message) |
| | | 243 | | { |
| | | 244 | | try |
| | | 245 | | { |
| | 2 | 246 | | if (System.Diagnostics.Debugger.IsAttached) |
| | | 247 | | { |
| | 0 | 248 | | if (message == null) |
| | | 249 | | { |
| | 0 | 250 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e); |
| | | 251 | | } |
| | | 252 | | else |
| | | 253 | | { |
| | 0 | 254 | | throw TraceUtility.ThrowHelperError(e, message); |
| | | 255 | | } |
| | | 256 | | } |
| | | 257 | | else |
| | | 258 | | { |
| | 2 | 259 | | if (message == null) |
| | | 260 | | { |
| | 2 | 261 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e); |
| | | 262 | | } |
| | | 263 | | else |
| | | 264 | | { |
| | 0 | 265 | | TraceUtility.ThrowHelperError(e, message); |
| | | 266 | | } |
| | | 267 | | } |
| | 0 | 268 | | } |
| | 2 | 269 | | catch (Exception e2) |
| | | 270 | | { |
| | 2 | 271 | | if (!ReferenceEquals(e, e2)) |
| | | 272 | | { |
| | 0 | 273 | | throw; |
| | | 274 | | } |
| | 2 | 275 | | } |
| | 2 | 276 | | } |
| | | 277 | | |
| | | 278 | | internal static void ThrowAndCatch(Exception e) |
| | | 279 | | { |
| | 2 | 280 | | ThrowAndCatch(e, null); |
| | 2 | 281 | | } |
| | | 282 | | } |
| | | 283 | | } |