public void OnError(ServerCallInterceptorContext context, Exception error) { if (context.ServerCallContext.UserState.TryGetValue(VisitMarker, out _)) { return; } context.ServerCallContext.UserState.Add(VisitMarker, string.Empty); var faultOrIgnore = _errorHandler.ProvideFaultOrIgnore(context, error); if (!faultOrIgnore.HasValue) { return; } var fault = faultOrIgnore.Value; var status = new Status(fault.StatusCode ?? StatusCode.Internal, fault.Message ?? error.Message); var metadata = fault.Trailers; if (fault.Detail != null) { if (metadata == null) { metadata = new Metadata(); } metadata.Add(CallContext.HeaderNameErrorDetail, _marshallerFactory.SerializeHeader(fault.Detail)); metadata.Add(CallContext.HeaderNameErrorDetailType, fault.Detail.GetType().GetShortAssemblyQualifiedName()); } throw new RpcException(status, metadata ?? Metadata.Empty, status.Detail); }
protected override ServerFaultDetail?ProvideFaultOrIgnoreCore(ServerCallInterceptorContext context, Exception error) { // handle FaultException<ApplicationExceptionFaultDetail> if (error is FaultException <ApplicationExceptionFaultDetail> appFault) { // pass detail to a client call return(new ServerFaultDetail { Detail = appFault.Detail }); } // handle FaultException<InvalidOperationExceptionFaultDetail> if (error is FaultException <InvalidOperationExceptionFaultDetail> opFault) { // pass detail to a client call return(new ServerFaultDetail { Detail = opFault.Detail }); } // ignore other error return(null); }
protected override ServerFaultDetail?ProvideFaultOrIgnoreCore(ServerCallInterceptorContext context, Exception error) { return(new ServerFaultDetail { Message = error.Message, Detail = new ExceptionDetail(error) }); }
public ServerFaultDetail? ProvideFaultOrIgnore(ServerCallInterceptorContext context, Exception error) { if (error is ApplicationException) { // provide a marker for the client exception handler return new ServerFaultDetail { Detail = "ApplicationException" }; } // ignore other exceptions return null; }
public void BeforeEachTest() { var serverContext = new Mock <ServerCallContext> { CallBase = true }; _context = new ServerCallInterceptorContext(serverContext.Object); _errorHandler = new Mock <IServerErrorHandler>(MockBehavior.Strict); _sut = new ServerCallErrorInterceptor(_errorHandler.Object, DataContractMarshallerFactory.Default); }
public ServerFaultDetail?ProvideFaultOrIgnore(ServerCallInterceptorContext context, Exception error) { if (error is NotSupportedException || error is InvalidOperationException) { // provide detailed information for the client error handler var detail = new UnexpectedErrorDetail { Message = error.Message, ExceptionType = error.GetType().FullName, FullException = error.ToString(), MethodName = Process.GetCurrentProcess().ProcessName }; return(new ServerFaultDetail { Detail = detail }); } // ignore other exceptions return(null); }