-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
61 lines (44 loc) · 1.06 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
package goutility
import "fmt"
const (
UnspecifiedCode = -1
UnspecifiedCodeContext = "goutility"
)
type ErrorCode int
type ErrorTypeInterface interface {
Error() string
Message() string
Code() ErrorCode
CodeContext() string
HasValidCode() bool
}
type ErrorType struct {
message string
code ErrorCode
codeContext string
}
func MakeError(message string) ErrorType {
return MakeErrorWithCode(message, UnspecifiedCode, UnspecifiedCodeContext)
}
func MakeErrorWithCode(message string, code ErrorCode, codeContext string) ErrorType {
return ErrorType{
message: message,
code: code,
codeContext: codeContext,
}
}
func (this ErrorType) Error() string {
return fmt.Sprintf("Context: %s, Code: %d, %s", this.CodeContext(), this.Code(), this.Message())
}
func (this ErrorType) Message() string {
return this.message
}
func (this ErrorType) Code() ErrorCode {
return this.code
}
func (this ErrorType) CodeContext() string {
return this.codeContext
}
func (this ErrorType) HasValidCode() bool {
return this.Code() != UnspecifiedCode
}