-
-
Notifications
You must be signed in to change notification settings - Fork 955
Expand file tree
/
Copy pathexample126_test.go
More file actions
37 lines (30 loc) · 866 Bytes
/
example126_test.go
File metadata and controls
37 lines (30 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//go:build go1.26
package pq_test
import (
"database/sql"
"errors"
"fmt"
"github.com/lib/pq"
)
func Example_error() {
db, _ := sql.Open("postgres", "")
defer db.Close()
_, _ = db.Exec(`create temp table users (email text)`)
_, _ = db.Exec(`create unique index on users(lower(email))`)
_, err := db.Exec(`insert into users values ('a@example.com'), ('a@example.com')`)
if err != nil {
fmt.Println("Error()")
fmt.Println(err)
fmt.Println("\nErrorWithDetail()")
if pqErr, ok := errors.AsType[*pq.Error](err); ok {
fmt.Println(pqErr.ErrorWithDetail())
}
}
// Output:
// Error()
// pq: duplicate key value violates unique constraint "users_lower_idx" (23505)
//
// ErrorWithDetail()
// ERROR: duplicate key value violates unique constraint "users_lower_idx" (23505)
// DETAIL: Key (lower(email))=(a@example.com) already exists.
}