This is an automated email from the ASF dual-hosted git repository.
hepin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko.git
The following commit(s) were added to refs/heads/main by this push:
new c9713f0899 Improve error message for actorOf on typed ActorSystem
(#2798)
c9713f0899 is described below
commit c9713f0899303f10eebd47ce8b152a248ac18a6f
Author: He-Pin(kerr) <[email protected]>
AuthorDate: Sat Mar 28 17:00:06 2026 +0800
Improve error message for actorOf on typed ActorSystem (#2798)
When calling system.actorOf() on a typed ActorSystem, provide a more
helpful error message explaining that top-level actors should be spawned
as children of the guardian behavior, or a classic ActorSystem should be
used instead. The generic 'custom user guardian' message is preserved
for non-typed custom guardians.
Fix upstream bug: the isTypedGuardian check used startsWith with a
package prefix that never matched due to case mismatch (T vs t). Use
exact class name match instead.
Upstream: akka/akka-core@f73fae3783
Cherry-picked from akka/akka-core v2.8.0, which is now Apache licensed.
Co-authored-by: Copilot <[email protected]>
---
.../scala/org/apache/pekko/actor/ActorSystem.scala | 29 +++++++++++++++++-----
1 file changed, 23 insertions(+), 6 deletions(-)
diff --git a/actor/src/main/scala/org/apache/pekko/actor/ActorSystem.scala
b/actor/src/main/scala/org/apache/pekko/actor/ActorSystem.scala
index 6e7df09fda..ba3ace091b 100644
--- a/actor/src/main/scala/org/apache/pekko/actor/ActorSystem.scala
+++ b/actor/src/main/scala/org/apache/pekko/actor/ActorSystem.scala
@@ -933,15 +933,29 @@ private[pekko] class ActorSystemImpl(
def actorOf(props: Props, name: String): ActorRef =
if (guardianProps.isEmpty) guardian.underlying.attachChild(props, name,
systemService = false)
- else
- throw new UnsupportedOperationException(
- s"cannot create top-level actor [$name] from the outside on
ActorSystem with custom user guardian")
+ else {
+ val message =
+ if (isTypedGuardian)
+ s"cannot create top-level actor [$name] from the outside on a typed
ActorSystem. In a typed ActorSystem, " +
+ "top-level actors should be spawned as children of the guardian
behavior; if this is not possible (e.g. this " +
+ "actorOf call is in a library), a classic ActorSystem should be
created and used to spawn top-level actors."
+ else s"cannot create top-level actor [$name] from the outside on
ActorSystem with custom user guardian"
+
+ throw new UnsupportedOperationException(message)
+ }
def actorOf(props: Props): ActorRef =
if (guardianProps.isEmpty) guardian.underlying.attachChild(props,
systemService = false)
- else
- throw new UnsupportedOperationException(
- "cannot create top-level actor from the outside on ActorSystem with
custom user guardian")
+ else {
+ val message =
+ if (isTypedGuardian)
+ "cannot create top-level actor from the outside on a typed
ActorSystem. In a typed ActorSystem, " +
+ "top-level actors should be spawned as children of the guardian
behavior; if this is not possible (e.g. this " +
+ "actorOf call is in a library), a classic ActorSystem should be
created and used to spawn top-level actors."
+ else "cannot create top-level actor from the outside on ActorSystem
with custom user guardian"
+
+ throw new UnsupportedOperationException(message)
+ }
def stop(actor: ActorRef): Unit = {
val path = actor.path
@@ -1338,4 +1352,7 @@ private[pekko] class ActorSystemImpl(
*/
def terminationFuture: Future[T] = done.future
}
+
+ private def isTypedGuardian: Boolean =
+ guardianProps.exists(_.clazz.getName ==
"org.apache.pekko.actor.TypedCreatorFunctionConsumer")
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]