How to catch base exception class in Ruby on Rails?

I’d like to catch any RoR exceptions in REST API application with

rescue_from StandardError do |exception|
message = Rails.env.production? ? ‘API server error’ :
render json: {status: “Error”, message: message}, status:

but it catches too much irrelevant exceptions. Can I catch RoR
only? Is it a good practice at all? If not, what else you can recommend?

This is actually a very bad practice, you are actually creating a black
hole that will swallow many relevant problems and will be super hard to
debug and maintain in the future. What you want to do is only catch
that you can handle, do not catch everything, you actually have to let
raise and have 2 things: 1) generic message for the users and 2) errors
notification system that will send technical details to you and your
have a look at GitHub - airbrake/airbrake: The official Airbrake library for Ruby applications and get
about it. If you want more help, please be more specific about what you
want to achieve.

A communication with API server made with JSON.
In case of application error instead of crashing with status 500 with
response I’d like that API clients will receive response with generic
{status: “Error”, message: “API server error”}
or may be something more specific in some cases.

Of course, any application error should be reported to developer via
Airbrake or similar gem:

Just for sake of clarity here is excerpt of that Airbrake suggests:

params = {
# params that you pass to a method that can throw an exception
my_unpredicable_method(params)rescue => e
parameters: params,
cgi_data: ENV.to_hash

In code above they assume that application catches every descendant of
StandardError. Is that good or bad practice?

In this case, if you are using Airbrake you can let it catch all,
the purpose of exceptions it to notify and inform the developers,
the program would just crash and stop running without providing any
information of what happen. The problem with the previous code block you
had was that it was returning json: {status: "Error", message: message}
to everything and not notifying the developers with the technical
information of the error, that is a bad practice.

In any case if you are setting your content headers correctly in the
request the server will not respond with an HTML page it will return
code 500 for the AJAX request without you needing to do anything, and if
you are using GitHub - airbrake/airbrake: The official Airbrake library for Ruby applications it is already
with rails so you dont need to set up that code block and you dont need
setup the environment condition like that, Airbrake.configure can have
ignore_environments = %w(development test) . I think your problem might
more related to how you are setting up your ajax request.

Now I think that you should still be more specific, for example, if you
performing a save operation in the my_unpredicable_method(params) you
want to do

ActiveRecord::ReadOnlyRecord => invalid
puts invalid.record.errorsend

if its connection errors then have a look at

response = Net::HTTP.post_form(…) # or any Net::HTTP callrescue
Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
Net::ProtocolError => e

Is even to that, for your own procedures you create your own exception

class InvalidModifyType < StandardError; end

The more specific you are the more control you will have, the easier it
will be to maintain and debug.