-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
errors.go
85 lines (70 loc) · 2.1 KB
/
errors.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
80
81
82
83
84
85
package gocrawl
import (
"errors"
)
var (
// ErrEnqueueRedirect is returned when a redirection is requested, so that the
// worker knows that this is not an actual Fetch error, but a request to
// enqueue the redirect-to URL.
ErrEnqueueRedirect = errors.New("redirection not followed")
// ErrMaxVisits is returned when the maximum number of visits, as specified by the
// Options field MaxVisits, is reached.
ErrMaxVisits = errors.New("the maximum number of visits is reached")
// ErrInterrupted is returned when the crawler is manually stopped
// (via a call to Stop).
ErrInterrupted = errors.New("interrupted")
)
// CrawlErrorKind indicated the kind of crawling error.
type CrawlErrorKind uint8
// The various kinds of crawling errors.
const (
CekFetch CrawlErrorKind = iota
CekParseRobots
CekHttpStatusCode
CekReadBody
CekParseBody
CekParseURL
CekProcessLinks
CekParseRedirectURL
)
var (
lookupCek = [...]string{
CekFetch: "Fetch",
CekParseRobots: "ParseRobots",
CekHttpStatusCode: "HttpStatusCode",
CekReadBody: "ReadBody",
CekParseBody: "ParseBody",
CekParseURL: "ParseURL",
CekProcessLinks: "ProcessLinks",
CekParseRedirectURL: "ParseRedirectURL",
}
)
// String returns the string representation of the error kind.
func (cek CrawlErrorKind) String() string {
return lookupCek[cek]
}
// CrawlError contains information about the crawling error.
type CrawlError struct {
// The URL Context where the error occurred.
Ctx *URLContext
// The underlying error.
Err error
// The error kind.
Kind CrawlErrorKind
msg string
}
// Error implements of the error interface for CrawlError.
func (ce CrawlError) Error() string {
if ce.Err != nil {
return ce.Err.Error()
}
return ce.msg
}
// Create a new CrawlError based on a source error.
func newCrawlError(ctx *URLContext, e error, kind CrawlErrorKind) *CrawlError {
return &CrawlError{ctx, e, kind, ""}
}
// Create a new CrawlError with the specified message.
func newCrawlErrorMessage(ctx *URLContext, msg string, kind CrawlErrorKind) *CrawlError {
return &CrawlError{ctx, nil, kind, msg}
}