当前位置: 首页>>代码示例>>Scala>>正文


Scala DefaultHttpErrorHandler类代码示例

本文整理汇总了Scala中play.api.http.DefaultHttpErrorHandler的典型用法代码示例。如果您正苦于以下问题:Scala DefaultHttpErrorHandler类的具体用法?Scala DefaultHttpErrorHandler怎么用?Scala DefaultHttpErrorHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了DefaultHttpErrorHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: ErrorHandler

//设置package包名称以及导入依赖的类
package conf.util

import javax.inject._

import play.api._
import play.api.http.DefaultHttpErrorHandler
import play.api.mvc.Results._
import play.api.mvc._
import play.api.routing.Router

import scala.concurrent._


class ErrorHandler @Inject()(
         env: Environment,
         config: Configuration,
         sourceMapper: OptionalSourceMapper,
         router: Provider[Router])
  extends DefaultHttpErrorHandler(env, config, sourceMapper, router) {

  override def onProdServerError(request: RequestHeader, exception: UsefulException) = {
    Future.successful(
      InternalServerError("A server error occurred: " + exception.getMessage)
    )
  }

  override protected def onNotFound(request: RequestHeader, message: String): Future[Result] = {
    Future.successful(
      InternalServerError("A server error occurred: " + message)
    )
  }

  override def onForbidden(request: RequestHeader, message: String) = {
    Future.successful(
      Forbidden("You're not allowed to access this resource.")
    )
  }

  override def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    Future.successful(
      Status(statusCode)("A client error occurred: " + message)
    )
  }

  override def onServerError(request: RequestHeader, exception: Throwable) = {
    Future.successful(
      InternalServerError("A server error occurred: " + exception.getMessage)
    )
  }

  override protected def onBadRequest(request: RequestHeader, message: String): Future[Result] = {
    Future.successful(
      InternalServerError("A server error occurred: " + message)
    )
  }




} 
开发者ID:Masebeni,项目名称:hwork,代码行数:61,代码来源:ErrorHandler.scala

示例2: ErrorHandler

//设置package包名称以及导入依赖的类
package utils

import javax.inject.{Inject, Provider}

import com.mohiva.play.silhouette.api.actions.SecuredErrorHandler
import play.api.http.DefaultHttpErrorHandler
import play.api.i18n.{I18nSupport, Messages, MessagesApi}
import play.api.mvc.Results._
import play.api.mvc.{RequestHeader, Result}
import play.api.routing.Router
import play.api.{Configuration, OptionalSourceMapper}

import scala.concurrent.Future
import controllers.{WebJarAssets, routes}

class ErrorHandler @Inject() (
                               val messagesApi: MessagesApi,
                               env: play.api.Environment,
                               config: Configuration,
                               sourceMapper: OptionalSourceMapper,
                               router: javax.inject.Provider[Router],
                               p: Provider[WebJarAssets])
  extends DefaultHttpErrorHandler(env, config, sourceMapper, router)
    with SecuredErrorHandler with I18nSupport {

  // https://www.playframework.com/documentation/2.5.x/Migration25#Handling-legacy-components
  implicit lazy val webJarAssets = p.get()

  override def onNotAuthenticated(implicit request: RequestHeader): Future[Result] =
    Future.successful(Redirect(routes.Auth.signIn()))

  override def onNotAuthorized(implicit request: RequestHeader): Future[Result] =
    Future.successful(Redirect(routes.Auth.signIn()).flashing("error" -> Messages("error.accessDenied")))

  override def onNotFound(request: RequestHeader, message: String): Future[Result] =
    Future.successful(Ok(views.html.errors.notFound(request)))

  override def onServerError(request:RequestHeader, exception:Throwable):Future[Result] =
    Future.successful(Ok(views.html.errors.serverError(request, exception)))
} 
开发者ID:tm-sukehiro,项目名称:play-hands-on,代码行数:41,代码来源:ErrorHandler.scala

示例3: ErrorHandler

//设置package包名称以及导入依赖的类
import play.api.http.DefaultHttpErrorHandler
import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import play.api.routing.Router
import scala.concurrent.Future
import javax.inject._

class ErrorHandler @Inject() (
    env: Environment,
    config: Configuration,
    sourceMapper: OptionalSourceMapper,
    router: Provider[Router],
    webErrorHandler: web.ErrorHandler,
    adminErrorHandler: admin.ErrorHandler) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) {

  
  private def getSubdomain(request: RequestHeader) = request.domain.replaceFirst("[\\.]?[^\\.]+[\\.][^\\.]+$", "")

  // 404 - page not found error
  override def onNotFound(request: RequestHeader, message: String): Future[Result] = getSubdomain(request) match {
    case "admin" => adminErrorHandler.onNotFound(request, message)
    case _ => webErrorHandler.onNotFound(request, message)
  }

  // 500 - internal server error
  override def onProdServerError(request: RequestHeader, exception: UsefulException) = getSubdomain(request) match {
    case "admin" => adminErrorHandler.onProdServerError(request, exception)
    case _ => webErrorHandler.onProdServerError(request, exception)
  }

} 
开发者ID:pawank,项目名称:play-silhouette-mongodb-multi-project-sbt,代码行数:33,代码来源:ErrorHandler.scala

示例4: ErrorHandler

//设置package包名称以及导入依赖的类
package web

import play.api.http.DefaultHttpErrorHandler
import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import play.api.i18n.{ I18nSupport, MessagesApi, Messages }
import play.api.routing.Router
import scala.concurrent._
import javax.inject._

class ErrorHandler @Inject() (
    env: Environment,
    config: Configuration,
    sourceMapper: OptionalSourceMapper,
    router: Provider[Router],
    val messagesApi: MessagesApi) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) with I18nSupport {

  // 404 - page not found error
  override def onNotFound(request: RequestHeader, message: String): Future[Result] = Future.successful {
    NotFound(env.mode match {
      case Mode.Prod => views.html.web.errors.notFound(request)(request2Messages(request))
      case _ => views.html.defaultpages.devNotFound(request.method, request.uri, Some(router.get))
    })
  }

  // 500 - internal server error
  override def onProdServerError(request: RequestHeader, exception: UsefulException) = Future.successful {
    InternalServerError(views.html.web.errors.error(request, exception)(request2Messages(request)))
  }
} 
开发者ID:pawank,项目名称:play-silhouette-mongodb-multi-project-sbt,代码行数:32,代码来源:ErrorHandler.scala

示例5: ErrorHandler

//设置package包名称以及导入依赖的类
package admin

import play.api.http.DefaultHttpErrorHandler
import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import play.api.i18n.{ I18nSupport, MessagesApi, Messages }
import play.api.routing.Router
import scala.concurrent._
import javax.inject._

class ErrorHandler @Inject() (
    env: Environment,
    config: Configuration,
    sourceMapper: OptionalSourceMapper,
    router: Provider[Router],
    val messagesApi: MessagesApi) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) with I18nSupport {

  // 404 - page not found error
  override def onNotFound(request: RequestHeader, message: String): Future[Result] = Future.successful {
    NotFound(env.mode match {
      case Mode.Prod => views.html.admin.errors.notFound(request)(request2Messages(request))
      case _ => views.html.defaultpages.devNotFound(request.method, request.uri, Some(router.get))
    })
  }

  // 500 - internal server error
  override def onProdServerError(request: RequestHeader, exception: UsefulException) = Future.successful {
    InternalServerError(views.html.admin.errors.error(request, exception)(request2Messages(request)))
  }
} 
开发者ID:pawank,项目名称:play-silhouette-mongodb-multi-project-sbt,代码行数:32,代码来源:ErrorHandler.scala

示例6: ErrorHandler

//设置package包名称以及导入依赖的类
import javax.inject._

import play.api._
import play.api.http.DefaultHttpErrorHandler
import play.api.mvc.Results._
import play.api.mvc._
import play.api.routing.Router

import scala.concurrent.Future

@Singleton
class ErrorHandler @Inject()(
  env: Environment,
  config: Configuration,
  sourceMapper: OptionalSourceMapper,
  router: Provider[Router]
) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) {

  override def onProdServerError(request: RequestHeader, exception: UsefulException): Future[Result] = Future.successful {
    InternalServerError("A server error occurred: " + exception.getMessage)
  }

  override def onClientError(request: RequestHeader, statusCode: Int, message: String): Future[Result] = env.mode match {
    case Mode.Prod => Future.successful(Results.Status(statusCode)(message))
    case _ => super.onClientError(request, statusCode, message)
  }
} 
开发者ID:stonexx,项目名称:utils,代码行数:28,代码来源:ErrorHandler.scala

示例7: ErrorHandler

//设置package包名称以及导入依赖的类
import javax.inject._

import play.api._
import play.api.http.DefaultHttpErrorHandler
import play.api.mvc.Results._
import play.api.mvc._
import play.api.routing.Router

import scala.concurrent._

@Singleton
class ErrorHandler @Inject()(
    env: Environment,
    config: Configuration,
    sourceMapper: OptionalSourceMapper,
    router: Provider[Router]
) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) {

  override def onProdServerError(request: RequestHeader, exception: UsefulException) =
    Future.successful(InternalServerError("A server error occurred: " + exception.getMessage))

  override def onForbidden(request: RequestHeader, message: String) =
    Future.successful(Forbidden("You're not allowed to access this resource."))

  override protected def onNotFound(request: RequestHeader, message: String) =
    Future.successful(NotFound("Resource not found"))

  override protected def onBadRequest(request: RequestHeader, message: String) =
    Future.successful(BadRequest("Bad request"))

} 
开发者ID:scalafiddle,项目名称:scalafiddle-editor,代码行数:32,代码来源:ErrorHandler.scala

示例8: ErrorHandler

//设置package包名称以及导入依赖的类
package conf.util

import javax.inject._
import play.api.http.DefaultHttpErrorHandler
import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import play.api.routing.Router
import scala.concurrent._
import play.api.libs.concurrent.Execution.Implicits.defaultContext


class ErrorHandler @Inject()(
         env: Environment,
         config: Configuration,
         sourceMapper: OptionalSourceMapper,
         router: Provider[Router])
  extends DefaultHttpErrorHandler(env, config, sourceMapper, router) {

  override def onProdServerError(request: RequestHeader, exception: UsefulException) = {
    Future.successful(
      InternalServerError("A server error occurred: " + exception.getMessage)
    )
  }

  override protected def onNotFound(request: RequestHeader, message: String): Future[Result] = {
    Future.successful(
      InternalServerError("A server error occurred: " + message)
    )
  }

  override def onForbidden(request: RequestHeader, message: String) = {
    Future.successful(
      Forbidden("You're not allowed to access this resource.")
    )
  }

  override def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    Future.successful(
      Status(statusCode)("A client error occurred: " + message)
    )
  }

  override def onServerError(request: RequestHeader, exception: Throwable) = {
    Future.successful(
      InternalServerError("A server error occurred: " + exception.getMessage)
    )
  }

  override protected def onBadRequest(request: RequestHeader, message: String): Future[Result] = {
    Future.successful(
      InternalServerError("A server error occurred: " + message)
    )
  }




} 
开发者ID:Thulebona,项目名称:hashpvtapi,代码行数:60,代码来源:ErrorHandler.scala

示例9: RegExCORSFilter

//设置package包名称以及导入依赖的类
package com.github.dgilling.regexcors

import akka.stream.Materializer
import play.api.Logger
import play.api.http.{DefaultHttpErrorHandler, HttpErrorHandler}
import play.api.mvc.{Result, RequestHeader, Filter}
import play.core.j.JavaHttpErrorHandlerAdapter
import play.filters.cors.{CORSFilter, AbstractCORSPolicy, CORSConfig}

import scala.concurrent.Future


class RegExCORSFilter(
                  override protected val corsConfig: CORSConfig = CORSConfig(),
                  override protected val errorHandler: HttpErrorHandler = DefaultHttpErrorHandler,
                  private val pathPrefixes: Seq[String] = Seq("/"),
                  private val ignoredPathPrefixes: Seq[String] = Seq.empty[String])(override implicit val mat: Materializer)
  extends CORSFilter {

  // Java constructor
  def this(corsConfig: CORSConfig, errorHandler: play.http.HttpErrorHandler, pathPrefixes: java.util.List[String])(mat: Materializer) = {
    this(corsConfig, new JavaHttpErrorHandlerAdapter(errorHandler), Seq(pathPrefixes.toArray.asInstanceOf[Array[String]]: _*))(mat)
  }

  override protected val logger = Logger(classOf[CORSFilter])

  override def apply(f: RequestHeader => Future[Result])(request: RequestHeader): Future[Result] = {
    if (pathPrefixes.exists(request.path.startsWith) &&
      !ignoredPathPrefixes.exists(request.path.startsWith)) {
      filterRequest(f, request)
    } else {
      f(request)
    }
  }
}

object RegExCORSFilter {

  val RequestTag = "CORS_REQUEST"

  def apply(corsConfig: CORSConfig = CORSConfig(), errorHandler: HttpErrorHandler = DefaultHttpErrorHandler,
            pathPrefixes: Seq[String] = Seq("/"))(implicit mat: Materializer) =
    new CORSFilter(corsConfig, errorHandler, pathPrefixes)

} 
开发者ID:dgilling,项目名称:play-regex-cors,代码行数:46,代码来源:RegExCORSFilter.scala

示例10: ErrorHandler

//设置package包名称以及导入依赖的类
import javax.inject._

import play.api.http.DefaultHttpErrorHandler
import play.api._
import play.api.libs.json.JsString
import play.api.mvc._
import play.api.mvc.Results._
import play.api.routing.Router
import services.ApiSecurity

import scala.concurrent._

@Singleton
class ErrorHandler @Inject() (
                               env: Environment,
                               config: Configuration,
                               sourceMapper: OptionalSourceMapper,
                               router: Provider[Router]
                             ) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) with ApiSecurity {

  override def onBadRequest(request: RequestHeader, message: String): Future[Result] = {
    Future.successful(
      JsonAnswer(400,JsString("Bad Request Error: " + message))(request)
    )
  }

  override def onServerError(request: RequestHeader, exception: Throwable): Future[Result] = {
    Future.successful(
      JsonAnswer(500,JsString("Internal Server Error: " + exception.getMessage))(request)
    )
  }

  override def onNotFound(request: RequestHeader, message: String): Future[Result] = {
    Future.successful(
      JsonAnswer(404,JsString("Not Found Error: " + message))(request)
    )
  }

  override def onForbidden(request: RequestHeader, message: String) = {
    Future.successful(
      Forbidden("You're not allowed to access this resource.")
    )
  }
} 
开发者ID:kormoglaz,项目名称:TestTask,代码行数:45,代码来源:ErrorHandler.scala

示例11: ErrorHandler

//设置package包名称以及导入依赖的类
package utils

import javax.inject.Inject

import com.mohiva.play.silhouette.api.SecuredErrorHandler

import play.api.http.DefaultHttpErrorHandler
import play.api.i18n.{I18nSupport,Messages,MessagesApi}
import play.api.mvc.Results._
import play.api.mvc.{Result,RequestHeader}
import play.api.routing.Router
import play.api.{OptionalSourceMapper,Configuration}

import scala.concurrent.Future

class ErrorHandler @Inject() (
  val messagesApi: MessagesApi,
  env: play.api.Environment,
  config: Configuration,
  sourceMapper: OptionalSourceMapper,
  router: javax.inject.Provider[Router])
  extends DefaultHttpErrorHandler(env, config, sourceMapper, router)
  with SecuredErrorHandler with I18nSupport {

  override def onNotAuthenticated(request: RequestHeader, messages: Messages): Option[Future[Result]] =
    Some(Future.successful(Ok("please sign in")))

  override def onNotAuthorized(request: RequestHeader, messages: Messages): Option[Future[Result]] =
    Some(Future.successful(Ok(Messages("error.accessDenied")(messages))))

  override def onNotFound(request: RequestHeader, message: String): Future[Result] = 
    Future.successful(Ok("not found"))

  override def onServerError(request:RequestHeader, exception:Throwable):Future[Result] = 
    Future.successful(Ok("server error"))
} 
开发者ID:k19862217,项目名称:book-shoes-api,代码行数:37,代码来源:ErrorHandler.scala

示例12: ErrorHandler

//设置package包名称以及导入依赖的类
package utils

import javax.inject.Inject

import com.mohiva.play.silhouette.api.SecuredErrorHandler

import play.api.http.DefaultHttpErrorHandler
import play.api.i18n.{I18nSupport,Messages,MessagesApi}
import play.api.mvc.Results._
import play.api.mvc.{Result,RequestHeader}
import play.api.routing.Router
import play.api.{OptionalSourceMapper,Configuration}

import scala.concurrent.Future

import controllers.routes

class ErrorHandler @Inject() (
  val messagesApi: MessagesApi,
  env: play.api.Environment,
  config: Configuration,
  sourceMapper: OptionalSourceMapper,
  router: javax.inject.Provider[Router])
  extends DefaultHttpErrorHandler(env, config, sourceMapper, router)
  with SecuredErrorHandler with I18nSupport {

  override def onNotAuthenticated(request: RequestHeader, messages: Messages): Option[Future[Result]] =
    Some(Future.successful(Redirect(routes.Auth.signIn())))

  override def onNotAuthorized(request: RequestHeader, messages: Messages): Option[Future[Result]] =
    Some(Future.successful(Redirect(routes.Auth.signIn()).flashing("error" -> Messages("error.accessDenied")(messages))))

  override def onNotFound(request: RequestHeader, message: String): Future[Result] = 
    Future.successful(Ok(views.html.errors.notFound(request)))

  override def onServerError(request:RequestHeader, exception:Throwable):Future[Result] = 
    Future.successful(Ok(views.html.errors.serverError(request, exception)))
} 
开发者ID:Viva-con-Agua,项目名称:drops,代码行数:39,代码来源:ErrorHandler.scala

示例13: ServerErrorHandler

//设置package包名称以及导入依赖的类
package utils.web

import org.slf4j.LoggerFactory
import play.api._
import play.api.http.DefaultHttpErrorHandler
import play.api.mvc.Results._
import play.api.mvc._
import play.api.routing.Router
import play.core.SourceMapper

import scala.concurrent.Future

class ServerErrorHandler(
    env: play.api.Environment, config: Configuration,
    sourceMapper: Option[SourceMapper], router: Option[Router]
) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) {

  private val log = LoggerFactory.getLogger(classOf[ServerErrorHandler])

  override def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    Future.successful(
      Status(statusCode)("A client error occurred: " + message)
    )
  }

  override def onServerError(request: RequestHeader, exception: Throwable) = {
    log.error("Server error " + exception.getMessage + "for uri" + request.uri)
    Future.successful(
      InternalServerError("A server error occurred: " + exception.getMessage)
    )
  }
} 
开发者ID:iriddhi,项目名称:mis,代码行数:33,代码来源:ServerErrorHandler.scala

示例14: ErrorHandler

//设置package包名称以及导入依赖的类
package ylabs.play.common

import java.io.{PrintWriter, StringWriter}
import javax.inject.{Inject, Provider}

import play.api.http.DefaultHttpErrorHandler
import play.api.libs.json.{JsObject, JsString, Json, Writes}
import play.api.mvc.RequestHeader
import play.api.mvc.Results._
import play.api.routing.Router
import play.api.{Configuration, Environment, OptionalSourceMapper, UsefulException}
import ylabs.play.common.models.Helpers.ApiFailure.Failed
import ylabs.play.common.models.ValidationError.{Field, Invalid, Reason}

import scala.concurrent.Future

class ErrorHandler @Inject() (
    env: Environment,
    config: Configuration,
    sourceMapper: OptionalSourceMapper,
    router: Provider[Router]) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) {

  def prettyTrace(t: Throwable) = {
    val sw = new StringWriter
    t.printStackTrace(new PrintWriter(sw))
    sw.toString
  }

  implicit def exceptionWrites: Writes[Throwable] = new Writes[Throwable] {
    def writes(t: Throwable) = JsObject(Seq(
      "message" -> JsString(t.getMessage),
      "trace" -> JsString(prettyTrace(t))))
  }

  override def onDevServerError(request: RequestHeader, exception: UsefulException) = {
    Future.successful(InternalServerError(Json.toJson(exception)))
  }

  override def onProdServerError(request: RequestHeader, exception: UsefulException) = {
    Future.successful(InternalServerError(Json.toJson("Internal Error")))
  }

  override def onBadRequest(request: RequestHeader, message: String) = {
    def errorToMessage(error: String) = error match {
      case "error.path.missing" ? "Value missing"
      case _                    ? error
    }

    //This is a bit of a mess because play doesn't allow invalid json to get to our client code
    //Message will be something like "Json validation error List((obj.name,List(ValidationError(List(error.path.missing),WrappedArray()))))"
    //So this extracts the field and reason
    val errorRegex = """\(obj.([^,]+),List\(ValidationError\(List\(([^\)]+)""".r
    val errors = errorRegex.findAllMatchIn(message) map { m ? Invalid(Field(m.group(1)), Reason(errorToMessage(m.group(2)))) }
    val list = if(errors.nonEmpty) errors.toList else List(Invalid(Field("Unknown"), Reason(message)))

    val failed = Failed(list)
    Future.successful(BadRequest(Json.toJson(failed)))
  }
} 
开发者ID:springnz,项目名称:play-app-base,代码行数:60,代码来源:ErrorHandler.scala

示例15: ErrorHandler

//设置package包名称以及导入依赖的类
import javax.inject._

import play.api._
import play.api.http.DefaultHttpErrorHandler
import play.api.mvc.Results._
import play.api.mvc._
import play.api.routing.Router

import scala.concurrent._

class ErrorHandler @Inject() (
                                 env: Environment,
                                 config: Configuration,
                                 sourceMapper: OptionalSourceMapper,
                                 router: Provider[Router]
                             ) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) {

    override def onProdServerError(request: RequestHeader, exception: UsefulException) = {
        Future.successful(
            InternalServerError("A server error occurred: :3")
        )
    }

    override def onForbidden(request: RequestHeader, message: String) = {
        Future.successful(
            Forbidden("You're not allowed to access this resource :3")
        )
    }

    override def onNotFound(request: RequestHeader, message: String) = {
        Future.successful(
            NotFound("Not found :3")
        )
    }
} 
开发者ID:CC5111,项目名称:isThereAnyOffer,代码行数:36,代码来源:ErrorHandler.scala


注:本文中的play.api.http.DefaultHttpErrorHandler类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。