-
Notifications
You must be signed in to change notification settings - Fork 175
/
error.go
58 lines (49 loc) · 1.34 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
package rest
import "net/http"
// StatusError provides custom error with http status.
type StatusError interface {
error // this is the actual error that occured
Status() int // the HTTP status code to return
UserMessage() string // the error message to return to the client
}
// NewRestError creates an error returned to user with provided status
// user displayed message and internal error
func NewRestError(status int, msg string, err error) *RestError {
return &RestError{
status: status,
userMessage: msg,
err: err,
}
}
// NewNotFoundError creates a new not found rest error.
func NewNotFoundError(msg string, err error) *RestError {
return &RestError{
status: http.StatusNotFound,
userMessage: msg,
err: err,
}
}
// NewBadRequestError creates a new bad request rest error.
func NewBadRequestError(err error) *RestError {
return &RestError{
status: http.StatusBadRequest,
userMessage: err.Error(),
err: err,
}
}
// RestError is implementation of status error.
type RestError struct {
status int
userMessage string
err error
}
func (e *RestError) UserMessage() string {
return e.userMessage
}
// Status returns error http status code.
func (e *RestError) Status() int {
return e.status
}
func (e *RestError) Error() string {
return e.err.Error()
}