Browse Source

server: Add custom play HttpErrorHandler

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
068746b52c
  1. 41
      server/app/com/xsn/explorer/play/MyHttpErrorHandler.scala
  2. 4
      server/conf/application.conf

41
server/app/com/xsn/explorer/play/MyHttpErrorHandler.scala

@ -0,0 +1,41 @@
package com.xsn.explorer.play
import javax.inject.Inject
import com.alexitc.playsonify.PublicErrorRenderer
import com.alexitc.playsonify.models.{ErrorId, PublicError}
import org.slf4j.LoggerFactory
import play.api.http.HttpErrorHandler
import play.api.libs.json.Json
import play.api.mvc.RequestHeader
import play.api.mvc.Results.{InternalServerError, Status}
import scala.concurrent.Future
class MyHttpErrorHandler @Inject() (errorRenderer: PublicErrorRenderer) extends HttpErrorHandler {
private val logger = LoggerFactory.getLogger(this.getClass)
def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
val publicError = PublicError.genericError(message)
val error = errorRenderer.renderPublicError(publicError)
val json = Json.obj(
"errors" -> Json.arr(error)
)
val result = Status(statusCode)(json)
Future.successful(result)
}
def onServerError(request: RequestHeader, exception: Throwable) = {
val errorId = ErrorId.create
val error = errorRenderer.renderPrivateError(errorId)
val json = Json.obj(
"errors" -> Json.arr(error)
)
logger.error(s"Server error, errorId = [${errorId.string}]", exception)
val result = InternalServerError(json)
Future.successful(result)
}
}

4
server/conf/application.conf

@ -6,6 +6,10 @@ play.filters.hosts {
allowed = ["localhost", "localhost:9000", "127.0.0.1:9000"]
}
play.http {
errorHandler = "com.xsn.explorer.play.MyHttpErrorHandler"
}
rpc {
host = "http://localhost:51473"
username = "dummy"

Loading…
Cancel
Save