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

Log message when the gem has an error #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ Style/ExpandPathArguments:
# SupportedStyles: implicit, explicit
Style/RescueStandardError:
Exclude:
- 'lib/api_error_handler.rb'
- 'spec/api_error_handler/serializers/json_api_spec.rb'
- 'spec/api_error_handler/serializers/json_spec.rb'
- 'spec/api_error_handler/serializers/xml_spec.rb'
Expand Down
13 changes: 12 additions & 1 deletion lib/api_error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ def handle_api_errors(options = {})
content_type: content_type,
status: status
)
rescue
rescue StandardError => e
if defined?(Rails)
Rails.logger.error(
<<~MESSAGE
Error from inside api_error_handler `rescue_from` block. If you believe this to be a bug, please report this on the gem's GitHub page.
Exception: #{e}
Backtrace:
#{e.backtrace.join("\n")}
MESSAGE
)
end

raise error
end
end
Expand Down
3 changes: 2 additions & 1 deletion rails_5_test_app/spec/controllers/tests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
described_class.send(:handle_api_errors)
allow_any_instance_of(TestsController)
.to receive(:render)
.and_raise("This is not the error that should be raised")
.and_raise(StandardError, "This is not the error that should be raised")

expect(Rails.logger).to receive(:error)
expect { get :runtime_error }.to raise_error(RuntimeError, "This is a RuntimeError!")
end

Expand Down