Skip to content

Kotlin implementation #16

@mfickett

Description

@mfickett

Kotlin gRPC exposes a different server endpoint API from Java. It would be useful to have an example of throwing a StatusException, if that's what the Kotlin generated code expects users to do.

With this in build.gradle:

plugins {
    id "com.google.protobuf" version "0.8.12"
}
protobuf {
    protoc { artifact = "com.google.protobuf:protoc:3.12.2" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.29.0" }
        grpckt { artifact = "io.grpc:protoc-gen-grpc-kotlin:0.1.2" }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
            grpckt {}
        }
    }
}

The generated RPC functions to override are like this:

/**                                                                                                  
 * Holder for Kotlin coroutine-based client and server APIs for                                      
 * my_pkg.MyService.                                               
 */                                                                                                  
object MyServiceGrpcKt {
    ...

    /**
     * Returns the response to an RPC for valohealth_monocle.model_search.ModelSearchService.Search.
     *
     * If this method fails with a [StatusException], the RPC will fail with the corresponding      
     * [io.grpc.Status].  If this method fails with a [java.util.concurrent.CancellationException], 
     * the RPC will fail
     * with status `Status.CANCELLED`.  If this method fails for any other reason, the RPC will     
     * fail with `Status.UNKNOWN` with the exception as a cause.
     *
     * @param request The request from the client.
     */
    open suspend fun search(request: ModelSearchRequest): ModelSearchResponse = throw
        StatusException(UNIMPLEMENTED.withDescription("Method my_pkg.MyService.Search is unimplemented"))
}

So unlike Java, there's no observer to tag errors onto, but the default implementation here seems to give an example of throwing an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions