Caqti and unique violation error

I have a function that returns ('a, Caqti_error.t) result and I’m trying to catch unique violation error

  | Ok _ -> ....
  | Error err -> (
      match err with
      | `Response_failed err -> (
          let cause = `Response_failed err |> Caqti_error.cause in
          match cause with
          | `Integrity_constraint_violation__don't_match ->
              print_string "intercepted!"
          | _ -> ())
      | _ -> ())

After launching I see in the console “intercepted!”, Am I doing the type narrowing correctly? Why is the uniqueness error in Response_failed and not in Request_failed? Or is this a feature of the sqlite3 driver?

The detailed error causes are only supported for sqlite3.5.2.0 or later, since earlier versions of the bindings don’t support the extended errors.

I opened an issue about the error classification, thanks!

I can see it’s a long time since you posted this, I hope my reply is still useful. I recommend using the issue tracker if you find things not working according to expectations.

I resolved the issue with the classification of constraint violation errors by switching them to `Request_failed. I think it’s fair to treat this as a bug, since the original classification was inconsistent with the other drivers and the definition of response-failed in the documentation, but there is a risk that sqlite3-only code relies on the past behaviour.

My advice for users of the sqlite3 driver who needs to catch constraint violations is to match it with

| Error (`Request_failed _ | `Response_failed _ as err) ->
    let cause = Caqti_error.cause err in
    ...

until you can require "caqti-driver-sqlite3" {> "2.2.4"}.