mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 20:52:33 +02:00
improved api error handling
This commit is contained in:
parent
0e886f5ddf
commit
55134c8426
4 changed files with 17 additions and 20 deletions
|
@ -104,7 +104,7 @@ func RequireAuth(next http.HandlerFunc) http.HandlerFunc {
|
||||||
func checkToken(w http.ResponseWriter, r *http.Request) (ok bool) {
|
func checkToken(w http.ResponseWriter, r *http.Request) (ok bool) {
|
||||||
tokenCookie, err := r.Cookie("token")
|
tokenCookie, err := r.Cookie("token")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
U.HandleErr(w, r, E.PrependSubject("token", err), http.StatusUnauthorized)
|
U.RespondError(w, E.New("missing token"), http.StatusUnauthorized)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
var claims Claims
|
var claims Claims
|
||||||
|
@ -127,7 +127,7 @@ func checkToken(w http.ResponseWriter, r *http.Request) (ok bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
U.HandleErr(w, r, err, http.StatusForbidden)
|
U.RespondError(w, err, http.StatusForbidden)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ func List(w http.ResponseWriter, r *http.Request) {
|
||||||
switch what {
|
switch what {
|
||||||
case ListRoute:
|
case ListRoute:
|
||||||
if route := listRoute(which); route == nil {
|
if route := listRoute(which); route == nil {
|
||||||
http.Error(w, "not found", http.StatusNotFound)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
U.RespondJSON(w, r, route)
|
U.RespondJSON(w, r, route)
|
||||||
|
@ -59,10 +59,7 @@ func List(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func listRoute(which string) any {
|
func listRoute(which string) any {
|
||||||
if which == "" {
|
if which == "" || which == "all" {
|
||||||
which = "all"
|
|
||||||
}
|
|
||||||
if which == "all" {
|
|
||||||
return config.RoutesByAlias()
|
return config.RoutesByAlias()
|
||||||
}
|
}
|
||||||
routes := config.RoutesByAlias()
|
routes := config.RoutesByAlias()
|
||||||
|
|
|
@ -12,23 +12,22 @@ import (
|
||||||
// http.StatusInternalServerError is used.
|
// http.StatusInternalServerError is used.
|
||||||
//
|
//
|
||||||
// The error is only logged but not returned to the client.
|
// The error is only logged but not returned to the client.
|
||||||
func HandleErr(w http.ResponseWriter, r *http.Request, origErr error, code ...int) {
|
func HandleErr(w http.ResponseWriter, r *http.Request, err error, code ...int) {
|
||||||
if origErr == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
LogError(r).Msg(origErr.Error())
|
LogError(r).Msg(err.Error())
|
||||||
statusCode := http.StatusInternalServerError
|
if len(code) == 0 {
|
||||||
if len(code) > 0 {
|
code = []int{http.StatusInternalServerError}
|
||||||
statusCode = code[0]
|
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(statusCode), statusCode)
|
http.Error(w, http.StatusText(code[0]), code[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func RespondError(w http.ResponseWriter, err error, code ...int) {
|
func RespondError(w http.ResponseWriter, err error, code ...int) {
|
||||||
if len(code) > 0 {
|
if len(code) == 0 {
|
||||||
w.WriteHeader(code[0])
|
code = []int{http.StatusBadRequest}
|
||||||
}
|
}
|
||||||
WriteBody(w, []byte(ansi.StripANSI(err.Error())))
|
http.Error(w, ansi.StripANSI(err.Error()), code[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func ErrMissingKey(k string) error {
|
func ErrMissingKey(k string) error {
|
||||||
|
|
|
@ -8,9 +8,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func reqLogger(r *http.Request, level zerolog.Level) *zerolog.Event {
|
func reqLogger(r *http.Request, level zerolog.Level) *zerolog.Event {
|
||||||
return logging.WithLevel(level).Str("module", "api").
|
return logging.WithLevel(level).
|
||||||
Str("method", r.Method).
|
Str("module", "api").
|
||||||
Str("path", r.RequestURI)
|
Str("remote", r.RemoteAddr).
|
||||||
|
Str("uri", r.Method+" "+r.RequestURI)
|
||||||
}
|
}
|
||||||
|
|
||||||
func LogError(r *http.Request) *zerolog.Event { return reqLogger(r, zerolog.ErrorLevel) }
|
func LogError(r *http.Request) *zerolog.Event { return reqLogger(r, zerolog.ErrorLevel) }
|
||||||
|
|
Loading…
Add table
Reference in a new issue