-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Connect error codes in the Error type; cache error mappings (…
…#44)
- Loading branch information
Showing
8 changed files
with
180 additions
and
49 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
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
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
57 changes: 57 additions & 0 deletions
57
core/src/main/scala/org/ivovk/connect_rpc_scala/http/json/ConnectErrorFormat.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,57 @@ | ||
package org.ivovk.connect_rpc_scala.http.json | ||
|
||
import connectrpc.{Error, ErrorDetailsAny} | ||
import org.json4s.JsonAST.{JArray, JString, JValue} | ||
import org.json4s.MonadicJValue.* | ||
import org.json4s.{JNothing, JObject} | ||
import scalapb.json4s.{Parser, Printer} | ||
|
||
object ConnectErrorFormat { | ||
|
||
private val stringErrorCodes: Array[JString] = { | ||
val maxCode = connectrpc.Code.values.map(_.value).max | ||
val codes = new Array[JString](maxCode + 1) | ||
|
||
connectrpc.Code.values.foreach { code => | ||
codes(code.value) = JString(code.name.substring("CODE_".length).toLowerCase) | ||
} | ||
|
||
codes | ||
} | ||
|
||
val writer: (Printer, Error) => JValue = { (printer, error) => | ||
JObject(List.concat( | ||
Some("code" -> stringErrorCodes(error.code.value)), | ||
error.message.map("message" -> JString(_)), | ||
Option(error.details).filterNot(_.isEmpty).map(d => "details" -> JArray(d.map(printer.toJson).toList)), | ||
)) | ||
} | ||
|
||
val parser: (Parser, JValue) => Error = { | ||
case (parser, obj@JObject(fields)) => | ||
val code = obj \ "code" match | ||
case JString(code) => | ||
connectrpc.Code.fromName(s"CODE_${code.toUpperCase}") | ||
.getOrElse(throw new IllegalArgumentException(s"Unknown error code: $code")) | ||
case _ => throw new IllegalArgumentException(s"Error parsing Error: $obj") | ||
|
||
val message = obj \ "message" match | ||
case JString(message) => Some(message) | ||
case JNothing => None | ||
case _ => throw new IllegalArgumentException(s"Error parsing Error: $obj") | ||
|
||
val details = obj \ "details" match | ||
case JArray(details) => details.map(parser.fromJson[ErrorDetailsAny]) | ||
case JNothing => Seq.empty | ||
case _ => throw new IllegalArgumentException(s"Error parsing Error: $obj") | ||
|
||
Error( | ||
code = code, | ||
message = message, | ||
details = details, | ||
) | ||
case (_, other) => | ||
throw new IllegalArgumentException(s"Expected an object, got $other") | ||
} | ||
|
||
} |
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