-
Notifications
You must be signed in to change notification settings - Fork 64
Implemented submarine error propagation for Handle
#619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lenguyenthanh
wants to merge
22
commits into
typelevel:main
Choose a base branch
from
lenguyenthanh:submarine-with-fancy-pants
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+272
−2
Open
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5ddf622
Implemented submarine error propagation for `Handle`
djspiewak 5d3c47e
Added headers
djspiewak 4936fef
Collapsed `Handle` tests and fixed Scala 3 issues
djspiewak c62bcdb
Swapped `ensure` for `allow`
djspiewak bc17ca3
Swapped `recover` for `rescue`
djspiewak b273d1f
Happy now?
djspiewak b7861fb
Add extra typing for raise to fix the nested tests
lenguyenthanh b255b4c
Update scalafmt runner dialect for scala 3 code
lenguyenthanh 25d703c
Fix scala 2.12 compilation
lenguyenthanh 4f19410
Use using instead of implicit for scala 3
lenguyenthanh cb545b9
Remove duplicated files
lenguyenthanh 0b20cfc
Use AnyVal to avoid allocations
lenguyenthanh 74dda73
Use HandleCrossCompat to follow code convention
lenguyenthanh 85e418b
Add inline for AdhocSyntaxWired.apply
lenguyenthanh d1167df
Use FunSuite instead of BaseSuite
lenguyenthanh a70b9f4
Use dummy Byte instead of Unit to avoid warning
lenguyenthanh 4c26d00
Use method call with implicit to avoid one extra allocation
lenguyenthanh e9c0d37
Extract InnerHandler out of rescue
lenguyenthanh d1382bd
Inlining inner function
lenguyenthanh 3f7973f
minor stylistic update
lenguyenthanh 5de654a
Inline arguments of inner
lenguyenthanh fb9f86f
union tests for scala 3
lenguyenthanh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,20 @@ | ||
/* | ||
* Copyright 2021 Typelevel | ||
* | ||
* Licensed 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 cats | ||
package mtl | ||
|
||
private[mtl] trait HandleCrossCompat |
This file contains hidden or 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,46 @@ | ||
/* | ||
* Copyright 2021 Typelevel | ||
* | ||
* Licensed 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 cats | ||
package mtl | ||
|
||
private[mtl] trait HandleCrossCompat { this: Handle.type => | ||
import Handle.Submarine | ||
|
||
inline def allow[E]: AdHocSyntaxWired[E] = | ||
new AdHocSyntaxWired[E]() | ||
|
||
private[mtl] final class AdHocSyntaxWired[E]: | ||
inline def apply[F[_], A](inline body: Handle[F, E] ?=> F[A]): InnerWired[F, E, A] = | ||
new InnerWired(body) | ||
|
||
private[mtl] final class InnerWired[F[_], E, A](body: Handle[F, E] ?=> F[A]): | ||
def rescue(h: E => F[A]): ApplicativeThrow[F] ?=> F[A] = | ||
val Marker = new AnyRef | ||
|
||
def inner[B](fb: F[B])(f: E => F[B]): F[B] = | ||
ApplicativeThrow[F].handleErrorWith(fb): | ||
case Submarine(e, Marker) => f(e.asInstanceOf[E]) | ||
case t => ApplicativeThrow[F].raiseError(t) | ||
|
||
given Handle[F, E] with | ||
def applicative = Applicative[F] | ||
def raise[E2 <: E, B](e: E2): F[B] = | ||
ApplicativeThrow[F].raiseError(Submarine(e, Marker)) | ||
def handleWith[B](fb: F[B])(f: E => F[B]): F[B] = inner(fb)(f) | ||
|
||
inner(body)(h) | ||
} |
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
tests/shared/src/test/scala-3/cats/mtl/tests/Handle3Tests.scala
This file contains hidden or 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,59 @@ | ||
/* | ||
* Copyright 2021 Typelevel | ||
* | ||
* Licensed 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 cats | ||
package mtl | ||
package tests | ||
|
||
import cats.data.EitherT | ||
import cats.mtl.syntax.all.* | ||
import cats.syntax.all.* | ||
import cats.mtl.Handle.* | ||
|
||
class Handle3Tests extends munit.FunSuite: | ||
|
||
type F[A] = EitherT[Eval, Throwable, A] | ||
|
||
test("submerge custom errors (scala 3)"): | ||
enum Error: | ||
case First, Second, Third | ||
|
||
val test = | ||
allow[Error]: | ||
Error.Second.raise[F, String].as("nope") | ||
.rescue: | ||
case Error.First => "0".pure[F] | ||
case Error.Second => "1".pure[F] | ||
case Error.Third => "2".pure[F] | ||
|
||
assert.equals(test.value.value.toOption, Some("1")) | ||
|
||
test("submerge two independent errors (scala 3)"): | ||
enum Error1: | ||
case First, Second, Third | ||
enum Error2: | ||
case Fourth | ||
val test = | ||
allow[Error1]: | ||
allow[Error2]: | ||
Error1.Third.raise[F, String].as("nope") | ||
.rescue: | ||
case e => e.toString.pure[F] | ||
.rescue: | ||
case Error1.First => "first1".pure[F] | ||
case Error1.Second => "second1".pure[F] | ||
case Error1.Third => "third1".pure[F] | ||
assert.equals(test.value.value.toOption, Some("third1")) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.