mmottl / postgresql-ocaml

OCaml-bindings for the PostgreSQL database
Other
141 stars 23 forks source link

How to get error information on result? #23

Closed sgrove closed 6 years ago

sgrove commented 6 years ago

I'm trying to more robustly handle errors, and build up error objects with enough context to generate dev (and user)-friendly error messages.

From https://www.postgresql.org/docs/9.5/static/libpq-exec.html it looks like we should using PQresultErrorField. https://www.postgresql.org/docs/current/static/protocol-error-fields.html looks like it has everything we need.

https://stackoverflow.com/questions/18645626/postgres-sqlstate-pqresulterrorfield-returns-null also has a code snippet, but looking at the current c-stubs, I don't think this is exposed right now.

PGresult* res = PQexec(conn, "SELECT * FROM foobar");
  if (res) {
    if (PQresultStatus(res)==PGRES_FATAL_ERROR) {
      char* p = PQresultErrorField(res, PG_DIAG_SQLSTATE);
      if (p) {
        printf("sqlstate=%s\n", p?p:"null");
      }
    }
  }
mmottl commented 6 years ago

I'll discuss this on the related pull request page: https://github.com/mmottl/postgresql-ocaml/pull/24