-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
64 lines (56 loc) · 1.51 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
62
63
64
package pa
import (
"errors"
"fmt"
)
// Error codes which map good to http errors.
const (
ECONFLICT = "conflict"
EINTERNAL = "internal"
EINVALID = "invalid"
ENOTFOUND = "not_found"
ENOTIMPLEMENTED = "not_implemented"
EUNAUTHORIZED = "unauthorized"
)
// Error is a struct containing full details about the error.
type Error struct {
// Code to check the type of the error.
Code string
// Human readeable message.
Message string
}
// Error is used to implement the error interface.
func (e *Error) Error() string {
return fmt.Sprintf("patrickarvatu.com error: code=%s message=%s", e.Code, e.Message)
}
// ErrorCode is a helper function to retrieve the error code from a pa.Error.
// returns an empty string if err is nil.
// returns EINTERNAL if the error isnt a pa.Error.
func ErrorCode(err error) string {
var e *Error
if err == nil {
return ""
} else if errors.As(err, &e) {
return e.Code
}
return EINTERNAL
}
// ErrorMessage is a helper function to retrieve the error message from pa.Error.
// returns an empty string if err is nil.
// returns "Internal error." if the error isnt a pa.Error.
func ErrorMessage(err error) string {
var e *Error
if err == nil {
return ""
} else if errors.As(err, &e) {
return e.Message
}
return "Internal error."
}
// Errorf is a helper function to quickly init an error with code and format: message.
func Errorf(code string, format string, args ...interface{}) *Error {
return &Error{
Code: code,
Message: fmt.Sprintf(format, args...),
}
}