Skip to content

Instantly share code, notes, and snippets.

@sergeykolbasov
Last active November 16, 2015 21:37
Show Gist options
  • Save sergeykolbasov/e5a6e8d37d344fed4d99 to your computer and use it in GitHub Desktop.
Save sergeykolbasov/e5a6e8d37d344fed4d99 to your computer and use it in GitHub Desktop.
Abstraction over service in finch
class SimpleHttpService {
override def apply(f: (RequestContext) => FutureResult[ErrorResult, Response]): Service[Request, Response] = new Service[Request, Response] {
override def apply(request: Request): Future[Response] = {
for {
context <- requestContext(request)
response <- f.andThen(errorHandler)(context)
} yield {
response
}
}
}
private def errorHandler(futureResult: FutureResult[ErrorResult, Response]): Future[Response] {
futureResult.map({
case Failure(error: ErrorResult) => throw ErrorException(error) //throw exception and catch it with filter
case Success(data) => data
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment