storj/private/dbutil/pgutil/pgerrcode/err.go
Egon Elbre 4e8d53c8fb private/dbutil/pgutil: ensure storagenode doesn't depend on pgx
pgx is a large dependency and there's no need to include it in
storagenode binary.

Change-Id: I49c304c6420733d5f095d7edb35d32811210e41a
2020-09-30 14:28:47 +00:00

27 lines
826 B
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
// Package pgerrcode implements postgres error extraction without depending on a postgres
// library.
package pgerrcode
import "errors"
// FromError returns the 5-character PostgreSQL error code string associated
// with the given error, if any.
func FromError(err error) string {
var sqlStateErr errWithSQLState
if errors.As(err, &sqlStateErr) {
return sqlStateErr.SQLState()
}
return ""
}
// errWithSQLState is an interface supported by error classes corresponding
// to PostgreSQL errors from certain drivers. This is satisfied, in particular,
// by pgx (*pgconn.PgError) and may be adopted by other types. An effort is
// apparently underway to get lib/pq to add this interface.
type errWithSQLState interface {
SQLState() string
}