return Either[String, JsonRpcMessage]

This commit is contained in:
Eugene Yokota 2017-11-29 01:00:46 -05:00
parent e4dd090d0c
commit 6a996378c7
2 changed files with 9 additions and 6 deletions

View File

@ -18,6 +18,7 @@ import sbt.protocol._
import sbt.internal.langserver.ErrorCodes
import sbt.internal.util.{ ObjectEvent, StringEvent }
import sbt.internal.util.codec.JValueFormats
import sbt.internal.protocol.{ JsonRpcRequestMessage, JsonRpcNotificationMessage }
import sbt.util.Logger
final class NetworkChannel(val name: String,
@ -167,7 +168,7 @@ final class NetworkChannel(val name: String,
def handleBody(chunk: Vector[Byte]): Unit = {
if (isLanguageServerProtocol) {
Serialization.deserializeJsonMessage(chunk) match {
case Right(Right(req)) =>
case Right(req: JsonRpcRequestMessage) =>
try {
onRequestMessage(req)
} catch {
@ -175,7 +176,7 @@ final class NetworkChannel(val name: String,
log.debug(s"sending error: $code: $message")
langError(Option(req.id), code, message)
}
case Right(Left(ntf)) =>
case Right(ntf: JsonRpcNotificationMessage) =>
try {
onNotification(ntf)
} catch {
@ -183,6 +184,8 @@ final class NetworkChannel(val name: String,
log.debug(s"sending error: $code: $message")
langError(None, code, message) // new id?
}
case Right(msg) =>
log.debug(s"Unhandled message: $msg")
case Left(errorDesc) =>
val msg = s"Got invalid chunk from client (${new String(chunk.toArray, "UTF-8")}): " + errorDesc
langError(None, ErrorCodes.ParseError, msg)

View File

@ -15,6 +15,7 @@ import java.nio.ByteBuffer
import scala.util.{ Success, Failure }
import sbt.internal.util.StringEvent
import sbt.internal.protocol.{
JsonRpcMessage,
JsonRpcRequestMessage,
JsonRpcResponseMessage,
JsonRpcNotificationMessage
@ -143,18 +144,17 @@ object Serialization {
}
}
private[sbt] def deserializeJsonMessage(bytes: Seq[Byte])
: Either[String, Either[JsonRpcNotificationMessage, JsonRpcRequestMessage]] = {
private[sbt] def deserializeJsonMessage(bytes: Seq[Byte]): Either[String, JsonRpcMessage] = {
val buffer = ByteBuffer.wrap(bytes.toArray)
Parser.parseFromByteBuffer(buffer) match {
case Success(json) =>
import sbt.internal.protocol.codec.JsonRPCProtocol._
Converter.fromJson[JsonRpcRequestMessage](json) match {
case Success(request) if (request.id.nonEmpty) => Right(Right(request))
case Success(request) if (request.id.nonEmpty) => Right(request)
case Failure(e) => throw e
case _ => {
Converter.fromJson[JsonRpcNotificationMessage](json) match {
case Success(notification) => Right(Left(notification))
case Success(notification) => Right(notification)
case Failure(e) => throw e
}
}