Skip to content

Commit

Permalink
Add SeverityUnlocalized field to PgError / Notice
Browse files Browse the repository at this point in the history
  • Loading branch information
jackc committed Apr 7, 2024
1 parent 78b22c3 commit a3d9120
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
35 changes: 18 additions & 17 deletions pgconn/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,24 @@ func Timeout(err error) bool {
// http://www.postgresql.org/docs/11/static/protocol-error-fields.html for
// detailed field description.
type PgError struct {
Severity string
Code string
Message string
Detail string
Hint string
Position int32
InternalPosition int32
InternalQuery string
Where string
SchemaName string
TableName string
ColumnName string
DataTypeName string
ConstraintName string
File string
Line int32
Routine string
Severity string
SeverityUnlocalized string
Code string
Message string
Detail string
Hint string
Position int32
InternalPosition int32
InternalQuery string
Where string
SchemaName string
TableName string
ColumnName string
DataTypeName string
ConstraintName string
File string
Line int32
Routine string
}

func (pe *PgError) Error() string {
Expand Down
35 changes: 18 additions & 17 deletions pgconn/pgconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,23 +928,24 @@ func (pgConn *PgConn) Deallocate(ctx context.Context, name string) error {
// ErrorResponseToPgError converts a wire protocol error message to a *PgError.
func ErrorResponseToPgError(msg *pgproto3.ErrorResponse) *PgError {
return &PgError{
Severity: msg.Severity,
Code: string(msg.Code),
Message: string(msg.Message),
Detail: string(msg.Detail),
Hint: msg.Hint,
Position: msg.Position,
InternalPosition: msg.InternalPosition,
InternalQuery: string(msg.InternalQuery),
Where: string(msg.Where),
SchemaName: string(msg.SchemaName),
TableName: string(msg.TableName),
ColumnName: string(msg.ColumnName),
DataTypeName: string(msg.DataTypeName),
ConstraintName: msg.ConstraintName,
File: string(msg.File),
Line: msg.Line,
Routine: string(msg.Routine),
Severity: msg.Severity,
SeverityUnlocalized: msg.SeverityUnlocalized,
Code: string(msg.Code),
Message: string(msg.Message),
Detail: string(msg.Detail),
Hint: msg.Hint,
Position: msg.Position,
InternalPosition: msg.InternalPosition,
InternalQuery: string(msg.InternalQuery),
Where: string(msg.Where),
SchemaName: string(msg.SchemaName),
TableName: string(msg.TableName),
ColumnName: string(msg.ColumnName),
DataTypeName: string(msg.DataTypeName),
ConstraintName: msg.ConstraintName,
File: string(msg.File),
Line: msg.Line,
Routine: string(msg.Routine),
}
}

Expand Down
9 changes: 5 additions & 4 deletions pgconn/pgconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,9 +1556,9 @@ func TestConnOnNotice(t *testing.T) {
config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
require.NoError(t, err)

var msg string
config.OnNotice = func(c *pgconn.PgConn, notice *pgconn.Notice) {
msg = notice.Message
var notice *pgconn.Notice
config.OnNotice = func(c *pgconn.PgConn, n *pgconn.Notice) {
notice = n
}
config.RuntimeParams["client_min_messages"] = "notice" // Ensure we only get the message we expect.

Expand All @@ -1576,7 +1576,8 @@ begin
end$$;`)
err = multiResult.Close()
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)
}
Expand Down

0 comments on commit a3d9120

Please sign in to comment.