From 24a381a925b0240a86af33e3d82e1293fd8fd313 Mon Sep 17 00:00:00 2001 From: Nikhil Benesch Date: Fri, 10 Nov 2023 07:34:22 -0500 Subject: [PATCH] Improve error messages (#354) Include detail and hint fields for conn execution errors. This is crucial for certain types of errors (e.g., unable to connect to PostgreSQL source), where the true cause of the error message is only returned in the detail field. --- pkg/materialize/generic.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/materialize/generic.go b/pkg/materialize/generic.go index 44086077..d6b73898 100644 --- a/pkg/materialize/generic.go +++ b/pkg/materialize/generic.go @@ -1,9 +1,11 @@ package materialize import ( + "errors" "fmt" "log" + "github.com/jackc/pgx" "github.com/jmoiron/sqlx" ) @@ -41,6 +43,19 @@ func (b *Builder) exec(statement string) error { _, err := b.conn.Exec(statement) if err != nil { log.Printf("[DEBUG] error executing: %s", statement) + var pgErr pgx.PgError + pgErr, ok := err.(pgx.PgError) + if ok { + msg := fmt.Sprintf("%s: %s", pgErr.Severity, pgErr.Message) + if pgErr.Detail != "" { + msg += fmt.Sprintf(" DETAIL: %s", pgErr.Detail) + } + if pgErr.Hint != "" { + msg += fmt.Sprintf(" HINT: %s", pgErr.Hint) + } + msg += fmt.Sprintf(" (SQLSTATE %s)", pgErr.SQLState()) + return errors.New(msg) + } return err }