sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

feat: better database errors

Open saiintbrisson opened this issue 3 years ago • 0 comments

In light of problems I faced when error handling a back-end agnostic code, I've came up with an interface that lets the user figure out what kind of error the database returned, which fixes #2102.

The new DatabaseError::kind(&self) function tries to match the error code with a known kind, taking into account the back-end specific ways it might be returned. Four error kinds are supported for now: unique, foreign key, not null, and check violations.

SQLite and Postgres are pretty straight forward, MySQL has multiple codes for each kind of violation, but MSSQL has a funny way of reporting checks and foreign key violations, it uses the same error code and differentiates the two in the message.

I've performed tests and all four kinds are supported for all back-ends for now, including check and foreign key for MSSQL.

saiintbrisson avatar Sep 21 '22 15:09 saiintbrisson