Repository: elixir-grpc/grpc (elixir)

Issue:
Passing a nil channel to GRPC.Stub function errors on compressor
**Describe the bug**
If you pass nil to a GRPC.Stub function rather than a channel as expected, it will fail on `lib/grpc/stub:251: key :compressor not found in: nil`

**To Reproduce**
Steps to reproduce the behavior:
Call any RPC stub function with a nil channel as the first argument

**Expected behavior**
It should give a nice error message or a match error for the appropriate type, rather than an inadvertent error



Pull request description:
Pattern match on channel on stub functions
Provides a better error for invalid argument. Closes #353 

Patch:
diff --git a/lib/grpc/stub.ex b/lib/grpc/stub.ex
index 659ceaf..b96ad51 100644
--- a/lib/grpc/stub.ex
+++ b/lib/grpc/stub.ex
@@ -78,7 +78,7 @@ defmodule GRPC.Stub do
         }
 
         if req_stream do
-          def unquote(String.to_atom(func_name))(channel, opts \\ []) do
+          def unquote(String.to_atom(func_name))(%GRPC.Channel{} = channel, opts \\ []) do
             GRPC.Stub.call(
               unquote(service_mod),
               unquote(Macro.escape(rpc)),
@@ -88,7 +88,7 @@ defmodule GRPC.Stub do
             )
           end
         else
-          def unquote(String.to_atom(func_name))(channel, request, opts \\ []) do
+          def unquote(String.to_atom(func_name))(%GRPC.Channel{} = channel, request, opts \\ []) do
             GRPC.Stub.call(
               unquote(service_mod),
               unquote(Macro.escape(rpc)),


Test patch:
diff --git a/test/grpc/integration/stub_test.exs b/test/grpc/integration/stub_test.exs
index b497003..d24cdc6 100644
--- a/test/grpc/integration/stub_test.exs
+++ b/test/grpc/integration/stub_test.exs
@@ -68,6 +68,14 @@ defmodule GRPC.Integration.StubTest do
     end)
   end
 
+  test "invalid channel function clause error" do
+    req = Helloworld.HelloRequest.new(name: "GRPC")
+
+    assert_raise FunctionClauseError, ~r/Helloworld.Greeter.Stub.say_hello/, fn ->
+      Helloworld.Greeter.Stub.say_hello(nil, req)
+    end
+  end
+
   test "returns error when timeout" do
     run_server(SlowServer, fn port ->
       {:ok, channel} = GRPC.Stub.connect("localhost:#{port}")