6

Rails standardized error reporting interface by casperisfine · Pull Request #436...

 2 years ago
source link: https://github.com/rails/rails/pull/43625
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Copy link

Contributor

casperisfine commented 17 days ago

edited

Fix: #43472

The reporter is held by the executor, but the Rails module provides a
nicer Rails.error shortcut.

For ease of use, two block based specialized methods are exposed.

handle, which swallow errors and forward them to the subscribers:

Rails.error.handle do
  1 + '1' # raises TypeError
end
1 + 1 # This will be executed

record, which forward the errors to the subscribes but let it
continue rewinding the call stack:

Rails.error.record do
  1 + '1' # raises TypeError
end
1 + 1 # This won't be executed.

For cases where the blocked based API isn't suitable, the lower level
report method can be used:

Rails.error.report(error, handled: true / false)

Interface

In this PR I only introduce Rails.error. However in followups I'd like to introduce "local" error reporters so that gems like Active Support / Active Record can report errors as well. It would work a bit like Rails.logger today.

e.g. ActiveSupport.error would default to an error reporter that just logs the error, but in the Railtie we'd replace it with Rails.error.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK