-
Notifications
You must be signed in to change notification settings - Fork 36
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
More detailed error types #72
Comments
Yes, the database specific errors are only exposed as strings today. It would be possible to reveal them, but the user would have to link against each specific driver. So, I agree a better solution would be to translate the errors into an agnostic type. I also think it would be useful to classify the errors based on the kind of issue, the recipient of the error, and whether something can be done about it (like retry in case of timeout or connection failure). I only had a brief look this far and considering the number of errors for MariaDB and PostgreSQL it looks like a major task to classify and unite them. In comparison Sqlite3 seems to have few enumerated conditions, maybe even insufficient for what you suggest? But focusing on the former two, there is a common "SQLSTATE". It's not exposed in the PostgreSQL API, but the documentation suggests it present, so we can make a PR for that. In MariaDB the error is represented as On the side, I am a bit suspicious of using errors to detect unique constraint or other violations as a normal control flow. Though, maybe there isn't an easy alternative which works across database systems, except paying the overhead of querying up-front within the same transaction. |
If you look at SQLITE3 documentation, you can see that error codes are documented clearly with subclasses. I don’t think sqlite3 driver exposed the subclasses |
Isn't that actually the accepted way of doing this? You set a unique constraint and the database keeps it. Getting a unique violation tells you exactly what you need to know |
To outline the task:
|
My rough thoughts on the Caqti interface is to expose them as a field in |
@tcoopman It probably is. I usually try to avoid triggering errors, which has been useful in cases when I want to run a while series of updates in a transaction, since an error would abort the transaction. But that sometimes involves additional queries, within the transaction, or the use of not universally available |
I'm reconsidering my approach here. PostgreSQL provides SQLSTATE at a useful level, but MariaDB maps many errors to too generic classes, esp. to a fallback HY000, so I think we would need to redo the mapping to make it useful. Sqlite3 has no SQLSTATE support. But, creating a mapping for MariaDB and Sqlite3 turned out to be a tiring process with limited success. I pushed some limited mapping to https://github.com/paurkedal/ocaml-caqti/tree/error-sqlstate in case anyone wants to see a meagre start. My thinking now is that if we're not going for anything close to the full SQLSTATE, we may as well make something which is more ergonomic for OCaml code. So, I made about the same mapping to a variant type, https://github.com/paurkedal/ocaml-caqti/tree/error-cause. Example: (match Caqti_error.cause error with
| `Foreign_key_violation -> handle_foreign_key_violation ()
| #integrity_constraint_violation -> handle_other_constraint_violation ()
| _ -> handle_other_error ()) We don't have the extended sqlite3 error codes yet, so any constraint violation ends up on the |
I closed this before having sufficient bindings for sqlite3 to discriminate between different types of constraint violations. The refinement of the error cause variant is now also available for sqlite3 in master (ed71ade) (using |
For example, upon a
UNIQUE
constraint error, Caqti exposes aCaqti_error.t
which looks like this aftershow
:The actual value is
`Response_failed _
The
msg
field of_
could be either...Caqti_error.Msg
, which carries a string, and is therefore not useful for pattern matching. The string would have to be parsed instead.Caqti_driver_sqlite3.Rc
carrying, presumably,Sqlite3.CONSTRAINT
. This would be useful, but:Caqti_driver_sqlite3.Rc
is not exposed AFAICT.UNIQUE
constraint.Is there a way to get more detail in the error types? Since at the moment, all we can reasonably match on is
`Response_failed _
, all we essentially can know is that "some error occurred."On the subject of detail on
CONSTRAINT
, by contrast, @thangngoc89 reports in aantron/dream#86 thatsqlite3
cli gives errors likeIdeally, we would have a detailed database-agnostic error type, and translate database-specific errors in a thorough way into it.
The text was updated successfully, but these errors were encountered: