From ffed3eb088bc00f90a5e4b7367d4598fda007401 Mon Sep 17 00:00:00 2001 From: rdavisau Date: Tue, 1 Sep 2015 01:07:15 +1000 Subject: [PATCH] Throw `TypeLoadException` when an actor type or dependency cannot be found in a remote actor deploy scenario Actively handle cases where a null actor type is passed to a Props constructor --- .../DaemonMsgCreateSerializer.cs | 18 ++++++++++++++++- src/core/Akka.Tests/Actor/PropsSpec.cs | 20 +++++++++++++++++++ src/core/Akka/Actor/Props.cs | 17 ++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/core/Akka.Remote/Serialization/DaemonMsgCreateSerializer.cs b/src/core/Akka.Remote/Serialization/DaemonMsgCreateSerializer.cs index 1d4f2d91252..569df12034c 100644 --- a/src/core/Akka.Remote/Serialization/DaemonMsgCreateSerializer.cs +++ b/src/core/Akka.Remote/Serialization/DaemonMsgCreateSerializer.cs @@ -113,7 +113,23 @@ private DeployData GetDeployData(Deploy deploy) public override object FromBinary(byte[] bytes, Type type) { var proto = DaemonMsgCreateData.ParseFrom(bytes); - var clazz = Type.GetType(proto.Props.Clazz); + Type clazz; + + try + { + clazz = Type.GetType(proto.Props.Clazz, true); + } + catch (TypeLoadException ex) + { + var msg = string.Format( + "Could not find type '{0}' on the remote system. " + + "Ensure that the remote system has an assembly that contains the type {0} in its assembly search path", + proto.Props.Clazz); + + + throw new TypeLoadException(msg, ex); + } + var args = GetArgs(proto); var props = new Props(GetDeploy(proto.Props.Deploy), clazz, args); return new DaemonMsgCreate( diff --git a/src/core/Akka.Tests/Actor/PropsSpec.cs b/src/core/Akka.Tests/Actor/PropsSpec.cs index fc40ea51b31..e2df96090dc 100644 --- a/src/core/Akka.Tests/Actor/PropsSpec.cs +++ b/src/core/Akka.Tests/Actor/PropsSpec.cs @@ -8,6 +8,7 @@ using Akka.Actor; using Akka.TestKit; using System; +using System.Linq; using Xunit; namespace Akka.Tests.Actor @@ -56,6 +57,25 @@ public void Props_created_with_strategy_must_have_it_set() Assert.Equal(strategy, props.SupervisorStrategy); } + [Fact] + public void Props_created_with_null_type_must_throw() + { + Type missingType = null; + object[] args = new object[0]; + var argsEnumerable = Enumerable.Empty(); + var defaultStrategy = SupervisorStrategy.DefaultStrategy; + var defaultDeploy = Deploy.Local; + + Props p = null; + + Assert.Throws("type", () => p = new Props(missingType, args)); + Assert.Throws("type", () => p = new Props(missingType)); + Assert.Throws("type", () => p = new Props(missingType, defaultStrategy, argsEnumerable)); + Assert.Throws("type", () => p = new Props(missingType, defaultStrategy, args)); + Assert.Throws("type", () => p = new Props(defaultDeploy, missingType, argsEnumerable)); + Assert.Throws("type", () => p = Props.Create(missingType, args)); + } + private class TestProducer : IIndirectActorProducer { TestLatch latchActor; diff --git a/src/core/Akka/Actor/Props.cs b/src/core/Akka/Actor/Props.cs index 1e33b9d27c3..10b2269e5a4 100644 --- a/src/core/Akka/Actor/Props.cs +++ b/src/core/Akka/Actor/Props.cs @@ -32,6 +32,8 @@ namespace Akka.Actor /// public class Props : IEquatable , ISurrogated { + private const string NullActorTypeExceptionText = "Props must be instantiated with an actor type."; + public class PropsSurrogate : ISurrogate { public Type Type { get; set; } @@ -183,6 +185,8 @@ protected Props(Props copy) public Props(Type type, object[] args) : this(defaultDeploy, type, args) { + if (type == null) + throw new ArgumentNullException("type", NullActorTypeExceptionText); } /// @@ -192,6 +196,8 @@ public Props(Type type, object[] args) public Props(Type type) : this(defaultDeploy, type, noArgs) { + if (type == null) + throw new ArgumentNullException("type", NullActorTypeExceptionText); } /// @@ -203,6 +209,9 @@ public Props(Type type) public Props(Type type, SupervisorStrategy supervisorStrategy, IEnumerable args) : this(defaultDeploy, type, args.ToArray()) { + if (type == null) + throw new ArgumentNullException("type", NullActorTypeExceptionText); + SupervisorStrategy = supervisorStrategy; } @@ -215,6 +224,9 @@ public Props(Type type, SupervisorStrategy supervisorStrategy, IEnumerable args) : this(deploy, type, args.ToArray()) { + if (type == null) + throw new ArgumentNullException("type", NullActorTypeExceptionText); } /// @@ -394,6 +408,9 @@ public static Props CreateBy(params object[] args) where TProducer : /// Props. public static Props Create(Type type, params object[] args) { + if (type == null) + throw new ArgumentNullException("type", NullActorTypeExceptionText); + return new Props(type, args); }