Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more user friendly message to Connection#initialize #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

helaan
Copy link

@helaan helaan commented Apr 14, 2018

This adds the cause for the exception as a message to this exception,
making it more clear why this failed.

This saves a lot of debugging time when you have an issue with your database connection. As this is the first thing new users will do with this library, it is important to give users feedback on why this failed.

rescue Errno
raise DB::ConnectionRefused.new
rescue ex : Errno
raise DB::ConnectionRefused.new(ex.message)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about setting the exception as the cause: DB::ConnectionRefused.new(cause: ex). This is what the PG driver does.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that is a nicer way in theory, it'd make the error message equal to what it is now, which would defeat the point behind this PR. Using the sample code as given in the Crystal GitBook and running it on a machine that doesn't have MySQL running, the current message is " (DB::ConnectionRefused)", while my PR changes it to "Error connecting to 'localhost:3306': Connection refused (DB::ConnectionRefused)", which in my opinion is clearer than the current message.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the next version of crystal it will print out the cause of the exception, so it depends on if the next crystal-db release will be before or after 0.25.0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is DB::ConnectionRefused.new(cause: ex, message: ex.message) an acceptable variant then? This does print the nice error message on 0.24.2

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess that's fine for now. After 0.25.0 is released, we should probably refactor DB::ConnectionRefused to have a single constructor which takes the cause, and the exception message should always be "Connection to the database was refused".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the requested change. With respect to 0.25.0, I think it is better to give as much detail as possible, even returning the name of the database that is being connected to can be helpful to expose obvious configuration errors.

This adds the cause for the exception as a message to this exception,
making it more clear why this failed.
@RX14
Copy link

RX14 commented Apr 15, 2018

To be honest, DB should probably not expose DB::ConnectionRefused at all, and just re-raise the cause exception when it catches DB::ConnectionRefused, prefixed with a message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants