lib/pq — pull request #150 by paulhammond (2013-09-30 05:12:21+00:00)
Title: Error codes

This pull request adds `ErrorCode()` and `ErrorCodeName()` methods to `PGError` to make it possible for calling code to access the error code without having to understand the details of the protocol or parse the error string. It's based on the [suggestion](https://groups.google.com/forum/#!msg/golang-nuts/TURZm6W5A5o/5HW-gtkX2AUJ) made by @jmhodges on golang-nuts last year.

One implementation note: I initially attempted exposing code names as constants:
```
const (
…
CodeUniqueViolation = "23505"
…
)

// example usage:
// if (pqErr.ErrorCode() == pq.CodeUniqueViolation) {
// // handle duplicate unique key
// }
```

Unfortunately this won't work as five of the constant names are used for multiple error codes (`string_data_right_truncation`, `modifying_sql_data_not_permitted`, `prohibited_sql_statement_attempted`, `reading_sql_data_not_permitted` and `null_value_not_allowed`). Instead I've implemented a map between codes and names, with a convenience method to fetch the name in addition to the code. I'm open to other ideas on how to expose the names, including not doing anything and expecting calling code to use the 5 digit codes.