0

If I use:

Rails.error.handle do
  my_possible_error_code
end

The possible error is swallowed and never raised.

If I use:

Rails.error.record do
  my_possible_error_code
end

The possible error is always raised.

If I use:

Rails.error.unexpected("Unexpected error") do
  my_possible_error_code
end

The error is swallowed in production but not in test/development. But the error is wrapped in a ActiveSupport::ErrorReporter::UnexpectedError and the original message is hidden, which makes the console/log output information useless.

Where is the sweet combination where?:

  • The error is swallowed in production
  • The error is raised in test/development

Or, what workaround I can do to have this behaviour?

3
  • Not sure where these snippets live, but I suspect config.consider_all_requests_local might have something to do with it. I'd set it to false in development and see if the behavior matches what you see in PROD. Commented Oct 30 at 11:41
  • Have you tried just using a conditional like if Rails.env.local? Commented Oct 30 at 17:47
  • @engineersmnky, yes, forking by env may be a solution, but I don't find any way to use this approach without generating super messy code. The whole Rails.error is about to make error handling not add noise to your code Commented Oct 31 at 11:15

1 Answer 1

1

My workaround has been to create a custom `ErrorSubscriber` :

# config/initializers/error_subscriber.rb
class ErrorSubscriber
  def report(error, handled:, severity:, context:, source: nil)
    args = { error:, handled:, severity:, context:, source: }
    Rails.logger.error("ErrorSubscriber.report: #{args.inspect}")

    raise error if Rails.env.local?
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

You can use single condition Rails.env.local? instead of Rails.env.test? || Rails.env.development?
updated, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.