-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_util.go
46 lines (39 loc) · 946 Bytes
/
common_util.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
package util
import (
"encoding/json"
"net/http"
"strconv"
)
//ReturnHTTPError handles sending out Error response
// TODO Use the Norman API error framework instead
func ReturnHTTPError(w http.ResponseWriter, r *http.Request, httpStatus int, errorMessage string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(httpStatus)
err := AuthError{
Status: strconv.Itoa(httpStatus),
Message: errorMessage,
Type: "error",
}
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
enc.Encode(err)
}
func GetHTTPErrorCode(httpStatus int) string {
switch httpStatus {
case 401:
return "Unauthorized"
case 404:
return "NotFound"
case 403:
return "PermissionDenied"
case 500:
return "ServerError"
}
return "ServerError"
}
//AuthError structure contains the error resource definition
type AuthError struct {
Type string `json:"type"`
Status string `json:"status"`
Message string `json:"message"`
}