If you just want to turn all of the rejections into your custom json format, you can create a rejection handler. For example, I'll put this in my ServiceActor
and do the following:
class ApiServiceActor extends Actor with HttpServiceActor with ApiServices {
def jsonify(response: HttpResponse): HttpResponse = {
response.withEntity(HttpBody(ContentType.`application/json`,
JSONObject(Map(
"details" -> response.entity.asString.toJson,
"errorType" -> ApiErrorType.Unknown.toJson
)).toString()))
}
implicit val apiRejectionHandler = RejectionHandler {
case rejections => mapHttpResponse(jsonify) {
RejectionHandler.Default(rejections)
}
}
def receive = runRoute {
yourRoute ~ yourOtherRoute ~ someOtherRoute
}
}