-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:Add onErrorComplete stream operator.
- Loading branch information
Showing
10 changed files
with
463 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
docs/src/main/paradox/stream/operators/Source-or-Flow/onErrorComplete.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# onErrorComplete | ||
|
||
Allows completing the stream when an upstream error occur. | ||
|
||
@ref[Error handling](../index.md#error-handling) | ||
|
||
## Signature | ||
|
||
@apidoc[Source.onErrorComplete](Source) { scala="#onErrorComplete(pf%3A%20PartialFunction%5BThrowable%2C%20Boolean%5D)%3AFlowOps.this.Repr%5BT%5D" java="#onErrorComplete(java.util.function.Predicate)" } | ||
@apidoc[Source.onErrorComplete](Source) { scala="#onErrorComplete%5BT%20%3C%3A%20Throwable%5D()(implicit%20tag%3A%20ClassTag%5BT%5D)%3AFlowOps.this.Repr%5BT%5D" java="#onErrorComplete(java.lang.Class)" } | ||
@apidoc[Flow.onErrorComplete](Flow) { scala="#onErrorComplete(pf%3A%20PartialFunction%5BThrowable%2C%20Boolean%5D)%3AFlowOps.this.Repr%5BT%5D" java="#onErrorComplete(java.util.function.Predicate)" } | ||
@apidoc[Flow.onErrorComplete](Flow) { scala="#onErrorComplete%5BT%20%3C%3A%20Throwable%5D()(implicit%20tag%3A%20ClassTag%5BT%5D)%3AFlowOps.this.Repr%5BT%5D" java="#onErrorComplete(java.lang.Class)" } | ||
|
||
## Description | ||
|
||
Allows to complete the stream when an upstream error occur. | ||
|
||
## Reactive Streams semantics | ||
|
||
@@@div { .callout } | ||
|
||
**emits** element is available from the upstream | ||
|
||
**backpressures** downstream backpressures | ||
|
||
**completes** upstream completes or upstream failed with exception this operator can handle | ||
|
||
**Cancels when** downstream cancels | ||
@@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
stream-tests/src/test/scala/org/apache/pekko/stream/scaladsl/FlowOnErrorCompleteSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
|
||
package org.apache.pekko.stream.scaladsl | ||
|
||
import org.apache.pekko.stream.testkit.StreamSpec | ||
import org.apache.pekko.stream.testkit.scaladsl.TestSink | ||
|
||
import scala.concurrent.TimeoutException | ||
import scala.util.control.NoStackTrace | ||
|
||
class FlowOnErrorCompleteSpec extends StreamSpec { | ||
val ex = new RuntimeException("ex") with NoStackTrace | ||
|
||
"A CompleteOn" must { | ||
"can complete with all exceptions" in { | ||
Source(List(1, 2)) | ||
.map { a => | ||
if (a == 2) throw ex else a | ||
} | ||
.onErrorComplete[Throwable]() | ||
.runWith(TestSink[Int]()) | ||
.request(2) | ||
.expectNext(1) | ||
.expectComplete() | ||
} | ||
|
||
"can complete with dedicated exception type" in { | ||
Source(List(1, 2)) | ||
.map { a => | ||
if (a == 2) throw new IllegalArgumentException() else a | ||
} | ||
.onErrorComplete[IllegalArgumentException]() | ||
.runWith(TestSink[Int]()) | ||
.request(2) | ||
.expectNext(1) | ||
.expectComplete() | ||
} | ||
|
||
"can fail if an unexpected exception occur" in { | ||
Source(List(1, 2)) | ||
.map { a => | ||
if (a == 2) throw new IllegalArgumentException() else a | ||
} | ||
.onErrorComplete[TimeoutException]() | ||
.runWith(TestSink[Int]()) | ||
.request(1) | ||
.expectNext(1) | ||
.request(1) | ||
.expectError() | ||
} | ||
|
||
"can complete if the pf is applied" in { | ||
Source(List(1, 2)) | ||
.map { a => | ||
if (a == 2) throw new TimeoutException() else a | ||
} | ||
.onErrorComplete { | ||
case _: IllegalArgumentException => false | ||
case _: TimeoutException => true | ||
} | ||
.runWith(TestSink[Int]()) | ||
.request(2) | ||
.expectNext(1) | ||
.expectComplete() | ||
} | ||
|
||
"can fail if the pf is not applied" in { | ||
Source(List(1, 2)) | ||
.map { a => | ||
if (a == 2) throw ex else a | ||
} | ||
.onErrorComplete { | ||
case _: IllegalArgumentException => false | ||
case _: TimeoutException => true | ||
} | ||
.runWith(TestSink[Int]()) | ||
.request(2) | ||
.expectNext(1) | ||
.expectError() | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.