-
Notifications
You must be signed in to change notification settings - Fork 683
Expand file tree
/
Copy pathexample_http_error_test.go
More file actions
38 lines (32 loc) · 1.02 KB
/
Copy pathexample_http_error_test.go
File metadata and controls
38 lines (32 loc) · 1.02 KB
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
38
package clickhouse_test
import (
"context"
"errors"
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
)
// Server-side failures carry a typed *clickhouse.Exception on both the native
// and HTTP protocols. Over HTTP, a non-200 response is additionally wrapped in
// *clickhouse.HTTPError; exceptions that occur after the server started
// streaming a 200 response surface as a bare *clickhouse.Exception.
func ExampleHTTPError() {
conn, err := clickhouse.Open(&clickhouse.Options{
Protocol: clickhouse.HTTP,
Addr: []string{"localhost:8123"},
})
if err != nil {
panic(err)
}
defer conn.Close()
_, err = conn.Query(context.Background(), "SELECT * FROM non_existent_table")
var chErr *clickhouse.Exception
if errors.As(err, &chErr) {
// works on the native protocol too; CodeName (e.g. "UNKNOWN_TABLE")
// is best-effort and currently only set over HTTP
fmt.Println(chErr.Code, chErr.CodeName, chErr.Message)
}
var httpErr *clickhouse.HTTPError
if errors.As(err, &httpErr) {
fmt.Println(httpErr.StatusCode)
}
}