Skip to content

Commit

Permalink
Merge pull request #14 from morissetcl/fix-name-error-handler
Browse files Browse the repository at this point in the history
Fix name error handler
  • Loading branch information
morissetcl authored Jan 28, 2024
2 parents c0e2d61 + c92be12 commit b72a2be
Showing 2 changed files with 34 additions and 11 deletions.
20 changes: 16 additions & 4 deletions lib/pry-cyrano/exceptions_handler.rb
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@
class ExceptionsHandler < Base
attr_reader :exception, :output, :pry

UNINITIALIZED_CONSTANT = "uninitialized constant"
UNDEFINED_TABLE = "UndefinedTable"

def initialize(output, exception, pry)
@output = output
@exception = exception
@@ -16,20 +19,29 @@ def initialize(output, exception, pry)

def call
case exception
in NameError
in NameError => error
# eg: Usert.last
# NameError is a Superclass for all undefined statement.
# In this context We only need to one including an `uninitialized constant` error.
return pry_exception_handler unless error.message.include?(UNINITIALIZED_CONSTANT)
Exceptions::NameError.call(output, exception, pry)
in ActiveRecord::StatementInvalid => error
# eg: User.joins(:groups).where(grous: { name: "Landlord" }).last
# ActiveRecord::StatementInvalid is a Superclass for all database execution errors.
# We only need to one including an `UndefinedTable` error.
# eg: User.joins(:groups).where(grous: { name: "Landlord" }).last
return unless error.message.include?("UndefinedTable")
return pry_exception_handler unless error.message.include?(UNDEFINED_TABLE)
Exceptions::ActiveRecord::StatementInvalid.call(output, exception, pry)
in ActiveRecord::ConfigurationError
# eg: User.joins(:group).where(groups: { name: "Landlord" }).last
Exceptions::ActiveRecord::ConfigurationError.call(output, exception, pry)
else
Pry::ExceptionHandler.handle_exception(output, exception, pry)
pry_exception_handler
end
end

private

def pry_exception_handler
Pry::ExceptionHandler.handle_exception(output, exception, pry)
end
end
25 changes: 18 additions & 7 deletions spec/exceptions_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -10,17 +10,28 @@
let(:application_dictionary) { {ar_models_dictionary: ["User"], associations_dictionary: ["users", "user"]} }

context "given a NameError" do
let(:exception) { NameError.new }
context "given a ActiveRecord::StatementInvalid related to uninitialized constant" do
let(:exception) { NameError.new(ExceptionsHandler::UNINITIALIZED_CONSTANT) }

it "calls Exceptions::NameError error" do
expect(Exceptions::NameError).to receive(:call).with(output, exception, pry)
subject
it "calls Exceptions::NameError error" do
expect(Exceptions::NameError).to receive(:call).with(output, exception, pry)
subject
end
end

context "given a ActiveRecord::StatementInvalid not related to uninitialized constant" do
let(:exception) { NameError.new }

it "calls Pry::ExceptionHandler" do
expect(Pry::ExceptionHandler).to receive(:handle_exception)
subject
end
end
end

context "given a ActiveRecord::StatementInvalid error" do
context "given a ActiveRecord::StatementInvalid related to undefined table" do
let(:exception) { ActiveRecord::StatementInvalid.new("PG::UndefinedTable") }
let(:exception) { ActiveRecord::StatementInvalid.new(ExceptionsHandler::UNDEFINED_TABLE) }

it "calls Exceptions::ActiveRecord::StatementInvalid" do
expect(Exceptions::ActiveRecord::StatementInvalid).to receive(:call).with(output, exception, pry)
@@ -31,8 +42,8 @@
context "given a ActiveRecord::StatementInvalid not related to undefined table" do
let(:exception) { ActiveRecord::StatementInvalid.new }

it "does not calls Exceptions::ActiveRecord::StatementInvalid" do
expect(Exceptions::ActiveRecord::StatementInvalid).not_to receive(:call)
it "calls Pry::ExceptionHandler" do
expect(Pry::ExceptionHandler).to receive(:handle_exception)
subject
end
end

0 comments on commit b72a2be

Please sign in to comment.