anse1 / emacs-libpq

An Emacs 25 module for accessing postgres via libpq.
GNU General Public License v3.0
22 stars 3 forks source link

Error handling #10

Closed gnusupport closed 5 years ago

gnusupport commented 5 years ago

I should be able to get the database erorr programmatically and not just enter debugger on something like

(pq:query *rcd-pg* "SEjnjn")

so how do I do it?

I cannot find description for these optional parameters:

pq:query is a module function.

(pq:query ARG1 ARG2 &optional ARG3 ARG4 ARG5 ARG6
ARG7 ARG8 ARG9 ARG10 ARG11 ARG12 ARG13 ARG14)

Execute QUERY on CONNECTION with optional PARAMETERS.

I was thinking there is some error handling option, I cannot find it. Please help. Imagine editing a table field and it should be unique, and database is raising error. I think I should be able to capture, understand error and do further programming based on that.

anse1 commented 5 years ago

notifications@github.com writes:

I should be able to get the erorr and not just enter debugger on something like

(pq:query *rcd-pg* "SEjnjn")

so how do I do it?

currently, your only option is to catch all emacs errors, look at the error string, and re-throw if you don't handle them. Example:

(condition-case err
    (pq:query *pq* "moo")
  (error
   (if (string-match "^ERROR:  syntax error" (cdr err))
       (message "whoop whoop")
     ;; re-throw
     (signal (car err) (cdr err))
     )))

I was thinking there is some error handling option, I cannot find it. Please help. Imagine editing a table field and it should be unique, and database is raising error. I think I should be able to capture, understand error and do further programming based on that.

Originally, emacs-libpq used a custom error signal, but @marsam changed it to common emacs errors in an early patch. I'm afraid I have to reintroduce it in order to make sane handling of SQL errors possible (e.g., handing out the SQLSTATE would allow for easy and standardized classification of errors thrown by emacs-libpq).

gnusupport commented 5 years ago

Thank you for that example of condition-case, I can now do something usable with it. Thanks for updates to the code.

anse1 commented 5 years ago

I just pushed a change that introduces a custom error signal that makes the SQLSTATE error code available. This will break the example I posted above, but is a much more reliable way to catch specific SQL errors. See the updated README for an example.