diff options
author | Jakob Odersky <jakob@odersky.com> | 2017-07-14 15:25:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-14 15:25:38 -0700 |
commit | dd9e22ac2e085505e2b6a1ca476fe1dcdc7e8618 (patch) | |
tree | da106c15b9402f3f6f9f19f92fe2a2258d5cf7d3 /src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala | |
parent | 3d902b5197db861c30325c159dc10cfb211ae209 (diff) | |
parent | d733294cb16fa83be014ce1efccbf364aa309d25 (diff) | |
download | rest-query-dd9e22ac2e085505e2b6a1ca476fe1dcdc7e8618.tar.gz rest-query-dd9e22ac2e085505e2b6a1ca476fe1dcdc7e8618.tar.bz2 rest-query-dd9e22ac2e085505e2b6a1ca476fe1dcdc7e8618.zip |
Merge pull request #7 from drivergroup/restification-tcv0.2.0
Implement trial curation rest services
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala b/src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala new file mode 100644 index 0000000..6ceadb2 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala @@ -0,0 +1,89 @@ +package xyz.driver.pdsuicommon.error + +import xyz.driver.pdsuicommon.json.Serialization.seqJsonFormat +import ErrorCode.{ErrorCode, Unspecified} +import ErrorsResponse.ResponseError +import xyz.driver.pdsuicommon.auth.{AnonymousRequestContext, RequestId} +import xyz.driver.pdsuicommon.utils.Utils +import play.api.http.Writeable +import play.api.libs.functional.syntax._ +import play.api.libs.json._ +import play.api.mvc.{Result, Results} +import xyz.driver.pdsuicommon.validation.JsonValidationErrors + +class ErrorsResponse(val errors: Seq[ResponseError], private val httpStatus: Results#Status, val requestId: RequestId) { + def toResult(implicit writeable: Writeable[ErrorsResponse]): Result = httpStatus(this) +} + +object ErrorsResponse { + + /** + * @param data Any data that can be associated with particular error.Ex.: error field name + * @param message Error message + * @param code Unique error code + * + * @see https://driverinc.atlassian.net/wiki/display/RA/REST+API+Specification#RESTAPISpecification-HTTPStatuscodes + */ + final case class ResponseError(data: Option[String], message: String, code: ErrorCode) + + object ResponseError { + + implicit val responseErrorJsonFormat: Format[ResponseError] = ( + (JsPath \ "data").formatNullable[String] and + (JsPath \ "message").format[String] and + (JsPath \ "code").format[ErrorCode] + )(ResponseError.apply, unlift(ResponseError.unapply)) + + } + + implicit val writes: Writes[ErrorsResponse] = Writes { errorsResponse => + Json.obj( + "errors" -> Json.toJson(errorsResponse.errors), + "requestId" -> Json.toJson(errorsResponse.requestId.value) + ) + } + + // deprecated, will be removed in REP-436 + def fromString(message: String, httpStatus: Results#Status)( + implicit context: AnonymousRequestContext): ErrorsResponse = { + new ErrorsResponse( + errors = Seq( + ResponseError( + data = None, + message = message, + code = Unspecified + )), + httpStatus = httpStatus, + requestId = context.requestId + ) + } + + // scalastyle:off null + def fromExceptionMessage(e: Throwable, httpStatus: Results#Status = Results.InternalServerError)( + implicit context: AnonymousRequestContext): ErrorsResponse = { + val message = if (e.getMessage == null || e.getMessage.isEmpty) { + Utils.getClassSimpleName(e.getClass) + } else { + e.getMessage + } + + fromString(message, httpStatus) + } + // scalastyle:on null + + // deprecated, will be removed in REP-436 + def fromJsonValidationErrors(validationErrors: JsonValidationErrors)( + implicit context: AnonymousRequestContext): ErrorsResponse = { + val errors = validationErrors.map { + case (path, xs) => + ResponseError( + data = Some(path.toString()), + message = xs.map(_.message).mkString("\n"), + code = Unspecified + ) + } + + new ErrorsResponse(errors, Results.BadRequest, context.requestId) + } + +} |