-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
alerts-server: Remove error related dependencies from AbstractJsonCon…
…troller This is a part of a big refactor with the goal to allow the use of the AbstractJsonController in different applications. The changes include: - Rename PrivateError to ServerError - Split the JsonErrorRenderer into PublicErrorRenderer and ApplicationErrorMapper - The JsonControllerComponents are now required to be implemented to set the application dependant classes. - PublicError is moved to the commons package. - Base application errors are moved to the commons package. - JsonFieldValidationError is now a subclass of JsonControllerErrors. - The ApplicationError file is split into several files, one per error type. - Existing controllers now use MyJsonController. - AbstractJsonControllerSpec now uses specific errors defined locally.
- Loading branch information
Showing
32 changed files
with
297 additions
and
180 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 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
8 changes: 8 additions & 0 deletions
8
alerts-server/app/com/alexitc/coinalerts/commons/ApplicationErrorMapper.scala
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 com.alexitc.coinalerts.commons | ||
|
||
import play.api.i18n.Lang | ||
|
||
trait ApplicationErrorMapper { | ||
|
||
def toPublicErrorList(error: ApplicationError)(implicit lang: Lang): Seq[PublicError] | ||
} |
1 change: 0 additions & 1 deletion
1
alerts-server/app/com/alexitc/coinalerts/commons/FutureOr.scala
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
22 changes: 14 additions & 8 deletions
22
alerts-server/app/com/alexitc/coinalerts/commons/JsonControllerComponents.scala
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 |
---|---|---|
@@ -1,15 +1,21 @@ | ||
package com.alexitc.coinalerts.commons | ||
|
||
import javax.inject.Inject | ||
|
||
import com.alexitc.coinalerts.errors.JsonErrorRenderer | ||
import com.alexitc.coinalerts.services.JWTService | ||
import play.api.mvc.MessagesControllerComponents | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
class JsonControllerComponents @Inject() ( | ||
val messagesControllerComponents: MessagesControllerComponents, | ||
val jwtService: JWTService, | ||
val errorRenderer: JsonErrorRenderer, | ||
val executionContext: ExecutionContext) | ||
trait JsonControllerComponents { | ||
|
||
def messagesControllerComponents: MessagesControllerComponents | ||
|
||
// TODO: allow to override it | ||
def jwtService: JWTService | ||
|
||
def executionContext: ExecutionContext | ||
|
||
def publicErrorRenderer: PublicErrorRenderer | ||
|
||
def applicationErrorMapper: ApplicationErrorMapper | ||
|
||
} |
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
38 changes: 38 additions & 0 deletions
38
alerts-server/app/com/alexitc/coinalerts/commons/PublicErrorRenderer.scala
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,38 @@ | ||
package com.alexitc.coinalerts.commons | ||
|
||
import com.alexitc.coinalerts.core.ErrorId | ||
import play.api.libs.json.{JsValue, Json} | ||
|
||
class PublicErrorRenderer { | ||
def renderPublicError(publicError: PublicError): JsValue = publicError match { | ||
case e: GenericPublicError => | ||
val obj = Json.obj( | ||
"type" -> "generic-error", | ||
"message" -> e.message | ||
) | ||
Json.toJson(obj) | ||
|
||
case e: FieldValidationError => | ||
val obj = Json.obj( | ||
"type" -> "field-validation-error", | ||
"field" -> e.field, | ||
"message" -> e.message | ||
) | ||
Json.toJson(obj) | ||
|
||
case e: HeaderValidationError => | ||
val obj = Json.obj( | ||
"type" -> "header-validation-error", | ||
"header" -> e.header, | ||
"message" -> e.message | ||
) | ||
Json.toJson(obj) | ||
} | ||
|
||
def renderPrivateError(errorId: ErrorId) = { | ||
Json.obj( | ||
"type" -> "server-error", | ||
"errorId" -> errorId.string | ||
) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
alerts-server/app/com/alexitc/coinalerts/commons/applicationErrors.scala
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,12 @@ | ||
package com.alexitc.coinalerts.commons | ||
|
||
// Top-level errors | ||
trait ApplicationError | ||
trait InputValidationError extends ApplicationError | ||
trait ConflictError extends ApplicationError | ||
trait NotFoundError extends ApplicationError | ||
trait AuthenticationError extends ApplicationError | ||
trait ServerError extends ApplicationError { | ||
// contains data private to the server | ||
def cause: Throwable | ||
} |
9 changes: 9 additions & 0 deletions
9
alerts-server/app/com/alexitc/coinalerts/commons/jsonControllerErrors.scala
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,9 @@ | ||
package com.alexitc.coinalerts.commons | ||
|
||
import com.alexitc.coinalerts.core.MessageKey | ||
import play.api.libs.json.JsPath | ||
|
||
sealed trait JsonControllerErrors | ||
|
||
// play json validation errors | ||
case class JsonFieldValidationError(path: JsPath, errors: Seq[MessageKey]) extends JsonControllerErrors with InputValidationError |
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
83 changes: 0 additions & 83 deletions
83
alerts-server/app/com/alexitc/coinalerts/errors/ApplicationError.scala
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
7 changes: 7 additions & 0 deletions
7
alerts-server/app/com/alexitc/coinalerts/errors/dailyPriceAlertErrors.scala
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,7 @@ | ||
package com.alexitc.coinalerts.errors | ||
|
||
import com.alexitc.coinalerts.commons.ConflictError | ||
|
||
// Daily price alert | ||
sealed trait DailyPriceAlertError | ||
case object RepeatedDailyPriceAlertError extends DailyPriceAlertError with ConflictError |
8 changes: 8 additions & 0 deletions
8
alerts-server/app/com/alexitc/coinalerts/errors/exchangeCurrencyErrors.scala
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 com.alexitc.coinalerts.errors | ||
|
||
import com.alexitc.coinalerts.commons.{InputValidationError, NotFoundError} | ||
|
||
sealed trait ExchangeCurrencyError | ||
case object UnknownExchangeCurrencyIdError extends ExchangeCurrencyError with InputValidationError | ||
case object RepeatedExchangeCurrencyError extends ExchangeCurrencyError with InputValidationError | ||
case object ExchangeCurrencyNotFoundError extends ExchangeCurrencyError with NotFoundError |
13 changes: 13 additions & 0 deletions
13
alerts-server/app/com/alexitc/coinalerts/errors/fixedPriceAlertErrors.scala
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,13 @@ | ||
package com.alexitc.coinalerts.errors | ||
|
||
import com.alexitc.coinalerts.commons.{ConflictError, InputValidationError, NotFoundError} | ||
import com.alexitc.coinalerts.core.Count | ||
|
||
// Fixed price alert | ||
sealed trait FixedPriceAlertError | ||
case object InvalidPriceError extends FixedPriceAlertError with InputValidationError | ||
case object InvalidBasePriceError extends FixedPriceAlertError with InputValidationError | ||
case object FixedPriceAlertNotFoundError extends FixedPriceAlertError with NotFoundError | ||
case class TooManyFixedPriceAlertsError(reachedLimit: Count) extends FixedPriceAlertError with ConflictError | ||
case object InvalidFilterError extends FixedPriceAlertError with InputValidationError | ||
case object InvalidOrderError extends FixedPriceAlertError with InputValidationError |
7 changes: 7 additions & 0 deletions
7
alerts-server/app/com/alexitc/coinalerts/errors/jWTErrors.scala
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,7 @@ | ||
package com.alexitc.coinalerts.errors | ||
|
||
import com.alexitc.coinalerts.commons.AuthenticationError | ||
|
||
sealed trait JWTError | ||
case object AuthorizationHeaderRequiredError extends JWTError with AuthenticationError | ||
case object InvalidJWTError extends JWTError with AuthenticationError |
Oops, something went wrong.