Skip to content

Document that transform and transformAsync propagate exceptions thrown by the function - #8696

Closed
cindykrafft wants to merge 1 commit into
google:masterfrom
cindykrafft:docs-futures-transform-exceptions
Closed

cindykrafft wants to merge 1 commit into
google:masterfrom
cindykrafft:docs-futures-transform-exceptions

Conversation

@cindykrafft

Copy link
Copy Markdown

The Javadoc for Futures.transform, Futures.transformAsync, FluentFuture.transform and FluentFuture.transformAsync says that if the input fails, "the returned {@code Future} fails with the same exception (and the function is not invoked)" (e.g. Futures.java:465-466). As far as I can see, it does not say what happens when the function itself throws. This adds the following sentence right after the existing one in all four methods:

If the function throws an exception, the returned {@code Future} fails with that exception.

This is a Javadoc-only change. catching and catchingAsync (in both Futures and FluentFuture) already say "If, during the invocation of {@code fallback}, an exception is thrown, this exception is used as the result of the output {@code Future}", so they are not changed.

Fixes #2690

Verification:

  • In AbstractTransformFuture.run(), a Throwable from doTransform is caught with catch (Throwable t) and passed to setException(t) (AbstractTransformFuture.java:125-130).
  • I also ran a small program against the built jar, once with directExecutor() and once with a thread pool. In every case the returned future failed, and ExecutionException.getCause() was the same instance the function threw:
    • transform with a RuntimeException or an Error, whether the input was already done or completed later
    • transformAsync with a RuntimeException, a checked exception or an Error
    • the same checks through FluentFuture
  • If an AsyncFunction returns null, the returned future fails with a NullPointerException ("AsyncFunction.apply returned null instead of a Future..."). This PR does not document that case.

Testing:

  • ./mvnw -pl guava,guava-testlib,guava-tests test -Dtest.include="**/FuturesTest.java,**/FluentFutureTest.java": 169 tests (160 in FuturesTest, 9 in FluentFutureTest), 0 failures, 0 errors.
  • ./mvnw -pl guava javadoc:javadoc: BUILD SUCCESS, and the new sentence appears in the generated Futures.html and FluentFuture.html.

…hrown by the function.

`Futures.transform`, `Futures.transformAsync`, `FluentFuture.transform`,
and `FluentFuture.transformAsync` already document that a failure of the
input is propagated to the returned `Future`, but not what happens when
the function itself throws. In that case, the returned `Future` fails
with the thrown exception. (`catching` and `catchingAsync` already
document the equivalent behavior for their fallback.)

Fixes google#2690

Claude-Session: https://claude.ai/code/session_01X59fWmqnA4Nmb9rggkTP7n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 package=concurrent type=api-docs Change/add API documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document that exceptions thrown by the Function passed to Futures.transform, etc. are propagated to the output Future

2 participants