mmottl/postgresql-ocaml

How to get error information on result?

Closed this issue · 1 comments

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");
      }
    }
  }

I'll discuss this on the related pull request page: #24