2 changed files with 45 additions and 0 deletions
@ -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) |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue