-
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.
Pass error details in headers instead of an error message (#47)
- Loading branch information
Showing
7 changed files
with
82 additions
and
72 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
18 changes: 18 additions & 0 deletions
18
core/src/main/scala/org/ivovk/connect_rpc_scala/grpc/GrpcHeaders.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,18 @@ | ||
package org.ivovk.connect_rpc_scala.grpc | ||
|
||
import connectrpc.ErrorDetailsAny | ||
import io.grpc.Metadata | ||
|
||
object GrpcHeaders { | ||
|
||
val ErrorDetailsKey: Metadata.Key[ErrorDetailsAny] = | ||
Metadata.Key.of("connect-error-details-bin", new Metadata.BinaryMarshaller[ErrorDetailsAny] { | ||
override def toBytes(value: ErrorDetailsAny): Array[Byte] = value.toByteArray | ||
|
||
override def parseBytes(serialized: Array[Byte]): ErrorDetailsAny = ErrorDetailsAny.parseFrom(serialized) | ||
}) | ||
|
||
inline def asciiKey(name: String): Metadata.Key[String] = | ||
Metadata.Key.of(name, Metadata.ASCII_STRING_MARSHALLER) | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
core/src/main/scala/org/ivovk/connect_rpc_scala/syntax/all.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,44 @@ | ||
package org.ivovk.connect_rpc_scala.syntax | ||
|
||
import io.grpc.{StatusException, StatusRuntimeException} | ||
import org.ivovk.connect_rpc_scala.grpc.GrpcHeaders | ||
import scalapb.GeneratedMessage | ||
|
||
object all extends RuntimeExceptionSyntax, ProtoMappingsSyntax | ||
|
||
trait RuntimeExceptionSyntax { | ||
|
||
extension (e: StatusRuntimeException) { | ||
def withDetails[T <: GeneratedMessage](t: T): StatusRuntimeException = { | ||
e.getTrailers.put(GrpcHeaders.ErrorDetailsKey, connectrpc.ErrorDetailsAny( | ||
`type` = t.companion.scalaDescriptor.fullName, | ||
value = t.toByteString | ||
)) | ||
e | ||
} | ||
} | ||
|
||
extension (e: StatusException) { | ||
def withDetails[T <: GeneratedMessage](t: T): StatusException = { | ||
e.getTrailers.put(GrpcHeaders.ErrorDetailsKey, connectrpc.ErrorDetailsAny( | ||
`type` = t.companion.scalaDescriptor.fullName, | ||
value = t.toByteString | ||
)) | ||
e | ||
} | ||
} | ||
|
||
} | ||
|
||
trait ProtoMappingsSyntax { | ||
|
||
extension [T <: GeneratedMessage](t: T) { | ||
def toProtoAny: com.google.protobuf.any.Any = { | ||
com.google.protobuf.any.Any( | ||
typeUrl = "type.googleapis.com/" + t.companion.scalaDescriptor.fullName, | ||
value = t.toByteString | ||
) | ||
} | ||
} | ||
|
||
} |