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 f1a36ba76b Add StatusReply.fromTry and fromTryKeepException factory 
methods (#2796)
f1a36ba76b is described below

commit f1a36ba76bd670bfdfceb5eb571a0a0fb157349a
Author: He-Pin(kerr) <[email protected]>
AuthorDate: Sat Mar 28 15:41:17 2026 +0800

    Add StatusReply.fromTry and fromTryKeepException factory methods (#2796)
    
    Add convenience factories to convert scala.util.Try into StatusReply:
    - fromTry: converts Try to StatusReply, wrapping failure message as
      ErrorMessage string. Improved over upstream with null-safe getMessage
      handling (falls back to exception class name).
    - fromTryKeepException: converts Try to StatusReply, preserving the
      original exception for typed error handling.
    
    Tests cover success, failure-with-message, failure-keeping-exception,
    fromTryKeepException success path, and null-message edge case (5 tests).
    
    Upstream: akka/akka-core@38f3fd8472
    Cherry-picked from akka/akka-core v2.8.0, which is now Apache licensed.
    
    Co-authored-by: Copilot <[email protected]>
---
 .../org/apache/pekko/pattern/StatusReplySpec.scala | 36 ++++++++++++++++++++++
 .../org/apache/pekko/pattern/StatusReply.scala     | 26 ++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git 
a/actor-tests/src/test/scala/org/apache/pekko/pattern/StatusReplySpec.scala 
b/actor-tests/src/test/scala/org/apache/pekko/pattern/StatusReplySpec.scala
index 364dc8529d..3356800eae 100644
--- a/actor-tests/src/test/scala/org/apache/pekko/pattern/StatusReplySpec.scala
+++ b/actor-tests/src/test/scala/org/apache/pekko/pattern/StatusReplySpec.scala
@@ -74,6 +74,42 @@ class StatusReplySpec extends PekkoSpec with ScalaFutures {
         TestException("boo"))
 
     }
+
+    "create from Try success" in {
+      import scala.util.Success
+      val reply = StatusReply.fromTry(Success("woho"))
+      reply.isSuccess should ===(true)
+      reply.getValue should ===("woho")
+    }
+
+    "create from Try failure with message" in {
+      import scala.util.Failure
+      val reply = StatusReply.fromTry(Failure(TestException("boo")))
+      reply.isError should ===(true)
+      reply.getError.getMessage should ===("boo")
+      reply.getError shouldBe a[StatusReply.ErrorMessage]
+    }
+
+    "create from Try failure keeping exception" in {
+      import scala.util.Failure
+      val reply = 
StatusReply.fromTryKeepException(Failure(TestException("boo")))
+      reply.isError should ===(true)
+      reply.getError should ===(TestException("boo"))
+    }
+
+    "create from Try success via fromTryKeepException" in {
+      import scala.util.Success
+      val reply = StatusReply.fromTryKeepException(Success("woho"))
+      reply.isSuccess should ===(true)
+      reply.getValue should ===("woho")
+    }
+
+    "create from Try failure with null message" in {
+      import scala.util.Failure
+      val reply = StatusReply.fromTry(Failure(new RuntimeException()))
+      reply.isError should ===(true)
+      reply.getError.getMessage should ===("java.lang.RuntimeException")
+    }
   }
 
   "askWithStatus" should {
diff --git a/actor/src/main/scala/org/apache/pekko/pattern/StatusReply.scala 
b/actor/src/main/scala/org/apache/pekko/pattern/StatusReply.scala
index 636313b971..4825f4caa0 100644
--- a/actor/src/main/scala/org/apache/pekko/pattern/StatusReply.scala
+++ b/actor/src/main/scala/org/apache/pekko/pattern/StatusReply.scala
@@ -105,6 +105,32 @@ object StatusReply {
    */
   def error[T](exception: Throwable): StatusReply[T] = Error(exception)
 
+  /**
+   * Scala API: Turn a [[scala.util.Try]] into a status reply.
+   *
+   * Transforms exceptions into status reply errors containing just the 
exception message string.
+   *
+   * See [[#fromTryKeepException]] for passing the exception along as is.
+   */
+  def fromTry[T](status: Try[T]): StatusReply[T] = status match {
+    case ScalaSuccess(value) => success(value)
+    case ScalaFailure(t)     => 
error[T](Option(t.getMessage).getOrElse(t.getClass.getName))
+  }
+
+  /**
+   * Scala API: Turn a [[scala.util.Try]] into a status reply.
+   *
+   * Prefer the string based error response over this one when possible to 
avoid tightly coupled logic across
+   * actors and passing internal failure details on to callers that can not do 
much to handle them. [[#fromTry]]
+   * provides a convenience factory doing that for [[scala.util.Try]].
+   *
+   * For cases where types are needed to identify errors and behave 
differently enumerating them with a specific
+   * set of response messages may be a better alternative to encoding them as 
generic exceptions.
+   *
+   * Also note that Pekko does not contain pre-built serializers for arbitrary 
exceptions.
+   */
+  def fromTryKeepException[T](status: Try[T]): StatusReply[T] = new 
StatusReply(status)
+
   /**
    * Carrier exception used for textual error descriptions.
    *


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to