forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi_errors.go
More file actions
34 lines (29 loc) · 788 Bytes
/
api_errors.go
File metadata and controls
34 lines (29 loc) · 788 Bytes
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
package uadmin
import (
"fmt"
"net/http"
)
const (
contentTypeHeader = "Content-Type"
jsonContentType = "application/json; charset=utf-8"
errStatus = "error"
)
func RespondAndLogError(w http.ResponseWriter, r *http.Request, code int, errMsg string, err error) {
// log original error
logError(r, errMsg, err)
if errMsg == "" {
errMsg = fmt.Sprintf("%d. %s", code, http.StatusText(code))
}
w.Header().Set(contentTypeHeader, jsonContentType)
w.WriteHeader(code)
ReturnJSON(w, r, map[string]interface{}{
"status": errStatus,
"err_msg": errMsg,
})
}
func logError(r *http.Request, msg string, err error) {
method := r.Method
uri := r.RequestURI
logMessage := fmt.Sprintf("failed [%s] to [%s], msg: %s", method, uri, msg)
Trail(ERROR, logMessage, err)
}