pgconn icon indicating copy to clipboard operation
pgconn copied to clipboard

Add "Detail" to PgError.Error()

Open DmitriyVTitov opened this issue 4 years ago • 2 comments
trafficstars

I think current error message lacks some important information. For example, when I try to insert a record and operation violates some constraint, then error message (PgError.Message) does not contain info about which record caused the error. It only says than constraint violation occured. Could you please add Detail filed to (pe *PgError) Error() method? I know that I can cast err to pgconn.PgError, but I think it would be more conveniently to see error details by default.

DmitriyVTitov avatar Aug 26 '21 02:08 DmitriyVTitov

That would be convenient in many cases. But I'm concerned about how long the error message could be. DETAIL sometimes echos back the entire failing row. This could make the error message hundreds of characters long. I don't think there is an actual hard rule about how long a Go error string should be, but it seems idiomatic to be pretty short. There's also the matter of how to format it. psql splits that type of error onto multiple lines. e.g.

ERROR:  23502: null value in column "user_name" of relation "users" violates not-null constraint
DETAIL:  Failing row contains (1, null, , , null, f, 2021-08-28 09:30:06.519231-05, 2021-08-28 09:30:06.519231-05, fejklf aefwe wekjfajwe ekfjljawe awfekljfkwelaj efkjfwelewkj ewk..., null, null, t, t, f, f, f, f, f, f, f, f, f, null, null, t, f, f, f).

I'm willing to concede if there is a strong consensus for this, but otherwise I think it should stay just the message.

jackc avatar Aug 28 '21 14:08 jackc

Maybe you should return say first 100 bytes of Detail message? I got your point but in my experience error messages themselves are pretty useless: something like SQL ERROR 23305. So choose yourself but I think some additional info required.

DmitriyVTitov avatar Aug 28 '21 15:08 DmitriyVTitov