Skip to content

Commit ba74ed4

Browse files
committed
use handleError instead of recoverError (so use Scala's func instead of partial func)
1 parent 6f034e4 commit ba74ed4

8 files changed

Lines changed: 29 additions & 37 deletions

File tree

application/src/main/scala/application/Version4.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package application
1212
- orchestrate domain, infrastructure, parsing and error handling
1313
- run the whole app lifted in the IO monad
1414
- print final rover output to the console if everything is ok
15-
- recover from any unhandled error and print it
15+
- handle any unhandled error by printing it
1616
*/
1717
object Version4 {
1818

application/src/main/scala/application/Version5.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ object Version5 {
7474
_ <- runMission(display, planet, rover, commands)
7575
} yield ()
7676

77-
runResult.recoverWith(display.error(_))
77+
runResult.handleErrorWith(display.error)
7878
}
7979

8080
// NOTE: is used display to print obstacle/completed cases

application/src/main/scala/application/Version6.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ object Version6 {
8181

8282
loadResult
8383
.map(toSuccessful)
84-
.recover(toFailed(_))
84+
.handleError(toFailed)
8585
// NOTE: signal to the Runtime to continue the loop
8686
.map(continue)
8787

application/src/main/scala/application/answers/Version4.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object Version4 {
1919
_ <- runMission(planet, rover, commands)
2020
} yield ()
2121

22-
runResult.recoverWith(writeError(_))
22+
runResult.handleErrorWith(writeError)
2323
}
2424

2525
def runMission(planet: Planet, rover: Rover, commands: List[Command]): IO[Unit] =

application/src/main/scala/application/answers/Version5.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object Version5 {
5757
_ <- runMission(display, planet, rover, commands)
5858
} yield ()
5959

60-
runResult.recoverWith(display.error(_))
60+
runResult.handleErrorWith(display.error)
6161
}
6262

6363
def runMission(display: DisplayWriter, planet: Planet, rover: Rover, commands: List[Command]): IO[Unit] =

application/src/main/scala/application/answers/Version6.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ object Version6 {
6767

6868
loadResult
6969
.map(toSuccessful)
70-
.recover(toFailed(_))
70+
.handleError(toFailed)
7171
.map(continue)
7272

7373
case Effect.AskCommands =>

demos/src/test/scala/demos/Either.scala

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package demos
33
@munit.IgnoreSuite
44
class EitherDemo extends munit.FunSuite {
55

6+
import cats.syntax.applicativeError._
67
import cats.syntax.either._
78

89
test("creation") {
@@ -42,30 +43,16 @@ class EitherDemo extends munit.FunSuite {
4243
}
4344

4445
test("error handling") {
45-
46-
def handle(e: Either[String, Int]): Either[String, Int] =
47-
e.recover(e =>
48-
e match {
49-
case "unknown" => 42
50-
}
51-
)
46+
val e1: Either[String, Int] = Left("unknown")
5247

5348
// The Left[String] value is converted to a Right[Int]
54-
val e1: Either[String, Int] = handle(Left("unknown"))
55-
56-
// The Left[String] value isn't converted
57-
val e2: Either[String, Int] = handle(Left("anything else"))
49+
val e2: Either[String, Int] =
50+
e1.handleError(e => e.hashCode())
5851

59-
def handleWith(e: Either[String, Int]): Either[String, Int] =
60-
e.recoverWith(e =>
61-
e match {
62-
case "unknown" => Right(42)
63-
}
64-
)
52+
val e3: Either[String, Int] = Left("unknown")
6553

6654
// Same conversions as before
67-
val e3: Either[String, Int] = handleWith(Left("unknown"))
68-
val e4: Either[String, Int] = handleWith(Left("anything else"))
55+
val e4: Either[String, Int] = e3.handleErrorWith(e => Right(e.hashCode))
6956
}
7057

7158
}

demos/src/test/scala/demos/IO.scala

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class IODemo extends munit.FunSuite {
1818
val io2 = IO(throw new RuntimeException("boom!"))
1919

2020
// In order evaluate our program we explicitly call unsafeRunSync
21-
IO(5).unsafeRunSync()
21+
io1.unsafeRunSync()
2222

2323
// Pay attention to the pure function, it's eager-evaluated
2424

@@ -29,7 +29,20 @@ class IODemo extends munit.FunSuite {
2929
// val io4 = IO.pure(System.out.println("hi!"))
3030

3131
// Eval throw and then create IO
32-
// val io5 = I.pureO(throw new RuntimeException("boom!"))
32+
// val io5 = IO.pure(throw new RuntimeException("boom!"))
33+
}
34+
35+
test("manipulation") {
36+
val io1: IO[Int] = IO(5)
37+
38+
// Change the right type parameter's type
39+
val io2: IO[String] = io1.map(_.toString)
40+
41+
// Change IO
42+
val io3: IO[String] = io2.flatMap(_ => IO("hi!"))
43+
44+
// Change IO's state
45+
val io4: IO[String] = io3.flatMap(_ => IO(throw new RuntimeException("boom!")))
3346
}
3447

3548
test("error handling") {
@@ -39,18 +52,10 @@ class IODemo extends munit.FunSuite {
3952
val io1: IO[String] = IO(throw UnreachableServer())
4053

4154
val io2: IO[String] =
42-
io1.recover(e =>
43-
e match {
44-
case UnreachableServer() => "localhost"
45-
}
46-
)
55+
io1.handleError(e => e.getMessage())
4756

4857
val io3: IO[String] =
49-
io1.recoverWith(e =>
50-
e match {
51-
case UnreachableServer() => IO("localhost")
52-
}
53-
)
58+
io1.handleErrorWith(e => IO.pure(e.getMessage))
5459

5560
// Plus, IO provides attempt function that trap any error and
5661
// materialize them as Either[Throwable, A]

0 commit comments

Comments
 (0)