(f handlerFunc, ul UserLevelFunc)
| 394 | } |
| 395 | |
| 396 | func (h *Handler) WebErrors(f handlerFunc, ul UserLevelFunc) http.HandlerFunc { |
| 397 | return func(w http.ResponseWriter, r *http.Request) { |
| 398 | // TODO: factor out this logic shared with Web() |
| 399 | h.handleHTTPError(w, r, func() error { |
| 400 | var status int |
| 401 | start := time.Now() |
| 402 | |
| 403 | defer func() { |
| 404 | if e := recover(); e != nil { |
| 405 | u := getUserSession(h.app.App(), r) |
| 406 | username := "None" |
| 407 | if u != nil { |
| 408 | username = u.Username |
| 409 | } |
| 410 | log.Error("User: %s\n\n%s: %s", username, e, debug.Stack()) |
| 411 | h.errors.InternalServerError.ExecuteTemplate(w, "base", pageForReq(h.app.App(), r)) |
| 412 | status = 500 |
| 413 | } |
| 414 | |
| 415 | log.Info(h.app.ReqLog(r, status, time.Since(start))) |
| 416 | }() |
| 417 | |
| 418 | var session *sessions.Session |
| 419 | var err error |
| 420 | if ul(h.app.App().cfg) != UserLevelNoneType { |
| 421 | session, err = h.sessionStore.Get(r, cookieName) |
| 422 | if err != nil && (ul(h.app.App().cfg) == UserLevelNoneRequiredType || ul(h.app.App().cfg) == UserLevelUserType) { |
| 423 | // Cookie is required, but we can ignore this error |
| 424 | log.Error("Handler: Unable to get session (for user permission %d); ignoring: %v", ul(h.app.App().cfg), err) |
| 425 | } |
| 426 | |
| 427 | _, gotUser := session.Values[cookieUserVal].(*User) |
| 428 | if ul(h.app.App().cfg) == UserLevelNoneRequiredType && gotUser { |
| 429 | to := correctPageFromLoginAttempt(r) |
| 430 | log.Info("Handler: Required NO user, but got one. Redirecting to %s", to) |
| 431 | err := impart.HTTPError{http.StatusFound, to} |
| 432 | status = err.Status |
| 433 | return err |
| 434 | } else if ul(h.app.App().cfg) == UserLevelUserType && !gotUser { |
| 435 | log.Info("Handler: Required a user, but DIDN'T get one. Sending not logged in.") |
| 436 | err := ErrNotLoggedIn |
| 437 | status = err.Status |
| 438 | return err |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // TODO: pass User object to function |
| 443 | err = f(h.app.App(), w, r) |
| 444 | if err == nil { |
| 445 | status = 200 |
| 446 | } else if httpErr, ok := err.(impart.HTTPError); ok { |
| 447 | status = httpErr.Status |
| 448 | if status < 300 || status > 399 { |
| 449 | addSessionFlash(h.app.App(), w, r, httpErr.Message, session) |
| 450 | return impart.HTTPError{http.StatusFound, r.Referer()} |
| 451 | } |
| 452 | } else { |
| 453 | e := fmt.Sprintf("[Web handler] 500: %v", err) |
no test coverage detected