mirror of
https://github.com/jackc/pgx.git
synced 2025-07-11 13:08:49 +00:00
Created Error Handling (markdown)
parent
bc90241e6d
commit
2ae7266c2f
16
Error-Handling.md
Normal file
16
Error-Handling.md
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# Error Handling
|
||||||
|
|
||||||
|
pgx uses Go 1.13+ style error wrapping. That means that `errors.Is()` and `errors.As()` should be used instead of directly testing an error for equality or attempting a type assertion.
|
||||||
|
|
||||||
|
If an error is returned from PostgreSQL then underlying error type will be a [*pgconn.PgError](https://pkg.go.dev/github.com/jackc/pgconn?tab=doc#PgError).
|
||||||
|
|
||||||
|
```go
|
||||||
|
err = conn.QueryRow(context.Background(), "select 1 +").Scan(&greeting)
|
||||||
|
if err != nil {
|
||||||
|
var pgErr *pgconn.PgError
|
||||||
|
if errors.As(err, &pgErr) {
|
||||||
|
fmt.Println(pgErr.Message) // => syntax error at end of input
|
||||||
|
fmt.Println(pgErr.Code) // => 42601
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user