-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
49 lines (43 loc) · 1.03 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
package helpers
import (
"log"
"net/http"
"strings"
)
// Error represents an error that occurred while handling a request.
type Error struct {
Code int `json:"code"`
Source interface{} `json:"source,omitempty"`
Title string `json:"title,omitempty"`
Message string `json:"message,omitempty"`
}
func (e *Error) Error() (errStr string) {
return e.Message
}
func (e *Error) Log() {
log.Printf("source: %+s \nerr: %+s \n", e.Source, e.Message)
}
func NewError(code int, message ...string) (err *Error) {
if len(message) == 0 {
message = append(message, http.StatusText(code))
}
err = &Error{
Code: code,
Source: WhereAmI(2),
Title: http.StatusText(code),
Message: strings.Join(message, " \n"),
}
return
}
func NewErrorSource(code int, source string, message ...string) (err *Error) {
if len(message) == 0 {
message = append(message, http.StatusText(code))
}
err = &Error{
Code: code,
Source: source,
Title: http.StatusText(code),
Message: strings.Join(message, " \n"),
}
return
}