Last active
November 16, 2015 21:37
-
-
Save sergeykolbasov/e5a6e8d37d344fed4d99 to your computer and use it in GitHub Desktop.
Abstraction over service in finch
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
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