Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save laurencer/50c0ea2fcef1e955b191 to your computer and use it in GitHub Desktop.
Save laurencer/50c0ea2fcef1e955b191 to your computer and use it in GitHub Desktop.
Demonstrates how errors affect `scalaz-stream` when using `io.resource`.
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