0

I am working on a rails 5 API that has SQLite3 database. Even though the database file is stored locally, I want to create my own exception handling in case the database file is not accessible, e.g. deleted. I actually want all such errors, that are caused by DB connectivity to create my own custom response and display it to the user, in text,json etc.

How can I achieve this? At the moment when I delete the database I get the error message about "Pending migration" or If I change the database filename to something else I get the error "Can't open database file". For all similar scenarios related to database I want to handle the error my own way.

Any ideas?

Thanks

Hussain Niazi
  • 579
  • 1
  • 3
  • 21
  • 4
    Number one idea - don't use SQLite. Its generally not up to the task of actually running production sites and doesn't work with SASS platforms with ephemeral file systems such as Heroku. Its also very light on features compared to for example Postgres. – max Aug 07 '21 at 18:54
  • Yes i changed to MySQL being hosted on a different server. However the problem remains the same :/ – Hussain Niazi Aug 08 '21 at 22:12

1 Answers1

0

rescue_from

https://apidock.com/rails/ActiveSupport/Rescuable/ClassMethods/rescue_from

You'll need to find the name of the exception(s) thrown.

Here's an example:

class ApplicationController < ActionController::Base
  rescue_from ActiveRecord::RecordInvalid, with: :show_errors

  private
    def show_errors(exception)
      render ...
    end
end
B Seven
  • 44,484
  • 66
  • 240
  • 385