Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: add SQLState #1080

Merged
merged 1 commit into from May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions error.go
Expand Up @@ -402,6 +402,11 @@ func (err *Error) Fatal() bool {
return err.Severity == Efatal
}

// SQLState returns the SQLState of the error.
func (err *Error) SQLState() string {
return string(err.Code)
}

// Get implements the legacy PGError interface. New code should use the fields
// of the Error struct directly.
func (err *Error) Get(k byte) (v string) {
Expand Down
16 changes: 16 additions & 0 deletions go18_test.go
Expand Up @@ -334,3 +334,19 @@ func TestTxOptions(t *testing.T) {
t.Errorf("Expected error to mention isolation level, got %q", err)
}
}

func TestErrorSQLState(t *testing.T) {
r := readBuf([]byte{67, 52, 48, 48, 48, 49, 0, 0}) // 40001
err := parseError(&r)
var sqlErr errWithSQLState
if !errors.As(err, &sqlErr) {
t.Fatal("SQLState interface not satisfied")
}
if state := err.SQLState(); state != "40001" {
t.Fatalf("unexpected SQL state %v", state)
}
}

type errWithSQLState interface {
SQLState() string
}