Skip to content

Commit

Permalink
Merge pull request #93 from rage-rb/rescue-from-arity
Browse files Browse the repository at this point in the history
Allow rescue_from handlers to not accept arguments
  • Loading branch information
rsamoilov authored Jul 30, 2024
2 parents 5424172 + aa1571d commit 7447f4f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/rage/controller/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __register_action(action)
lines = @__rescue_handlers.map do |klasses, handler|
<<~RUBY
rescue #{klasses.join(", ")} => __e
#{handler}(__e)
#{instance_method(handler).arity == 0 ? handler : "#{handler}(__e)"}
[@__status, @__headers, @__body]
RUBY
end
Expand Down Expand Up @@ -174,18 +174,17 @@ def define_tmp_method(block)
# Register a global exception handler. Handlers are inherited and matched from bottom to top.
#
# @param klasses [Class, Array<Class>] exception classes to watch on
# @param with [Symbol] the name of a handler method. The method must take one argument, which is the raised exception. Alternatively, you can pass a block, which must also take one argument.
# @param with [Symbol] the name of a handler method. Alternatively, you can pass a block.
# @example
# rescue_from User::NotAuthorized, with: :deny_access
#
# def deny_access(exception)
# def deny_access
# head :forbidden
# end
# @example
# rescue_from User::NotAuthorized do |_|
# head :forbidden
# rescue_from User::NotAuthorized do |exception|
# render json: { message: exception.message }, status: :forbidden
# end
# @note Unlike in Rails, the handler must always take an argument. Use `_` if you don't care about the actual exception.
def rescue_from(*klasses, with: nil, &block)
unless with
if block_given?
Expand All @@ -206,7 +205,7 @@ def rescue_from(*klasses, with: nil, &block)
# Register a new `before_action` hook. Calls with the same `action_name` will overwrite the previous ones.
#
# @param action_name [String, nil] the name of the callback to add
# @param action_name [Symbol, nil] the name of the callback to add
# @param [Hash] opts action options
# @option opts [Symbol, Array<Symbol>] :only restrict the callback to run only for specific actions
# @option opts [Symbol, Array<Symbol>] :except restrict the callback to run for all actions except specified
Expand Down
40 changes: 40 additions & 0 deletions spec/controller/api/rescue_from_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ def index
raise "123"
end
end

class TestController7 < RageController::API
rescue_from StandardError, with: :error_handler

def index
raise "111"
render plain: "hi"
end

private def error_handler
render plain: "error", status: 500
end
end

class TestController8 < RageController::API
rescue_from StandardError do
render plain: "block error", status: 500
end

def index
raise "111"
render plain: "hi"
end
end
end

RSpec.describe RageController::API do
Expand Down Expand Up @@ -138,4 +162,20 @@ def index
expect(subject).to match([500, instance_of(Hash), ["block error handler"]])
end
end

context "case 7" do
let(:klass) { RescueFromSpec::TestController7 }

it "correctly handles exceptions" do
expect(subject).to match([500, instance_of(Hash), ["error"]])
end
end

context "case 8" do
let(:klass) { RescueFromSpec::TestController8 }

it "correctly handles exceptions" do
expect(subject).to match([500, instance_of(Hash), ["block error"]])
end
end
end

0 comments on commit 7447f4f

Please sign in to comment.