forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
44 lines (38 loc) · 906 Bytes
/
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
package middlewares
import (
"encoding/json"
"net/http"
"github.com/sensu/sensu-go/backend/apid/actions"
)
func writeErr(w http.ResponseWriter, err error) {
var errRes actions.Error
if erro, ok := err.(actions.Error); ok {
errRes = erro
} else {
errRes = actions.Error{
Code: actions.InternalErr,
Message: err.Error(),
}
}
var st int
switch errRes.Code {
case actions.InternalErr:
st = http.StatusInternalServerError
case actions.InvalidArgument:
st = http.StatusBadRequest
case actions.NotFound:
st = http.StatusNotFound
case actions.AlreadyExistsErr:
st = http.StatusConflict
case actions.PermissionDenied:
st = http.StatusForbidden
case actions.Unauthenticated:
st = http.StatusUnauthorized
}
errJSON, err := json.Marshal(errRes)
if err != nil {
logger.WithError(err).Error("unable to marshal error")
}
w.WriteHeader(st)
_, _ = w.Write(errJSON)
}