This repository has been archived by the owner on May 13, 2022. It is now read-only.
/
exception.go
81 lines (68 loc) · 1.72 KB
/
exception.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
package errors
import "fmt"
func NewException(code *Code, exception string) *Exception {
if exception == "" {
return nil
}
return &Exception{
CodeNumber: code.Number,
Exception: exception,
}
}
// Wraps any error as a Exception
func AsException(err error) *Exception {
if err == nil {
return nil
}
switch e := err.(type) {
case *Exception:
return e
case CodedError:
return NewException(e.ErrorCode(), e.ErrorMessage())
default:
return NewException(Codes.Generic, err.Error())
}
}
func Wrapf(err error, format string, a ...interface{}) *Exception {
ex := AsException(err)
return NewException(Codes.Get(ex.CodeNumber), fmt.Sprintf(format, a...))
}
func Wrap(err error, message string) *Exception {
ex := AsException(err)
return NewException(Codes.Get(ex.CodeNumber), message+": "+ex.Exception)
}
func Errorf(code *Code, format string, a ...interface{}) *Exception {
return NewException(code, fmt.Sprintf(format, a...))
}
func (e *Exception) AsError() error {
// We need to return a bare untyped error here so that err == nil downstream
if e == nil {
return nil
}
return e
}
func (e *Exception) ErrorCode() *Code {
return Codes.Get(e.CodeNumber)
}
func (e *Exception) Error() string {
return fmt.Sprintf("error %d - %s: %s", e.CodeNumber, Codes.Get(e.CodeNumber), e.Exception)
}
func (e *Exception) String() string {
return e.Error()
}
func (e *Exception) ErrorMessage() string {
if e == nil {
return ""
}
return e.Exception
}
func (e *Exception) Equal(ce CodedError) bool {
ex := AsException(ce)
if e == nil || ex == nil {
return e == nil && ex == nil
}
return e.CodeNumber == ex.CodeNumber && e.Exception == ex.Exception
}
func (e *Exception) GetCode() *Code {
return Codes.Get(e.CodeNumber)
}