-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
To-om
committed
Dec 16, 2016
1 parent
e4e8e93
commit e398db7
Showing
5 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.elastic4play | ||
|
||
import scala.annotation.implicitNotFound | ||
import scala.concurrent.Future | ||
|
||
import play.api.Logger | ||
import play.api.http.HttpErrorHandler | ||
import play.api.libs.json.Json | ||
import play.api.mvc.{ RequestHeader, Result, Results } | ||
|
||
import org.elasticsearch.client.transport.NoNodeAvailableException | ||
import org.elasticsearch.index.IndexNotFoundException | ||
import org.elasticsearch.transport.RemoteTransportException | ||
|
||
import org.elastic4play.JsonFormat.attributeCheckingExceptionWrites | ||
import play.api.libs.json.JsValue | ||
import play.api.http.Status | ||
import play.api.libs.json.JsNull | ||
import play.api.mvc.ResponseHeader | ||
import play.api.http.Writeable | ||
|
||
/** | ||
* This class handles errors. It traverses all causes of exception to find known error and shows the appropriate message | ||
*/ | ||
class ErrorHandler extends HttpErrorHandler { | ||
|
||
def onClientError(request: RequestHeader, statusCode: Int, message: String) = Future.successful { | ||
Results.Status(statusCode)(s"A client error occurred on ${request.method} ${request.uri} : $message") | ||
} | ||
|
||
private def getRootCauseError(ex: Throwable): Option[(Int, JsValue)] = { | ||
ex match { | ||
case AuthenticationError(message) ⇒ Some(Status.UNAUTHORIZED → Json.obj("type" → "AuthenticationError", "error" → message)) | ||
case AuthorizationError(message) ⇒ Some(Status.FORBIDDEN → Json.obj("type" → "AuthorizationError", "error" → message)) | ||
case UpdateError(status, message, attributes) ⇒ Some(Status.INTERNAL_SERVER_ERROR → Json.obj("type" → "UpdateError", "error" → message, "obj" → attributes)) | ||
case InternalError(message) ⇒ Some(Status.INTERNAL_SERVER_ERROR → Json.obj("type" → "InternalError", "error" → message)) | ||
case nfe: NumberFormatException ⇒ Some(Status.BAD_REQUEST → Json.obj("type" → "NumberFormatException", "error" → ("Invalid format " + nfe.getMessage))) | ||
case NotFoundError(message) ⇒ Some(Status.NOT_FOUND → Json.obj("type" → "NotFoundError", "message" → message)) | ||
case BadRequestError(message) ⇒ Some(Status.BAD_REQUEST → Json.obj("type" → "BadRequest", "error" → message)) | ||
case SearchError(message, cause) ⇒ Some(Status.BAD_REQUEST → Json.obj("type" → "SearchError", "error" → s"$message (${cause.getMessage})")) | ||
case ace: AttributeCheckingError ⇒ Some(Status.BAD_REQUEST → Json.toJson(ace)) | ||
case iae: IllegalArgumentException ⇒ Some(Status.BAD_REQUEST → Json.obj("type" → "IllegalArgument", "error" → iae.getMessage)) | ||
case nnae: NoNodeAvailableException ⇒ Some(Status.INTERNAL_SERVER_ERROR → Json.obj("type" → "NoNodeAvailable", "error" → "ElasticSearch cluster is unreachable")) | ||
case CreateError(status, message, attributes) ⇒ Some(Status.INTERNAL_SERVER_ERROR → Json.obj("type" → "CreateError", "error" → message, "obj" → attributes)) | ||
case ConflictError(message, attributes) ⇒ Some(Status.BAD_REQUEST → Json.obj("type" → "ConflictError", "error" → message, "obj" → attributes)) | ||
case GetError(message) ⇒ Some(Status.INTERNAL_SERVER_ERROR → Json.obj("type" → "GetError", "error" → message)) | ||
case MultiError(message, exceptions) ⇒ | ||
val suberrors = exceptions.map(e ⇒ getRootCauseError(e)).collect { | ||
case Some((s, j)) ⇒ j | ||
} | ||
Some(Status.MULTI_STATUS → Json.obj("type" → "MultiError", "error" → message, "suberrors" → suberrors)) | ||
case rte: RemoteTransportException ⇒ | ||
rte.getCause match { | ||
case infe: IndexNotFoundException ⇒ Some(520 → JsNull) | ||
case t: Throwable ⇒ Some(Status.INTERNAL_SERVER_ERROR → Json.obj("type" → t.getClass.getName, "error" → s"Database error : ${t.getMessage}")) | ||
} | ||
case t: Throwable ⇒ Option(t.getCause).flatMap(getRootCauseError) | ||
} | ||
} | ||
|
||
def toResult[C](status: Int, c: C)(implicit writeable: Writeable[C]) = Result(header = ResponseHeader(status), body = writeable.toEntity(c)) | ||
|
||
def onServerError(request: RequestHeader, exception: Throwable) = { | ||
val (status, body) = getRootCauseError(exception).getOrElse(Status.INTERNAL_SERVER_ERROR → Json.obj("type" → exception.getClass.getName, "error" → exception.getMessage)) | ||
Logger.info(s"${request.method} ${request.uri} returned ${status}", exception) | ||
Future.successful(toResult(status, body)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# handler for errors (transform exception to related http status code | ||
play.http.errorHandler = org.elastic4play.ErrorHandler |