forked from javi11/nntppool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
69 lines (62 loc) · 1.9 KB
/
errors.go
File metadata and controls
69 lines (62 loc) · 1.9 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
package nntppool
import (
"errors"
"fmt"
)
// Error represents an NNTP protocol-level error with a status code.
type Error struct {
Code int
Message string
}
func (e *Error) Error() string {
return fmt.Sprintf("nntp: %d %s", e.Code, e.Message)
}
// Is matches by semantic category so that, for example, both 430 ("no such article")
// and 423 ("no article with that number") match ErrArticleNotFound.
func (e *Error) Is(target error) bool {
var t *Error
if !errors.As(target, &t) {
return false
}
return errorCategory(e.Code) == errorCategory(t.Code)
}
func errorCategory(code int) int {
switch code {
case 423, 430:
return 430 // article not found
default:
return code
}
}
var (
ErrArticleNotFound = &Error{Code: 430, Message: "no such article"}
ErrPostingNotPermitted = &Error{Code: 440, Message: "posting not permitted"}
ErrPostingFailed = &Error{Code: 441, Message: "posting failed"}
ErrAuthRequired = &Error{Code: 480, Message: "authentication required"}
ErrAuthRejected = &Error{Code: 481, Message: "authentication rejected"}
ErrServiceUnavailable = &Error{Code: 502, Message: "service unavailable"}
ErrCRCMismatch = errors.New("nntp: yEnc CRC mismatch")
ErrProtocolDesync = errors.New("nntp: protocol desync: expected status line, got binary data")
ErrQuotaExceeded = errors.New("nntp: download quota exceeded")
)
// toError maps an NNTP status code to a sentinel error, or returns nil for success codes.
func toError(code int, status string) error {
switch {
case code >= 200 && code < 400:
return nil
case code == 423 || code == 430:
return ErrArticleNotFound
case code == 440:
return ErrPostingNotPermitted
case code == 441:
return ErrPostingFailed
case code == 480:
return ErrAuthRequired
case code == 481:
return ErrAuthRejected
case code == 502:
return ErrServiceUnavailable
default:
return &Error{Code: code, Message: status}
}
}