Y
Y
yativ_sobb2021-01-18 11:53:41
PostgreSQL
yativ_sobb, 2021-01-18 11:53:41

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

1 answer(s)
E
Evgeny Mamonov, 2021-01-18
@yativ_sobb

Because of log.Fatalf

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question