Skip to content

Commit a3539ed

Browse files
committed
call stream_grpcweb_trailers from send_error_trailers
This *may* require that the status was already sent, as well as the state passed to `send_error_trailers` to decided whether or not to send grpcweb trailers. And so: * `send_error_trailers` now uses `check_sent_resp` instead of doing that check itself * `check_sent_resp` now takes an optional `status`, with 200 as the default * state is passed to `send_error_trailers` * `send_error_trailers` calls `stream_grpcweb_trailers` before `cowboy_req.stream_trailers` (which closes the connection as trailers implies fin)
1 parent 987365d commit a3539ed

File tree

1 file changed

+10
-10
lines changed
  • grpc_server/lib/grpc/server/adapters/cowboy

1 file changed

+10
-10
lines changed

grpc_server/lib/grpc/server/adapters/cowboy/handler.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
112112
{:error, error} ->
113113
Logger.error(fn -> inspect(error) end)
114114
trailers = HTTP2.server_trailers(error.status, error.message)
115-
req = send_error_trailers(req, 200, trailers)
115+
req = send_error_trailers(req, 200, trailers, state)
116116
{:ok, req, state}
117117
end
118118
end
@@ -626,22 +626,22 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
626626
end
627627
end
628628

629-
defp check_sent_resp(%{has_sent_resp: _} = req) do
629+
defp check_sent_resp(req, status \\ 200)
630+
631+
defp check_sent_resp(%{has_sent_resp: _} = req, status) do
630632
req
631633
end
632634

633-
defp check_sent_resp(req) do
634-
:cowboy_req.stream_reply(200, req)
635+
defp check_sent_resp(req, status) do
636+
:cowboy_req.stream_reply(status, req)
635637
end
636638

637-
defp send_error_trailers(%{has_sent_resp: _} = req, _, trailers) do
639+
defp send_error_trailers(req, status, trailers, state) do
640+
req = check_sent_resp(req, status)
641+
stream_grpcweb_trailers(req, trailers, state)
638642
:cowboy_req.stream_trailers(trailers, req)
639643
end
640644

641-
defp send_error_trailers(req, status, trailers) do
642-
:cowboy_req.reply(status, trailers, req)
643-
end
644-
645645
def exit_handler(pid, reason) do
646646
if Process.alive?(pid) do
647647
Process.exit(pid, reason)
@@ -712,7 +712,7 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
712712
exit_handler(pid, reason)
713713
end
714714

715-
send_error_trailers(req, status, trailers)
715+
send_error_trailers(req, status, trailers, state)
716716
end
717717

718718
# Similar with cowboy's read_body, but we need to receive the message

0 commit comments

Comments
 (0)