I'd like to catch any RoR exceptions in REST API application with 'rescue_from'.
rescue_from StandardError do |exception|
message = Rails.env.production? ? 'API server error' : exception.message
render json: {status: "Error", message: message}, status: :internal_server_error
end
but it catches too much irrelevant exceptions. Can I catch RoR exceptions only? Is it a good practice at all? If not, what else you can recommend?
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3552a5a5-0bb7-424e-ac75-f8d4c9771198%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
begin params = { # params that you pass to a method that can throw an exception } my_unpredicable_method(params) rescue => e Airbrake.notify_or_ignore( e, parameters: params, cgi_data: ENV.to_hash ) end
begin
complex_operation_that_calls_save!_internally
rescue ActiveRecord::ReadOnlyRecord => invalid
puts invalid.record.errors
end
begin
response = Net::HTTP.post_form(...) # or any Net::HTTP call
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
...
end
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/53cd1e48-6e5e-4408-8476-6039ebaa5bc7%40googlegroups.com.