Skip to content

Commit

Permalink
Add support for rescue_from
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-thatch committed Jun 25, 2024
1 parent 3504dad commit 01d8e3a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
28 changes: 27 additions & 1 deletion lib/tapioca/dsl/compilers/grape_endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ class GrapeEndpoints < Tapioca::Dsl::Compiler
sig { override.void }
def decorate
create_classes_and_includes
create_routing_methods

create_callbacks_methods
create_request_response_methods
create_routing_methods
end

class << self
Expand Down Expand Up @@ -108,6 +110,14 @@ def callbacks_methods_module
)
end

sig { returns(RBI::Scope) }
def request_response_methods_module
@request_response_methods_module ||= T.let(
api.create_module(RequestResponseMethodsModuleName),
T.nilable(RBI::Scope),
)
end

sig { returns(RBI::Scope) }
def routing_methods_module
@routing_methods_module ||= T.let(
Expand All @@ -119,6 +129,7 @@ def routing_methods_module
sig { void }
def create_classes_and_includes
api.create_extend(CallbacksMethodsModuleName)
api.create_extend(RequestResponseMethodsModuleName)
api.create_extend(RoutingMethodsModuleName)
create_api_class
create_endpoint_class
Expand Down Expand Up @@ -159,6 +170,21 @@ def create_callbacks_methods
end
end

sig { void }
def create_request_response_methods
request_response_methods_module.create_method(
"rescue_from",
parameters: [
create_rest_param("args", type: "T.untyped"),
create_block_param(
"block",
type: "T.nilable(T.proc.bind(#{EndpointClassName}).params(e: Exception).void)",
),
],
return_type: "void",
)
end

sig { void }
def create_routing_methods
HTTP_VERB_METHODS.each do |verb|
Expand Down
1 change: 1 addition & 0 deletions lib/tapioca/dsl/helpers/grape_constants_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module GrapeConstantsHelper
extend T::Sig

CallbacksMethodsModuleName = "GeneratedCallbacksMethods"
RequestResponseMethodsModuleName = "GeneratedRequestResponseMethods"
RoutingMethodsModuleName = "GeneratedRoutingMethods"

APIInstanceClassName = "PrivateAPIInstance"
Expand Down
2 changes: 1 addition & 1 deletion rbi/grape.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Grape

module RequestResponse
module ClassMethods
sig { params(args: T.untyped, block: T.proc.bind(Grape::Endpoint).void).void }
sig { params(args: T.untyped, block: T.proc.bind(Grape::Endpoint).params(e: Exception).void).void }
def rescue_from(*args, &block); end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/tapioca/dsl/compilers/grape_endpoints_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def authenticate!
class TwitterAPI
extend GeneratedCallbacksMethods
extend GeneratedRequestResponseMethods
extend GeneratedRoutingMethods
module GeneratedCallbacksMethods
Expand All @@ -83,6 +84,11 @@ def before_validation(&block); end
def finally(&block); end
end
module GeneratedRequestResponseMethods
sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: Exception).void)).void }
def rescue_from(*args, &block); end
end
module GeneratedRoutingMethods
sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
def delete(*args, &block); end
Expand Down Expand Up @@ -150,6 +156,7 @@ class TwitterAPI < Grape::API::Instance
class TwitterAPI
extend GeneratedCallbacksMethods
extend GeneratedRequestResponseMethods
extend GeneratedRoutingMethods
module GeneratedCallbacksMethods
Expand All @@ -169,6 +176,11 @@ def before_validation(&block); end
def finally(&block); end
end
module GeneratedRequestResponseMethods
sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: Exception).void)).void }
def rescue_from(*args, &block); end
end
module GeneratedRoutingMethods
sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
def delete(*args, &block); end
Expand Down

0 comments on commit 01d8e3a

Please sign in to comment.