Skip to content

Instantly share code, notes, and snippets.

@vkostyukov
Last active January 23, 2016 00:55
Show Gist options
  • Save vkostyukov/f759448a430863f9237d to your computer and use it in GitHub Desktop.
Save vkostyukov/f759448a430863f9237d to your computer and use it in GitHub Desktop.
import io.finch._
import com.twitter.finagle.http.Status
import io.circe._
import io.circe.generic.auto._
import io.finch.circe._
// Here we tell Finch how to encode exceptions to send them over the wire.
val e: Encoder[Exception] = Encoder.instance {
case ce: MyError => MyError.jsonEncoder(ce)
case t: Throwable => Json.obj("message" -> Json.string(t.getMessage))
}
// Here we _handle_ errors - handling is only about whether or not we'll send a remote client
// this excpeption over the wire. Unhandled exceptions are converted into 500s without payloads.
(ping :+: ep1 :+: ep2 :+: ep3 :+: ep4).handle {
case ce: MyError => Output.failure(ce, ce.status)
case t: Throwable =>
logger.warn("Unhandled exception: ", t)
Output.failure(new Exception(t), Status.InternalServerError )
}.toService
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment