Skip to content

Commit

Permalink
Improve error messages (#354)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benesch authored Nov 10, 2023
1 parent 18f64b6 commit 24a381a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/materialize/generic.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package materialize

import (
"errors"
"fmt"
"log"

"github.com/jackc/pgx"
"github.com/jmoiron/sqlx"
)

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 24a381a

Please sign in to comment.