Npgsql fastpath
If NpgsqlException.Errors is null in your exception handler, you most likely got an error in the Npgsql Fastpath (I still consider this an error). Here is the violation line in version 2, but the same problem exists in version 1.
It basically boils down to this code, where the correct NpgsqlError built, and then thrown when an exception is raised, calling ToString() on it.
NpgsqlError e = new NpgsqlError(conn.BackendProtocolVersion, stream); throw new NpgsqlException(e.ToString());
A fix would be as simple as changing the code to this and using the already supported ability of NpgsqlException to take a list of NpgsqlError in the constructor.
NpgsqlError e = new NpgsqlError(conn.BackendProtocolVersion, stream); throw new NpgsqlException(new ArrayList(e));
So, you cannot do this without string manipulations unless you compile your own version of Npgsql, fixing the problem.
Npgsql
If you are not using Fastpath, then the created NpgsqlException object contains the Errors property, which must be non-null. This is an example of extracting a message from the first error.
(ex.Errors[0] as NpgsqlError).Message
David Ewen
source share