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 b66d04031cfd8dc8b6d14678d6b36fb46a66eed9 Author: duc91 <[email protected]> AuthorDate: Mon Aug 10 17:48:54 2020 +0700 JAMES-3349: add handle methodError --- .../org/apache/james/jmap/json/Serializer.scala | 16 ++++++------- .../james/jmap/method/MailboxGetMethod.scala | 1 - .../org/apache/james/jmap/model/Invocation.scala | 27 ++++++++++++++++++++-- .../apache/james/jmap/routes/JMAPApiRoutes.scala | 12 ++++------ .../james/jmap/routes/JMAPApiRoutesTest.scala | 2 +- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/Serializer.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/Serializer.scala index 1e3f5f5..09484a4 100644 --- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/Serializer.scala +++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/Serializer.scala @@ -106,14 +106,14 @@ class Serializer @Inject() (mailboxIdFactory: MailboxId.Factory) { (set: Set[_ <: Capability]) => { set.foldLeft(JsObject.empty)((jsObject, capability) => { capability match { - case capability: CoreCapability => ( - jsObject.+(capability.identifier.value, corePropertiesWriter.writes(capability.properties))) - case capability: MailCapability => ( - jsObject.+(capability.identifier.value, mailCapabilityWrites.writes(capability.properties))) - case capability: QuotaCapability => ( - jsObject.+(capability.identifier.value, quotaCapabilityWrites.writes(capability.properties))) - case capability: SharesCapability => ( - jsObject.+(capability.identifier.value, sharesCapabilityWrites.writes(capability.properties))) + case capability: CoreCapability => + jsObject.+(capability.identifier.value, corePropertiesWriter.writes(capability.properties)) + case capability: MailCapability => + jsObject.+(capability.identifier.value, mailCapabilityWrites.writes(capability.properties)) + case capability: QuotaCapability => + jsObject.+(capability.identifier.value, quotaCapabilityWrites.writes(capability.properties)) + case capability: SharesCapability => + jsObject.+(capability.identifier.value, sharesCapabilityWrites.writes(capability.properties)) case _ => jsObject } }) diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxGetMethod.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxGetMethod.scala index a310240..91b0b46 100644 --- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxGetMethod.scala +++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxGetMethod.scala @@ -74,7 +74,6 @@ class MailboxGetMethod @Inject() (serializer: Serializer, methodName = methodName, arguments = Arguments(serializer.serialize(mailboxGetResponse, mailboxGetRequest.properties, capabilities).as[JsObject]), methodCallId = invocation.methodCallId)) - } } )) diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/Invocation.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/Invocation.scala index 5e8ec02..7e99723 100644 --- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/Invocation.scala +++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/Invocation.scala @@ -37,7 +37,12 @@ object Invocation { def error(errorCode: ErrorCode, description: Option[String], methodCallId: MethodCallId): Invocation = { Invocation(MethodName("error"), - Arguments(JsObject(Seq("type" -> JsString(errorCode.code), "description" -> JsString(description.getOrElse(errorCode.defaultDescription))))), + Arguments(JsObject(Map("type" -> JsString(errorCode.code), "description" -> JsString(description.getOrElse(errorCode.defaultDescription))))), + methodCallId) + } + def error(errorCode: ErrorCode, methodCallId: MethodCallId): Invocation = { + Invocation(MethodName("error"), + Arguments(JsObject(Map("type" -> JsString(errorCode.code)))), methodCallId) } } @@ -49,8 +54,26 @@ sealed trait ErrorCode { object ErrorCode { case object InvalidArguments extends ErrorCode { - override def code: String = "invalidArguments" + override def code: String = "error" override def defaultDescription: String = "One of the arguments is of the wrong type or otherwise invalid, or a required argument is missing." } + + case object ServerFail extends ErrorCode { + override def code: String = "serverFail" + + override def defaultDescription: String = null + } + + case object UnknownMethod extends ErrorCode { + override def code: String = "unknownMethod" + + override def defaultDescription: String = null + } + + case object AccountNotFound extends ErrorCode { + override def code: String = "accountNotFound" + + override def defaultDescription: String = null + } } 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 e39d96d..66b2370 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 @@ -38,12 +38,12 @@ import org.apache.james.jmap.http.{Authenticator, MailboxesProvisioner, UserProv 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.{Arguments, MethodName} -import org.apache.james.jmap.model._ +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.{Endpoint, JMAPRoute, JMAPRoutes} import org.apache.james.mailbox.MailboxSession import org.slf4j.{Logger, LoggerFactory} -import play.api.libs.json.{JsError, JsSuccess, Json} +import play.api.libs.json.{JsError, JsSuccess} import reactor.core.publisher.Mono import reactor.core.scala.publisher.{SFlux, SMono} import reactor.core.scheduler.Schedulers @@ -137,10 +137,8 @@ class JMAPApiRoutes (val authenticator: Authenticator, private def processMethodWithMatchName(capabilities: Set[CapabilityIdentifier], invocation: Invocation, mailboxSession: MailboxSession): SMono[Invocation] = SMono.justOrEmpty(methodsByName.get(invocation.methodName)) .flatMap(method => SMono.fromPublisher(method.process(capabilities, invocation, mailboxSession))) - .switchIfEmpty(SMono.just(new Invocation( - MethodName("error"), - Arguments(Json.obj("type" -> "Not implemented")), - invocation.methodCallId))) + .onErrorResume(throwable => SMono.just(Invocation.error(ErrorCode.ServerFail, Option(throwable.getMessage), invocation.methodCallId))) + .switchIfEmpty(SMono.just(Invocation.error(ErrorCode.UnknownMethod, invocation.methodCallId))) private def handleError(throwable: Throwable, httpServerResponse: HttpServerResponse): SMono[Void] = throwable match { case exception: IllegalArgumentException => respondDetails(httpServerResponse, diff --git a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala index ccec3b8..3cf728b 100644 --- a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala +++ b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala @@ -151,7 +151,7 @@ object JMAPApiRoutesTest { | [ | "error", | { - | "type": "Not implemented" + | "type": "unknownMethod" | }, | "notsupport" | ] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
