Lucas Ocon
Lucas Ocon

Reputation: 199

How can I stop Rails execution from initializer?

I want to stop the app start up from an initializer. Something like if a config isn't present, stop server/console, etc. Also send a message in order to explain the error.

Is there a way to do that?

I looked into initialization events but I cannot make it happen.

Thanks in advance.

Upvotes: 3

Views: 1112

Answers (2)

Babar Al-Amin
Babar Al-Amin

Reputation: 3984

You can use the Kernel#abort method to do it. It'll stop the application with your provided message and won't throw up any error.

Example:

abort('You need to pass more info to start the application') if some_check_fails?

Upvotes: 2

Jimmy Baker
Jimmy Baker

Reputation: 3255

Yeah, just raise an exception like you normally would:

raise StandardError, "Stopping app start up because something is missing"

If you're doing this because some config is missing, consider using something like Figaro which does this for you.

Figaro.require_keys("pusher_app_id", "pusher_key", "pusher_secret")

https://github.com/laserlemon/figaro

Upvotes: 3

Related Questions