-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error handler to return appropriate response status code
- Loading branch information
Showing
10 changed files
with
49 additions
and
133 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
package models | ||
|
||
abstract class CortexError(message: String) extends Exception(message) | ||
|
||
case class JobNotFoundError(jobId: String) extends CortexError(s"Job $jobId not found") | ||
case class UnexpectedError(message: String) extends CortexError(message) | ||
case class AnalyzerNotFoundError(analyzerId: String) extends CortexError(s"Analyzer $analyzerId not found") | ||
case class InvalidRequestError(message: String) extends CortexError(message) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
package services | ||
|
||
import models.{ AnalyzerNotFoundError, InvalidRequestError, JobNotFoundError, UnexpectedError } | ||
import play.api.Logger | ||
import play.api.http.HttpErrorHandler | ||
import play.api.mvc.{ RequestHeader, Result, Results } | ||
|
||
import scala.concurrent.Future | ||
|
||
class ErrorHandler extends HttpErrorHandler { | ||
private[ErrorHandler] lazy val logger = Logger(getClass) | ||
def onClientError(request: RequestHeader, statusCode: Int, message: String): Future[Result] = Future.successful { | ||
Results.Status(statusCode)(s"A client error occurred on ${request.method} ${request.uri} : $message") | ||
} | ||
|
||
def onServerError(request: RequestHeader, exception: Throwable): Future[Result] = { | ||
val result = exception match { | ||
case JobNotFoundError(jobId) ⇒ Results.NotFound(s"Job $jobId not found") | ||
case UnexpectedError(message) ⇒ Results.InternalServerError(message) | ||
case AnalyzerNotFoundError(analyzerId) ⇒ Results.NotFound(s"analyzer $analyzerId not found") | ||
case InvalidRequestError(message) ⇒ Results.BadRequest(message) | ||
case error ⇒ Results.InternalServerError(s"Unexpected error: $error") | ||
} | ||
Logger.info(s"${request.method} ${request.uri} returned ${result.header.status}", exception) | ||
Future.successful(result) | ||
} | ||
} |
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