taocpp / taopq

C++ client library for PostgreSQL
Boost Software License 1.0
264 stars 40 forks source link

Better ways to detect constraint violations, etc #45

Closed mattfbacon closed 3 years ago

mattfbacon commented 3 years ago

Currently you just get a runtime error. Compare this to the exceptions provided by libpqxx: https://libpqxx.readthedocs.io/en/7.6.0/a00247.html.

I will try to do this myself, but I don't know much about PostgreSQL so it may be difficult.

d-frey commented 3 years ago

It's on my TODO list, but first I need to finish those iterator/container requirements I'm currently working on.

mattfbacon commented 3 years ago

Alright, no problem. BTW, for now I'm doing something like this:

    } catch (std::runtime_error const& e) {
        if (strstr(e.what(), "duplicate key value violates unique constraint")) {
            // constraint exception 
            // ...
        } else {
            throw;
        }
    }

Not sure if there's a better way to get the error message from the library, but this could be an option if not.

d-frey commented 3 years ago

I've given up on the iterator-stuff for today and I used the waiting time to Inspiration4's launch to implement the basic exception hierarchy similar to libpqxx. Actually, I mostly followed @jtv's lead - Jeroen, I hope you don't mind :)

jtv commented 3 years ago

Hi @d-frey, long time no see. No I don't mind. :-)

d-frey commented 3 years ago

I might have gone a little overboard, but I added exception classes for all the PostgreSQL Error Codes that are documented.

In their infinite wisdom, the PostgreSQL developers documented four condition names that are ambiguous. 🤬

I turned those into templates and you need to add the error class as a template parameter, so instead of the ambiguous string_data_right_truncation you need to specify which one you mean: string_data_right_truncation<warning> or string_data_right_truncation<data_exception>.

emmenlau commented 3 years ago

Awesome work!