Created
June 25, 2014 04:14
-
-
Save laurencer/50c0ea2fcef1e955b191 to your computer and use it in GitHub Desktop.
Demonstrates how errors affect `scalaz-stream` when using `io.resource`.
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
import scala.util._ | |
import scalaz._, Scalaz._, concurrent._, stream._ | |
// Testing error handling with scalaz-stream `io.resource` | |
// Specifically how it works when a Sink/Channel produces | |
// a function. | |
def testErrorInOpen = { | |
println("Error in Open Resource Task") | |
// Takes `String`s and produces `Int`s in the `Task` effect. | |
val channel: Channel[Task, String, Try[Int]] = io.resource(Task { | |
throw new Exception() | |
})(resource => Task { | |
0 | |
})(resource => Task.now { | |
(number: String) => Task { Try(number.toInt) } | |
}) | |
Process.emitSeq[Task, String](List("1", "2", "3", "foo", "4", "5")) | |
.through(channel) | |
.runLog | |
.run | |
} | |
def testErrorInResourceClose = { | |
println("Error in Close Resource Task") | |
// Takes `String`s and produces `Int`s in the `Task` effect. | |
val channel: Channel[Task, String, Try[Int]] = io.resource(Task { | |
0 | |
})(resource => Task { | |
throw new Exception() | |
})(resource => Task.now { | |
(number: String) => Task { Try(number.toInt) } | |
}) | |
Process.emitSeq[Task, String](List("1", "2", "3", "4", "5")) | |
.through(channel) | |
.runLog | |
.run | |
} | |
def testErrorInChannel = { | |
println("Error in Iterate Task") | |
// Takes `String`s and produces `Int`s in the `Task` effect. | |
val channel: Channel[Task, String, Try[Int]] = io.resource(Task { | |
0 | |
})(resource => Task { | |
0 | |
})(resource => Task.now { | |
(number: String) => Task { Try(number.toInt) } | |
}) | |
Process.emitSeq[Task, String](List("1", "2", "3", "foo", "4", "5")) | |
.through(channel) | |
.runLog | |
.run | |
} | |
testErrorInOpen | |
testErrorInResourceClose | |
testErrorInChannel | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment