Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for handling a fault in the request handler using Handl…

…eFault()
  • Loading branch information...
commit 36ecf2c16477db62b379a047e0dc2985d8aa5c3b 1 parent 0647b32
@phatboyg phatboyg authored
View
32 src/MassTransit.Tests/PublishRequest_Specs.cs
@@ -250,6 +250,38 @@ public void Should_throw_a_handler_exception_on_the_calling_thread()
pongReceived.IsAvailable(timeout).ShouldBeTrue("The pong was not received");
}
+ [Test]
+ public void Should_throw_an_exception_if_a_fault_was_published()
+ {
+ var pongReceived = new FutureMessage<PongMessage>();
+ var faultReceived = new FutureMessage<Fault<PingMessage>>();
+ var pingReceived = new FutureMessage<PingMessage>();
+
+ RemoteBus.SubscribeContextHandler<PingMessage>(x =>
+ {
+ pingReceived.Set(x.Message);
+
+ throw new InvalidOperationException("This should carry over to the calling request");
+ });
+ LocalBus.ShouldHaveSubscriptionFor<PingMessage>();
+
+ var ping = new PingMessage();
+
+ TimeSpan timeout = 24.Seconds();
+
+ LocalBus.PublishRequest(ping, x =>
+ {
+ x.Handle<PongMessage>(pongReceived.Set);
+ x.HandleFault(faultReceived.Set);
+
+ x.SetTimeout(timeout);
+ });
+
+ pingReceived.IsAvailable(timeout).ShouldBeTrue("The ping was not received");
+ faultReceived.IsAvailable(timeout).ShouldBeTrue("The fault was not received");
+ pongReceived.IsAvailable(1.Seconds()).ShouldBeFalse("The pong was not received");
+ }
+
[Test, Category("NotOnTeamCity")]
public void Should_throw_a_handler_exception_on_the_calling_thread_using_async()
{
View
90 src/MassTransit/Exceptions/RequestException.cs
@@ -1,54 +1,60 @@
-// Copyright 2007-2011 Chris Patterson, Dru Sellers, Travis Smith, et. al.
+// Copyright 2007-2012 Chris Patterson, Dru Sellers, Travis Smith, et. al.
//
-// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
-// Unless required by applicable law or agreed to in writing, software distributed
+// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
namespace MassTransit.Exceptions
{
- using System;
- using System.Runtime.Serialization;
-
- [Serializable]
- public class RequestException :
- MassTransitException
- {
- readonly object _response;
-
- public RequestException(string message, Exception innerException, object response)
- : base(message, innerException)
- {
- _response = response;
- }
-
- public RequestException()
- {
- }
-
- public RequestException(string message)
- : base(message)
- {
- }
-
- public RequestException(string message, Exception innerException)
- : base(message, innerException)
- {
- }
-
- protected RequestException(SerializationInfo info, StreamingContext context)
- : base(info, context)
- {
- }
-
- public object Response
- {
- get { return _response; }
- }
- }
+ using System;
+ using System.Runtime.Serialization;
+
+ [Serializable]
+ public class RequestException :
+ MassTransitException
+ {
+ readonly object _response;
+
+ public RequestException(string message, Exception innerException, object response)
+ : base(message, innerException)
+ {
+ _response = response;
+ }
+
+ public RequestException()
+ {
+ }
+
+ public RequestException(string message)
+ : base(message)
+ {
+ }
+
+ public RequestException(string message, Exception innerException)
+ : base(message, innerException)
+ {
+ }
+
+ protected RequestException(SerializationInfo info, StreamingContext context)
+ : base(info, context)
+ {
+ }
+
+ protected RequestException(string message, object response)
+ : base(message)
+ {
+ _response = response;
+ }
+
+ public object Response
+ {
+ get { return _response; }
+ }
+ }
}
View
46 src/MassTransit/Exceptions/RequestFaultException.cs
@@ -0,0 +1,46 @@
+// Copyright 2007-2012 Chris Patterson, Dru Sellers, Travis Smith, et. al.
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// this file except in compliance with the License. You may obtain a copy of the
+// License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+namespace MassTransit.Exceptions
+{
+ using System;
+ using System.Runtime.Serialization;
+
+ [Serializable]
+ public class RequestFaultException :
+ RequestException
+ {
+ public RequestFaultException(string message, Exception innerException, object fault)
+ : base(message, innerException, fault)
+ {
+ }
+
+ public RequestFaultException()
+ {
+ }
+
+ public RequestFaultException(string message, object fault)
+ : base(message, fault)
+ {
+ }
+
+ public RequestFaultException(string message, Exception innerException)
+ : base(message, innerException)
+ {
+ }
+
+ protected RequestFaultException(SerializationInfo info, StreamingContext context)
+ : base(info, context)
+ {
+ }
+ }
+}
View
2  src/MassTransit/MassTransit.csproj
@@ -359,6 +359,7 @@
<Compile Include="Exceptions\PublishException.cs" />
<Compile Include="Exceptions\RequestCancelledException.cs" />
<Compile Include="Exceptions\RequestException.cs" />
+ <Compile Include="Exceptions\RequestFaultException.cs" />
<Compile Include="Exceptions\RequestTimeoutException.cs" />
<Compile Include="Exceptions\SendException.cs" />
<Compile Include="IOutboundMessageInterceptor.cs" />
@@ -396,6 +397,7 @@
<Compile Include="RequestResponse\Configurators\InlineRequestConfiguratorImpl.cs" />
<Compile Include="RequestResponse\Configurators\TaskRequestConfigurator.cs" />
<Compile Include="RequestResponse\Configurators\TaskRequestConfiguratorImpl.cs" />
+ <Compile Include="RequestResponse\FaultHandler.cs" />
<Compile Include="RequestResponse\IAsyncRequest.cs" />
<Compile Include="RequestResponse\IRequest.cs" />
<Compile Include="RequestResponse\IRequestComplete.cs" />
View
12 src/MassTransit/RequestResponse/Configurators/InlineRequestConfigurator.cs
@@ -50,5 +50,17 @@ void Handle<TResponse>(Action<TResponse> handler)
/// <param name="handler">The handler to call with the response message</param>
void Handle<TResponse>(Action<IConsumeContext<TResponse>, TResponse> handler)
where TResponse : class;
+
+ /// <summary>
+ /// Specifies a handler for a fault published by the request handler
+ /// </summary>
+ /// <param name="faultCallback"></param>
+ void HandleFault(Action<Fault<TRequest>> faultCallback);
+
+ /// <summary>
+ /// Specifies a handler for a fault published by the request handler
+ /// </summary>
+ /// <param name="faultCallback"></param>
+ void HandleFault(Action<IConsumeContext<Fault<TRequest>>, Fault<TRequest>> faultCallback);
}
}
View
11 src/MassTransit/RequestResponse/Configurators/InlineRequestConfiguratorImpl.cs
@@ -54,6 +54,17 @@ public void Watch<T>(Action<IConsumeContext<T>, T> watcher)
AddHandler(typeof(T), () => new WatchResponseHandler<T>(RequestId, RequestSynchronizationContext, watcher));
}
+ public void HandleFault(Action<Fault<TRequest>> faultCallback)
+ {
+ AddHandler(typeof(Fault<TRequest>), () => new CompleteResponseHandler<Fault<TRequest>>(RequestId,
+ _request, RequestSynchronizationContext, faultCallback));
+ }
+
+ public void HandleFault(Action<IConsumeContext<Fault<TRequest>>, Fault<TRequest>> faultCallback)
+ {
+ AddHandler(typeof(Fault<TRequest>), () => new CompleteResponseHandler<Fault<TRequest>>(RequestId,
+ _request, RequestSynchronizationContext, faultCallback));
+ }
public IAsyncRequest<TRequest> Build(IServiceBus bus)
{
View
12 src/MassTransit/RequestResponse/Configurators/TaskRequestConfigurator.cs
@@ -43,6 +43,18 @@ Task<T> Handle<T>(Action<T> handler)
/// <param name="handler">The handler to call with the response message</param>
Task<T> Handle<T>(Action<IConsumeContext<T>, T> handler)
where T : class;
+
+ /// <summary>
+ /// Specifies a handler for a fault published by the request handler
+ /// </summary>
+ /// <param name="faultCallback"></param>
+ Task<Fault<TRequest>> HandleFault(Action<Fault<TRequest>> faultCallback);
+
+ /// <summary>
+ /// Specifies a handler for a fault published by the request handler
+ /// </summary>
+ /// <param name="faultCallback"></param>
+ Task<Fault<TRequest>> HandleFault(Action<IConsumeContext<Fault<TRequest>>, Fault<TRequest>> faultCallback);
}
#endif
}
View
16 src/MassTransit/RequestResponse/Configurators/TaskRequestConfiguratorImpl.cs
@@ -45,6 +45,22 @@ public Task<T> Handle<T>(Action<IConsumeContext<T>, T> handler)
return responseHandler.Task;
}
+ public Task<Fault<TRequest>> HandleFault(Action<Fault<TRequest>> handler)
+ {
+ TaskResponseHandler<Fault<TRequest>> responseHandler = AddHandler(typeof(Fault<TRequest>),
+ () => new CompleteTaskResponseHandler<Fault<TRequest>>(RequestId, handler));
+
+ return responseHandler.Task;
+ }
+
+ public Task<Fault<TRequest>> HandleFault(Action<IConsumeContext<Fault<TRequest>>, Fault<TRequest>> handler)
+ {
+ TaskResponseHandler<Fault<TRequest>> responseHandler = AddHandler(typeof(Fault<TRequest>),
+ () => new CompleteTaskResponseHandler<Fault<TRequest>>(RequestId, handler));
+
+ return responseHandler.Task;
+ }
+
public void Watch<T>(Action<T> watcher)
where T : class
{
View
43 src/MassTransit/RequestResponse/FaultHandler.cs
@@ -0,0 +1,43 @@
+// Copyright 2007-2012 Chris Patterson, Dru Sellers, Travis Smith, et. al.
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// this file except in compliance with the License. You may obtain a copy of the
+// License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+namespace MassTransit.RequestResponse
+{
+ using System;
+ using System.Threading;
+
+ public class FaultHandler<TRequest>
+ where TRequest : class
+ {
+ readonly Action<TRequest, Fault<TRequest>> _faultCallback;
+ readonly SynchronizationContext _synchronizationContext;
+
+ public FaultHandler(SynchronizationContext synchronizationSynchronizationContext,
+ Action<TRequest, Fault<TRequest>> faultCallback)
+ {
+ _faultCallback = faultCallback;
+ _synchronizationContext = synchronizationSynchronizationContext;
+ }
+
+ public void HandleFault(TRequest request, Fault<TRequest> fault)
+ {
+ if (_synchronizationContext != null)
+ {
+ _synchronizationContext.Post(state => _faultCallback(request, fault), state: null);
+ }
+ else
+ {
+ _faultCallback(request, fault);
+ }
+ }
+ }
+}
View
2  src/SolutionVersion.cs
@@ -8,7 +8,7 @@
[assembly: AssemblyVersion("2.6.3")]
[assembly: AssemblyFileVersion("2.6.3")]
-[assembly: AssemblyInformationalVersion("2.6.3.6b123d")]
+[assembly: AssemblyInformationalVersion("2.6.3.0647b3")]
[assembly: ComVisibleAttribute(false)]
[assembly: CLSCompliantAttribute(false)]
Please sign in to comment.
Something went wrong with that request. Please try again.