-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
54 lines (46 loc) · 979 Bytes
/
Copy patherror.go
File metadata and controls
54 lines (46 loc) · 979 Bytes
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
package imageapi
import (
"errors"
"fmt"
)
const (
ERRCONFLICT = "conflict"
ERRINTERNAL = "internal"
ERRINVALID = "invalid"
ERRNOTFOUND = "not_found"
)
type Error struct {
// Machine-readable error code.
Code string
// Human-readable error message.
Message string
}
func (e *Error) Error() string {
return fmt.Sprintf("error: code=%s message=%s", e.Code, e.Message)
}
func ErrorCode(err error) string {
var e *Error
if err == nil {
return ""
} else if errors.As(err, &e) {
return e.Code
}
return ERRINTERNAL
}
// ErrorMessage unwraps an application error and returns its message.
// Non-application errors always return "Internal 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."
}
func Errorf(code string, format string, args ...interface{}) *Error {
return &Error{
Code: code,
Message: fmt.Sprintf(format, args...),
}
}