Skip to content

Instantly share code, notes, and snippets.

@marcintustin
Created October 30, 2024 13:53
Show Gist options
  • Save marcintustin/47839ebee778a94093f17009e71ae006 to your computer and use it in GitHub Desktop.
Save marcintustin/47839ebee778a94093f17009e71ae006 to your computer and use it in GitHub Desktop.
Handling a pg error and getting the stacktrace in go
import (
"github.com/jackc/pgconn"
"github.com/pkg/errors"
)
func main() {
if err != nil {
var pgerr *pgconn.PgError
if errors.As(err, &pgerr) && pgerr.SQLState() == database.DUPLICATE {
t.Log(err)
type stackTracer interface {
error
StackTrace() errors.StackTrace
}
var tracerr stackTracer
var iterr error = err
for errors.As(iterr, &tracerr) {
t.Error(tracerr.StackTrace())
iterr = errors.Unwrap(tracerr)
}
t.Fatal(err)
} else {
t.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment