Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Play frameworkのカスタムエラーページにmessagesファイルに定義されている文字列を読み込んで表示するためのコードを組み込んだエラーハンドラ。
// Sample Implementation to display a message from the message file.
// See 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)
Status(statusCode)(views.html.error.render(Option(statusCode), new MessagesImpl(lang, messagesApi)))
override def onServerError(request: RequestHeader, exception: Throwable) = {
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