Rails 7.1 đã cho phép ErrorReporter để handle error classes.

Before Rails 7.1

# to handle a single error
begin
  code_that_raises_exception
rescue ArgumentError => e
  puts e.message
end   

# to handle multiple errors
exceptions = [ArgumentError, StandarError]

begin
  code_that_raises_argument_error
  code_that_raises_standard_error
rescue *exceptions => e
  puts "Error: #{e.message} occurred."
end

In Rails 7.1

@post = Post.new(params[:post])

Rails.error.handle(ActiveRecord::Errors, StandardError) do
  @post.save!
end

PR of the update

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
Read More

What’s new in Ruby 3.3.0

Table of Contents Hide YJITRJITIRBRangeRails Range#overlaps?(range)Ruby < 3.3Ruby 3.3Tham khảo Là một developer có niềm đam mê sâu…