-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathexception.go
executable file
·54 lines (46 loc) · 1.32 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
package sc
import (
"fmt"
)
// RegistryException structure contains message and error information for the exception caused by service-center
type RegistryException struct {
Title string
Err error
Message string
}
// Error gets the Error message from the Error
func (e *RegistryException) Error() string {
if e.Err == nil {
return fmt.Sprintf("%s(%s)", e.Title, e.Message)
}
return fmt.Sprintf("%s(%s), %s", e.Title, e.Err.Error(), e.Message)
}
func formatMessage(args []interface{}) string {
if len(args) == 0 {
return ""
}
format, ok := args[0].(string)
if !ok {
return fmt.Sprintf("%v", args)
}
return fmt.Sprintf(format, args[1:]...)
}
func newException(t string, e error, message string) *RegistryException {
return &RegistryException{
Title: t,
Err: e,
Message: message,
}
}
// NewCommonException creates a generic exception
func NewCommonException(format string, args ...interface{}) error {
return newException("Common exception", nil, fmt.Sprintf(format, args...))
}
// NewJSONException creates a JSON exception
func NewJSONException(e error, args ...interface{}) error {
return newException("JSON exception", e, formatMessage(args))
}
// NewIOException create and IO exception
func NewIOException(e error, args ...interface{}) error {
return newException("IO exception", e, formatMessage(args))
}