-
-
Notifications
You must be signed in to change notification settings - Fork 129
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
Database errors need to be more matchable [needs upstream rework] #86
Comments
(Also leaving myself a note that it might be good to create an actual example in this repo out of the program, or at least paste the code for catching errors into the existing SQL example). |
I think this needs to be solved upstream. The output error is kinda bad.
This doesn't give you any context about what fails. There could be many constraint in a complex query.
|
Opened paurkedal/ocaml-caqti#72 about this in Caqti. |
It looks like Caqti 1.8.0 has some support for this. I haven't had a chance to try it yet. |
@thangngoc89 The Dream program at the bottom of this message shows the closest I was able to get to actually matching on a precise SQL error (a
UNIQUE
constraint violation) using Caqti. The relevant snippet is:This program at least confirms that it caught the right exception. In the log output, there is
It doesn't appear that you can get any more precise error pattern than in this at the moment:
`Response_failed
has aCaqti_error.query_error
as a payload. Theuri
andquery
fields are not interesting here. They are just the DB connection string and the query we sent, of course. That leaves themsg
field.msg
field is an extensible variant, with only one constructor defined by the base Caqti, which just contains an opaque string.Caqti_driver_sqlite3
defines an additional constructorCaqti_driver_sqlite3.Rc
which carries an ocaml-sqlite3 return code, which at least has aSqlite3.CONSTRAINT
variand. However,Rc
is not exposed in the interface ofCaqti_driver_sqlite3
, so, to my knowledge, there is currently no way to match on it in a pattern.So, in summary, it seems that one has to, at the moment, convert the error to a string and parse the string to get any more details.
There is another problem, that even if we did match for
Sqlite3.CONSTRAINT
, the code would not be portable to other DB drivers, so this would not be a satisfactory solution anyway.We really need some kind of interpretation of driver errors at the Caqti level. Also, it should all be nicely documented, so that most users don't have to read
.mli
s like I just did :)I'm going to do a bit more digging and report this upstream to Caqti, to see if this is something that was considered and just not addressed yet. I'll post a link to that issue once it exists.
The text was updated successfully, but these errors were encountered: