Game handling errors

I am developing a REST API using a playback platform. I would like to implement centralized error handling for all my activities.

What is the best way to achieve this?

+4
source share
2 answers

You should look at GlobalSettings: https://www.playframework.com/documentation/2.3.x/ScalaGlobal

In particular, this allows you to override:

def onError(request: RequestHeader, ex: Throwable)
def onHandlerNotFound(request: RequestHeader)
def onBadRequest(request: RequestHeader, error: String)

onErrorprobably the one you are looking for, but others may be helpful :)

+5
source

An alternative way is to use filter , for example:

object ExceptionFilter extends EssentialFilter {
  def apply(nextFilter: EssentialAction) = new EssentialAction {
    def apply(requestHeader: RequestHeader) = {
      val next: Iteratee[Array[Byte], Result] = nextFilter(requestHeader)

      // Say your backend throws an ItemNotFound exception.
      next recoverWith {
        case e: ItemNotFound => Iteratee.ignore[Array[Byte]]
          .map(_ => Results.NotFound("Item not in the database!"))
      }
    }
  }
}

Then fix this in your global settings:

object Global extends WithFilters(CSRFFilter(), ExceptionFilter) with GlobalSettings

- , . GlobalSettings.onError, , .

+5

All Articles