crystal-mysql icon indicating copy to clipboard operation
crystal-mysql copied to clipboard

Generic `Exception`s raised in `MySql::Connection`

Open brodyhoskins opened this issue 1 year ago • 6 comments

Working with this shard there are specific exceptions I'd like to rescue from, but it looks like for the most part Mysql::Connection raises generic Exceptions.

I'd like to throw out the idea of using more specific exception types and see what the maintainer and/or community thinks. I'm also willing to open a PR for the change if anyone's interested.

brodyhoskins avatar Aug 21 '23 19:08 brodyhoskins

Adding specific exceptions sounds good. Feel free to send a PR or explain what use cases you have in mind.

bcardiff avatar Aug 21 '23 19:08 bcardiff

The context is:

I'm utilizing this shard within another small shard (run as an executable) that connects to the database every so often (long story) within Kubernetes. Due to the raised database connection error my shard crashes, the container crashes, then the entire pod crashes.

My goal is to handle specific exceptions so as to keep the loop happy and not bring down an entire Kubernetes pod. 😅

I'll submit a PR later on.

brodyhoskins avatar Aug 21 '23 20:08 brodyhoskins

If you use a connection pool instead of a direct connection you should be resilient to connection errors. I'm not sure how you are setting up/executing queries

bcardiff avatar Aug 21 '23 21:08 bcardiff

It's mostly related to credentials which have expired. Instead of crashing due to the exception it would be better for us to handle it specifically — to resolve it involves work outside of the container in question; it would eventually be killed off.

I just don't want to rescue every exception because if something more serious is happening it would be better to deal with a standard noisy crash than skipping it on by.

brodyhoskins avatar Aug 21 '23 22:08 brodyhoskins

If credentials are wrong I would expect you to get a DB::ConnectionRefused. Isn't that the case?

bcardiff avatar Aug 21 '23 23:08 bcardiff

Now that I think of it, I have seen DB::ConnectionRefused, but going back the exception encountered was:

Unhandled exception: Access denied for user '[REDACTED]'@'[REDACTED]' (using password: YES) (Exception)
  from app/lib/mysql/src/mysql/connection.cr:102:5 in 'handle_err_packet'
  from app/lib/mysql/src/mysql/connection.cr:116:7 in 'initialize'
  from app/lib/mysql/src/mysql/connection.cr:4:3 in 'new'
  from app/lib/mysql/src/mysql/driver.cr:3:5 in 'build_connection'
  from app/lib/db/src/db/database.cr:57:9 in '->'
  from app/lib/db/src/db/pool.cr:218:27 in 'build_resource'
  from app/lib/db/src/db/pool.cr:47:34 in 'initialize'
  from app/lib/db/src/db/pool.cr:40:5 in 'new:initial_pool_size:max_pool_size:max_idle_pool_size:checkout_timeout:retry_attempts:retry_delay'
  from app/lib/db/src/db/database.cr:56:15 in 'initialize'
  from app/lib/db/src/db/database.cr:49:5 in 'new'
  from app/lib/db/src/db.cr:155:5 in 'build_database'
  from app/lib/db/src/db.cr:151:5 in 'build_database'
  from app/lib/db/src/db.cr:124:5 in 'call'
  from app/src/leash.cr:54:3 in '__crystal_main'
  from usr/share/crystal/src/crystal/main.cr:115:5 in 'main_user_code'
  from usr/share/crystal/src/crystal/main.cr:101:7 in 'main'
  from usr/share/crystal/src/crystal/main.cr:127:3 in 'main'
  from /lib/ld-musl-x86_64.so.1 in '??'

brodyhoskins avatar Aug 22 '23 15:08 brodyhoskins