From db22bac3775d6e13aca705d9eb6793502b84801f Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Mon, 24 Jun 2024 10:49:29 -0400 Subject: [PATCH 1/3] Raise correct error on fail --- lib/grpc_rest.rb | 48 +++++++++++++++++++++++++++-- protoc-gen-rails/internal/output.go | 8 ++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/lib/grpc_rest.rb b/lib/grpc_rest.rb index 3f46c4b..a13c705 100644 --- a/lib/grpc_rest.rb +++ b/lib/grpc_rest.rb @@ -1,5 +1,6 @@ require 'google/protobuf/well_known_types' require 'grpc' +require 'grpc/core/status_codes' module GrpcRest class << self @@ -105,16 +106,59 @@ def assign_params(request, param_hash, body_string, params) end end + def grpc_http_status(code) + case code + when GRPC::Core::StatusCodes::OK + :ok + when GRPC::Core::StatusCodes::CANCELLED + 499 + when GRPC::Core::StatusCodes::INVALID_ARGUMENT, + GRPC::Core::StatusCodes::FAILED_PRECONDITION, + GRPC::Core::StatusCodes::OUT_OF_RANGE + :bad_request + when GRPC::Core::StatusCodes::DEADLINE_EXCEEDED + :gateway_timeout + when GRPC::Core::StatusCodes::NOT_FOUND + :not_found + when GRPC::Core::StatusCodes::ALREADY_EXISTS, GRPC::Core::StatusCodes::ABORTED + :conflict + when GRPC::Core::StatusCodes::PERMISSION_DENIED + :forbidden + when GRPC::Core::StatusCodes::UNAUTHENTICATED + :unauthorized + when GRPC::Core::StatusCodes::RESOURCE_EXHAUSTED + :too_many_requests + when GRPC::Core::StatusCodes::UNIMPLEMENTED + :not_implemented + when GRPC::Core::StatusCodes::UNAVAILABLE + :service_unavailable + else + :internal_server_error + end + end + def error_msg(error) + if error.respond_to?(:code) { - code: 3, - message: "InvalidArgument: #{error.message}", + code: error.code, + message: error.message, details: [ { backtrace: error.backtrace } ] } + else + { + code: 3, + message: "InvalidArgument: #{error.message}", + details: [ + { + backtrace: error.backtrace + } + ] + } + end end def send_gruf_request(klass, service_obj, method, request) diff --git a/protoc-gen-rails/internal/output.go b/protoc-gen-rails/internal/output.go index 6be5c50..19538ba 100644 --- a/protoc-gen-rails/internal/output.go +++ b/protoc-gen-rails/internal/output.go @@ -46,8 +46,14 @@ class {{.ControllerName}}Controller < ActionController::Base protect_from_forgery with: :null_session rescue_from StandardError do |e| - render json: GrpcRest.error_msg(e) + render json: GrpcRest.error_msg(e), status: :internal_server_error end + rescue_from GRPC::BadStatus do |e| + render json: GrpcRest.error_msg(e), status: :internal_server_error + end + rescue_from Google::Protobuf::TypeError do |e| + render json: GrpcRest.error_msg(e), status: :bad_request + end METHOD_PARAM_MAP = { {{range .Methods }} "{{.Name}}" => [ From caee7be7e6e11942182ca518a0db63bebf7e8da3 Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Tue, 25 Jun 2024 11:10:52 -0400 Subject: [PATCH 2/3] fix test --- .../base/app/controllers/my_service_controller.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/protoc-gen-rails/testdata/base/app/controllers/my_service_controller.rb b/protoc-gen-rails/testdata/base/app/controllers/my_service_controller.rb index 055898c..8a88560 100644 --- a/protoc-gen-rails/testdata/base/app/controllers/my_service_controller.rb +++ b/protoc-gen-rails/testdata/base/app/controllers/my_service_controller.rb @@ -6,8 +6,14 @@ class MyServiceController < ActionController::Base protect_from_forgery with: :null_session rescue_from StandardError do |e| - render json: GrpcRest.error_msg(e) + render json: GrpcRest.error_msg(e), status: :internal_server_error end + rescue_from GRPC::BadStatus do |e| + render json: GrpcRest.error_msg(e), status: :internal_server_error + end + rescue_from Google::Protobuf::TypeError do |e| + render json: GrpcRest.error_msg(e), status: :bad_request + end METHOD_PARAM_MAP = { "test" => [ From f7834e2cda259a0e57cb1f4503e4ee530bdd8c7b Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Tue, 25 Jun 2024 13:45:15 -0400 Subject: [PATCH 3/3] Add link --- lib/grpc_rest.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/grpc_rest.rb b/lib/grpc_rest.rb index a13c705..387575a 100644 --- a/lib/grpc_rest.rb +++ b/lib/grpc_rest.rb @@ -106,6 +106,7 @@ def assign_params(request, param_hash, body_string, params) end end + # Ported from https://github.com/grpc-ecosystem/grpc-gateway/blob/main/runtime/errors.go#L36 def grpc_http_status(code) case code when GRPC::Core::StatusCodes::OK