-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
86 lines (78 loc) · 2.97 KB
/
Copy patherrors.go
File metadata and controls
86 lines (78 loc) · 2.97 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package dbimp
import (
"fmt"
"io"
"net/http"
"strings"
)
// Error is an error of this package.
type Error string
// Error satisfies the error interface.
func (err Error) Error() string {
return string(err)
}
// Error values.
const (
// ErrNotSupported is the error for a feature that the database does not
// have, such as a transaction (D20).
ErrNotSupported Error = "not supported"
// ErrScheme is the error for a DSN whose scheme is not the name of the
// driver (D35).
ErrScheme Error = "wrong scheme"
// ErrUnknownKey is the error for a key in the query of a DSN that the
// driver does not know (D27).
ErrUnknownKey Error = "unknown key"
// ErrRepeatedKey is the error for a key that appears more than once in
// the query of a DSN (D27).
ErrRepeatedKey Error = "repeated key"
// ErrInvalidValue is the error for a value that has the wrong form.
ErrInvalidValue Error = "invalid value"
// ErrExtraColumn is the error for a row that has a column that the first
// row does not have (D18).
ErrExtraColumn Error = "column not in the first row"
// ErrColumnCount is the error for a row that has the wrong number of
// columns.
ErrColumnCount Error = "wrong number of columns"
// ErrIncomplete is the error for a result set that failed, or that the
// server cut short, after at least one of its rows reached the caller
// (D21 and D107). A failure before the first row does not wrap it.
ErrIncomplete Error = "result is incomplete"
// ErrUnterminated is the error for a statement that ends inside a
// literal, a quoted identifier or a comment.
ErrUnterminated Error = "unterminated literal or comment"
// ErrArguments is the error for arguments that do not match the
// placeholders of a statement (D34).
ErrArguments Error = "arguments do not match the placeholders"
)
// maxErrorBody is the most of the body of a response that a StatusError
// keeps.
const maxErrorBody = 64 << 10
// StatusError is the error for a response whose status is not 2xx. HTTP 429
// and HTTP 503 are a StatusError too, and nothing retries them (D8).
type StatusError struct {
// Code is the status code of the response.
Code int
// Body is the start of the body of the response, at most 64 KiB.
Body string
}
// Error satisfies the error interface.
func (err *StatusError) Error() string {
text := http.StatusText(err.Code)
if body := strings.TrimSpace(err.Body); body != "" {
return fmt.Sprintf("status %d %s: %s", err.Code, text, body)
}
return fmt.Sprintf("status %d %s", err.Code, text)
}
// CheckStatus returns nil if the status of res is 2xx. Otherwise it reads at
// most 64 KiB of the body, closes the body, and returns a *StatusError.
func CheckStatus(res *http.Response) error {
if res.StatusCode >= 200 && res.StatusCode < 300 {
return nil
}
defer res.Body.Close()
body, err := io.ReadAll(io.LimitReader(res.Body, maxErrorBody))
if err != nil {
return fmt.Errorf("reading the body of status %d: %w", res.StatusCode, err)
}
return &StatusError{Code: res.StatusCode, Body: string(body)}
}