/
code.go
56 lines (47 loc) · 852 Bytes
/
code.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
package errors
import (
"fmt"
)
// An annotated version of the pure numeric error code
type Code struct {
Number uint32
Name string
Description string
}
func code(description string) *Code {
return &Code{Description: description}
}
func (c *Code) Equal(other *Code) bool {
if c == nil {
return false
}
return c.Number == other.Number
}
func (c *Code) ErrorCode() *Code {
return c
}
func (c *Code) Uint32() uint32 {
if c == nil {
return 0
}
return c.Number
}
func (c *Code) Error() string {
if c == nil {
return ""
}
return fmt.Sprintf("Error %d: %s", c.Number, c.Description)
}
func (c *Code) ErrorMessage() string {
if c == nil {
return ""
}
return c.Description
}
func GetCode(err error) *Code {
exception := AsException(err)
if exception == nil {
return Codes.None
}
return exception.GetCode()
}