-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
95 lines (80 loc) · 2.24 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
86
87
88
89
90
91
92
93
94
95
package grcon
import (
"errors"
"fmt"
)
// Action is a type to indicate the part in which an error occurred.
type Action string
// Actions for error checks.
const (
// Write indicates that it happened on a write operation.
Write Action = "write"
// Read indicates that it happened on a read operation.
Read Action = "read"
)
// GrconError is the interface all errors from this packet implement.
// You can use this interface for errors.Is checks.
type GrconError interface {
// Default error interface.
error
// Action returns the action that produced the error.
Action() Action
}
func newGrconGenericError(act Action, err error) GrconGenericError {
return GrconGenericError{
Act: act,
Err: err,
}
}
// GrconGenericError is a generic error that provides default implementations for the grconError interface.
type GrconGenericError struct {
Err error
Act Action
}
// Error returns the error in string format.
func (rge GrconGenericError) Error() string {
return fmt.Sprintf("grcon: on %s: %s", rge.Action(), rge.Err.Error())
}
// Action returns the action where the error was thrown.
func (rge GrconGenericError) Action() Action {
return rge.Act
}
func newUnexpectedFormatError() UnexpectedFormatError {
return UnexpectedFormatError{
newGrconGenericError(
Read,
errors.New("unexpected response format: the packet is smaller than the minimum size"),
),
}
}
// UnexpectedFormatError occurres when the packet size is smaller than the minimum size.
// This indicates a wrongly composed/formatted packet.
type UnexpectedFormatError struct {
GrconGenericError
}
func newRequestTooLongError() RequestTooLongError {
return RequestTooLongError{
newGrconGenericError(
Write,
errors.New("request body is too long"),
),
}
}
// RequestTooLongError occurres when the length of a packet is to big.
// This indicates that the body is too long.
type RequestTooLongError struct {
GrconGenericError
}
func newResponseTooLongError() ResponseTooLongError {
return ResponseTooLongError{
newGrconGenericError(
Read,
errors.New("response packet is too long"),
),
}
}
// ResponseTooLongError occurres when the size of a packet is to big.
// This indicates a wrongly composed/formatted packet.
type ResponseTooLongError struct {
GrconGenericError
}