Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise correct error on fail #8

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions lib/grpc_rest.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'google/protobuf/well_known_types'
require 'grpc'
require 'grpc/core/status_codes'

module GrpcRest
class << self
Expand Down Expand Up @@ -105,16 +106,60 @@ 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, might be helpful to link to grpc status codes https://grpc.github.io/grpc/core/md_doc_statuscodes.html

Also, might be good to put them in order

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)
Expand Down
8 changes: 7 additions & 1 deletion protoc-gen-rails/internal/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}" => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" => [
Expand Down
Loading