postgresql - Get error code number from postgres in Go -
i'm unable retrieve error code number when error in postgres.
in test of program know i'll following error " pq: duplicate key value violates unique constraint "associations_pkey"".
looking in postgres docs pq error code of 23505.
i need number in go program can check on different types of errors , respond end user in helpful way.
however, can't seem hold of error code in go, error message. code follows:
stmt, _ := db.prepare("insert table (column_1) values ($1)") _, err = stmt.exec("12324354") if err != nil { log.println("failed stmt .exec while trying insert new association") log.println(err.error()) fmt.println(err.code()) } else { render.json(w, 200, "new row created succesfully") }
you need type assert error type *pq.error
:
pqerr := err.(*pq.error) log.println(pqerr.code)
Comments
Post a Comment