Add SeverityUnlocalized field to PgError / Notice

https://github.com/jackc/pgx/issues/1971
pull/1982/head
Jack Christensen 2024-04-07 08:58:10 -05:00
parent 78b22c3d2f
commit a3d9120636
3 changed files with 41 additions and 38 deletions

View File

@ -29,23 +29,24 @@ func Timeout(err error) bool {
// http://www.postgresql.org/docs/11/static/protocol-error-fields.html for // http://www.postgresql.org/docs/11/static/protocol-error-fields.html for
// detailed field description. // detailed field description.
type PgError struct { type PgError struct {
Severity string Severity string
Code string SeverityUnlocalized string
Message string Code string
Detail string Message string
Hint string Detail string
Position int32 Hint string
InternalPosition int32 Position int32
InternalQuery string InternalPosition int32
Where string InternalQuery string
SchemaName string Where string
TableName string SchemaName string
ColumnName string TableName string
DataTypeName string ColumnName string
ConstraintName string DataTypeName string
File string ConstraintName string
Line int32 File string
Routine string Line int32
Routine string
} }
func (pe *PgError) Error() string { func (pe *PgError) Error() string {

View File

@ -928,23 +928,24 @@ func (pgConn *PgConn) Deallocate(ctx context.Context, name string) error {
// ErrorResponseToPgError converts a wire protocol error message to a *PgError. // ErrorResponseToPgError converts a wire protocol error message to a *PgError.
func ErrorResponseToPgError(msg *pgproto3.ErrorResponse) *PgError { func ErrorResponseToPgError(msg *pgproto3.ErrorResponse) *PgError {
return &PgError{ return &PgError{
Severity: msg.Severity, Severity: msg.Severity,
Code: string(msg.Code), SeverityUnlocalized: msg.SeverityUnlocalized,
Message: string(msg.Message), Code: string(msg.Code),
Detail: string(msg.Detail), Message: string(msg.Message),
Hint: msg.Hint, Detail: string(msg.Detail),
Position: msg.Position, Hint: msg.Hint,
InternalPosition: msg.InternalPosition, Position: msg.Position,
InternalQuery: string(msg.InternalQuery), InternalPosition: msg.InternalPosition,
Where: string(msg.Where), InternalQuery: string(msg.InternalQuery),
SchemaName: string(msg.SchemaName), Where: string(msg.Where),
TableName: string(msg.TableName), SchemaName: string(msg.SchemaName),
ColumnName: string(msg.ColumnName), TableName: string(msg.TableName),
DataTypeName: string(msg.DataTypeName), ColumnName: string(msg.ColumnName),
ConstraintName: msg.ConstraintName, DataTypeName: string(msg.DataTypeName),
File: string(msg.File), ConstraintName: msg.ConstraintName,
Line: msg.Line, File: string(msg.File),
Routine: string(msg.Routine), Line: msg.Line,
Routine: string(msg.Routine),
} }
} }

View File

@ -1556,9 +1556,9 @@ func TestConnOnNotice(t *testing.T) {
config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_DATABASE")) config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
require.NoError(t, err) require.NoError(t, err)
var msg string var notice *pgconn.Notice
config.OnNotice = func(c *pgconn.PgConn, notice *pgconn.Notice) { config.OnNotice = func(c *pgconn.PgConn, n *pgconn.Notice) {
msg = notice.Message notice = n
} }
config.RuntimeParams["client_min_messages"] = "notice" // Ensure we only get the message we expect. config.RuntimeParams["client_min_messages"] = "notice" // Ensure we only get the message we expect.
@ -1576,7 +1576,8 @@ begin
end$$;`) end$$;`)
err = multiResult.Close() err = multiResult.Close()
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "hello, world", msg) assert.Equal(t, "NOTICE", notice.SeverityUnlocalized)
assert.Equal(t, "hello, world", notice.Message)
ensureConnValid(t, pgConn) ensureConnValid(t, pgConn)
} }