Answer the question
In order to leave comments, you need to log in
Why do go apps crash on postgres error?
I am using the pgx for postgres library. When there is an error, the application crashes.
For example, an error
ERROR: duplicate key value violates unique constraint "users_pkey"
func (creater *creatorUser) addUser(tx *sql.Tx) (err error) {
query := fmt.Sprintf(
`INSERT INTO %s ("uuid", phone_number, email, password_hash) VALUES($1, $2, $3, $4)`,
UserTable,
)
inserted, err := creater.database.Prepare(query)
defer inserted.Close()
if err != nil {
log.Fatalf("users: unable to rollback: %v\n", err)
return
}
_, err = tx.
StmtContext(creater.ctx, inserted).
Exec(
creater.data.UUID,
creater.data.PhoneNumber,
creater.data.Email,
creater.data.PasswordHash)
if err != nil {
tx.Rollback()
log.Fatalf("users: add user failed: %v", err)
}
return
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question