-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
67 lines (56 loc) · 1.46 KB
/
error.go
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
package tusgo
import (
"errors"
"fmt"
"io"
"net/http"
)
type TusError struct {
inner error
msg string
}
func (te TusError) Error() string {
return fmt.Sprintf("%s: %s", te.msg, te.inner)
}
func (te TusError) Unwrap() error {
return te.inner
}
func (te TusError) Is(e error) bool {
v, ok := e.(TusError)
return ok && v.msg == te.msg || errors.Is(te.inner, e)
}
func (te TusError) WithErr(err error) TusError {
te.inner = err
return te
}
func (te TusError) WithText(s string) TusError {
te.inner = errors.New(s)
return te
}
func (te TusError) WithResponse(r *http.Response) TusError {
if r == nil {
te.inner = fmt.Errorf("response is nil")
return te
}
b := make([]byte, 256)
if l, err := io.ReadFull(r.Body, b); err == nil || err == io.EOF {
if l > 0 {
te.inner = fmt.Errorf("HTTP %d: <no body>", r.StatusCode)
} else {
te.inner = fmt.Errorf("HTTP %d: %s", r.StatusCode, b[:l])
}
} else {
panic(err)
}
return te
}
var (
ErrUnsupportedFeature = TusError{msg: "unsupported feature"}
ErrUploadTooLarge = TusError{msg: "upload is too large"}
ErrUploadDoesNotExist = TusError{msg: "upload does not exist"}
ErrOffsetsNotSynced = TusError{msg: "client stream and server offsets are not synced"}
ErrChecksumMismatch = TusError{msg: "checksum mismatch"}
ErrProtocol = TusError{msg: "protocol error"}
ErrCannotUpload = TusError{msg: "can not upload"}
ErrUnexpectedResponse = TusError{msg: "unexpected HTTP response code"}
)