This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 4b2825dd56c55874c3d3bcafa41c62bcb02d95c8 Author: duc91 <[email protected]> AuthorDate: Tue Aug 11 10:16:33 2020 +0700 JAMES-3349: Propose a refactor for using ProblemDetails --- .../scala/org/apache/james/jmap/model/ProblemDetails.scala | 8 ++++++++ .../scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala | 11 +++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/ProblemDetails.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/ProblemDetails.scala index f617c86..4bfb65c 100644 --- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/ProblemDetails.scala +++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/ProblemDetails.scala @@ -18,6 +18,8 @@ ****************************************************************/ package org.apache.james.jmap.model +import eu.timepit.refined.auto._ +import org.apache.http.HttpStatus.SC_BAD_REQUEST import org.apache.james.jmap.model.RequestLevelErrorType.ErrorTypeIdentifier import org.apache.james.jmap.model.StatusCode.ErrorStatus @@ -27,3 +29,9 @@ import org.apache.james.jmap.model.StatusCode.ErrorStatus * see https://jmap.io/spec-core.html#errors */ case class ProblemDetails(`type`: ErrorTypeIdentifier, status: ErrorStatus, limit: Option[String], detail: String) + +object ProblemDetails { + def notRequestProblem(message: String): ProblemDetails = ProblemDetails(RequestLevelErrorType.NOT_REQUEST, SC_BAD_REQUEST, None, message) + def notJSONProblem(message: String): ProblemDetails = ProblemDetails(RequestLevelErrorType.NOT_JSON, SC_BAD_REQUEST, None, message) + def unknownCapabilityProblem(message: String): ProblemDetails = ProblemDetails(RequestLevelErrorType.UNKNOWN_CAPABILITY, SC_BAD_REQUEST, None, message) +} diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala index 66b2370..9623f74 100644 --- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala +++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala @@ -39,7 +39,8 @@ import org.apache.james.jmap.json.Serializer import org.apache.james.jmap.method.Method import org.apache.james.jmap.model.CapabilityIdentifier.CapabilityIdentifier import org.apache.james.jmap.model.Invocation.MethodName -import org.apache.james.jmap.model.{DefaultCapabilities, ErrorCode, Invocation, ProblemDetails, RequestLevelErrorType, RequestObject, ResponseObject} +import org.apache.james.jmap.model.ProblemDetails.{notJSONProblem, notRequestProblem, unknownCapabilityProblem} +import org.apache.james.jmap.model._ import org.apache.james.jmap.{Endpoint, JMAPRoute, JMAPRoutes} import org.apache.james.mailbox.MailboxSession import org.slf4j.{Logger, LoggerFactory} @@ -142,16 +143,14 @@ class JMAPApiRoutes (val authenticator: Authenticator, private def handleError(throwable: Throwable, httpServerResponse: HttpServerResponse): SMono[Void] = throwable match { case exception: IllegalArgumentException => respondDetails(httpServerResponse, - ProblemDetails(RequestLevelErrorType.NOT_REQUEST, SC_BAD_REQUEST, None, + notRequestProblem( s"The request was successfully parsed as JSON but did not match the type signature of the Request object: ${exception.getMessage}")) case exception: UnauthorizedException => SMono(handleAuthenticationFailure(httpServerResponse, JMAPApiRoutes.LOGGER, exception)) case exception: JsonParseException => respondDetails(httpServerResponse, - ProblemDetails(RequestLevelErrorType.NOT_JSON, SC_BAD_REQUEST, None, + notJSONProblem( s"The content type of the request was not application/json or the request did not parse as I-JSON: ${exception.getMessage}")) case exception: UnsupportedCapabilitiesException => respondDetails(httpServerResponse, - ProblemDetails(RequestLevelErrorType.UNKNOWN_CAPABILITY, - SC_BAD_REQUEST, None, - s"The request used unsupported capabilities: ${exception.capabilities}")) + unknownCapabilityProblem(s"The request used unsupported capabilities: ${exception.capabilities}")) case _ => SMono.fromPublisher(handleInternalError(httpServerResponse, throwable)) } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
