Rails way to catch and handle exceptions

Falko

I'm looking for the best way to catch and handle exceptions in a rails app from service classes.

I have a class which calls service x, service x returns an error if the user info is wrong. At the moment we handle the error and delete previously created objects in the DB.

class Main
...
error_response = ServiceX.new(hash).run

    if error_response
      StampRequest.delete(stamp_requests)
      raise StampError, error_response[:error]
    end
...
end

The problem is that service x calls several other services, including external an external AWS and currently if these services fail or cause an exception I'm not sure of the best way or 'rails way' to handle these errors and pass them back to the main class. Currently, if for example the call to the AWS service fails, the main class has no way of knowing this.

class ServiceX
...
return error_response unless subscribed_to_price_alert_users_available?(subscribed_to_price_alert_users)

    subscribed_to_price_alert_users.each do |user|
      PricingMailer.notify(user, user_hash(user)).deliver_later
      Sms::PricingSms.new(build_hash(user)).submit
      PricingService.new(user, reminder_hash(user)).submit unless reminders?
    end

    # return nil to indicate success
    nil
  end
...
end

If the external services fail I want to then delete the StampRequests from the DB as I do if there is a user error.

Prabakaran

Passing nil for success is not the best/right way. Hope the below code snippet may help you.

class Main
  ...
  begin
    ServiceX.new(hash).run
  rescue StandardError, AWSError => exception
    ...
    StampRequest.delete(stamp_requests)
    raise StampError, exception.message
  end
end


class ServiceX
  ...
  raise StandardError.new(error_response) unless subscribed_to_price_alert_users_available?(subscribed_to_price_alert_users)

  # Assuming you have AWS/External service calls in this loop
  # Let it raise exception (AWSError), this has to be handled wisely by the caller of this function.
  # In this case caller: a fn in Main class, it has rescue block to handle this exception 
  subscribed_to_price_alert_users.each do |user|
    PricingMailer.notify(user, user_hash(user)).deliver_later
    Sms::PricingSms.new(build_hash(user)).submit
    PricingService.new(user, reminder_hash(user)).submit unless reminders?
  end
  ...
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Completable futures. What's the best way to handle business "exceptions"?

Preferred way to handle Java exceptions in ServletContextListener

Using try-catch to handle input exceptions in Java

How to handle all the java.net exceptions in a single catch block?

Correct way to handle exceptions in Spring Boot

If the catch throws exceptions, how should I handle them?

Pythonic way to catch all exceptions from a module?

What is preferable way to handle exceptions?

Correct way to handle exceptions in Java

What is the correct way to catch all exceptions that is unhandled in service fabric ReceiveReminderAsync

Is there a way to handle exceptions automatically with Python Click?

Python proper way to catch exceptions on file close

Catch multiple exceptions in Clojure and handle them the same

Catch and handle (differently) different exceptions for the same block, with an ensure

Catch and handle PayPal SDK HTTP Exceptions PHP yii2

Is there a better way to handle exceptions ? try-catch block is really ugly

What is the best way to handle exceptions in this scenario

What is the proper way to load images in Flutter and handle exceptions on load

What is the cleaner way to handle exceptions

Recommended way to handle exceptions?

Proper way to handle exceptions in Ruby on Rails

Best way to handle exceptions that can not occur

Is there a way to globally catch unhandled promise exceptions in reactjs?

Marshmallow serialization - a way to catch exceptions on a per field basis?

Best way to throw multiple exceptions from a catch block

Python: Correct way to handle chain exceptions

Is there a way to handle exeption without try catch block?

How to globally handle/catch exceptions in Blazor.Server C#

How to handle Python exceptions in a decent way?