-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy patherror.go
79 lines (63 loc) · 1.17 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
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2014-2019 Liu Dong <[email protected]>.
// Licensed under the MIT license.
package httpclient
import (
"fmt"
"net"
"strings"
)
// Package errors
const (
_ = iota
ERR_DEFAULT
ERR_TIMEOUT
ERR_REDIRECT_POLICY
)
// Custom error
type Error struct {
Code int
Message string
}
// Implement the error interface
func (this Error) Error() string {
return fmt.Sprintf("httpclient #%d: %s", this.Code, this.Message)
}
func getErrorCode(err error) int {
if err == nil {
return 0
}
if e, ok := err.(*Error); ok {
return e.Code
}
return ERR_DEFAULT
}
// Check a timeout error.
func IsTimeoutError(err error) bool {
if err == nil {
return false
}
// TODO: does not work?
if e, ok := err.(net.Error); ok && e.Timeout() {
return true
}
// TODO: make it reliable
if strings.Contains(strings.ToLower(err.Error()), "timeout") {
return true
}
return false
}
// Check a redirect error
func IsRedirectError(err error) bool {
if err == nil {
return false
}
// TODO: does not work?
if getErrorCode(err) == ERR_REDIRECT_POLICY {
return true
}
// TODO: make it reliable
if strings.Contains(err.Error(), "redirect") {
return true
}
return false
}