Skip to content

Instantly share code, notes, and snippets.

@pandanote-info
Created September 29, 2019 14:18
Show Gist options
  • Save pandanote-info/30a894d2df91c95248ea167cf8c926a2 to your computer and use it in GitHub Desktop.
Save pandanote-info/30a894d2df91c95248ea167cf8c926a2 to your computer and use it in GitHub Desktop.
Play frameworkのカスタムエラーページにmessagesファイルに定義されている文字列を読み込んで表示するためのコードを組み込んだエラーハンドラ。
// Sample Implementation to display a message from the message file.
// See https://pandanote.info/?p=5370 for details.
import org.slf4j.LoggerFactory
import javax.inject.Inject
import play.api.http.HttpErrorHandler
import play.api.mvc.Results._
import play.api.mvc.RequestHeader
import play.api.i18n.{ Langs, Lang, MessagesApi, MessagesImpl }
import scala.concurrent.Future
class SampleWebErrorHandler @Inject()(langs: Langs, messagesApi: MessagesApi) extends HttpErrorHandler {
private lazy val logger = LoggerFactory.getLogger(classOf[SampleWebErrorHandler])
val lang = langs.availables.head
override def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
logger.warn("ClientError: statusCode="+statusCode,message)
Future.successful(
Status(statusCode)(views.html.error.render(Option(statusCode), new MessagesImpl(lang, messagesApi)))
)
}
override def onServerError(request: RequestHeader, exception: Throwable) = {
logger.error("ServerError:"+exception.toString,exception)
Future.successful(
InternalServerError(views.html.error.render(None, new MessagesImpl(lang, messagesApi)))
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment