Improve error handling in web server.

This commit is contained in:
Juliusz Chroboczek
2025-08-13 14:32:33 +02:00
parent e19900b45e
commit ea5ad4a725
2 changed files with 31 additions and 13 deletions
+20 -3
View File
@@ -37,17 +37,34 @@ func checkAdmin(w http.ResponseWriter, r *http.Request) bool {
func checkPasswordAdmin(w http.ResponseWriter, r *http.Request, groupname, user string, wildcard bool) bool { func checkPasswordAdmin(w http.ResponseWriter, r *http.Request, groupname, user string, wildcard bool) bool {
username, password, ok := r.BasicAuth() username, password, ok := r.BasicAuth()
if ok { if ok {
ok, _ := adminMatch(username, password) ok, err := adminMatch(username, password)
if err != nil {
internalError(w, "Admin match: %v", err)
return false
}
if ok { if ok {
return true return true
} }
} }
if ok && !wildcard && username == user { if ok && !wildcard && username == user {
desc, err := group.GetDescription(groupname) desc, err := group.GetDescription(groupname)
if err == nil && desc.Users != nil { if err != nil {
internalError(w,
"Get description for group %v: %v",
groupname, err,
)
return false
}
if desc.Users != nil {
u, ok := desc.Users[user] u, ok := desc.Users[user]
if ok { if ok {
ok, _ := u.Password.Match(password) ok, err := u.Password.Match(password)
if err != nil {
internalError(w,
"Password match: %v", err,
)
return false
}
if ok { if ok {
return true return true
} }
+11 -10
View File
@@ -118,6 +118,11 @@ func notFound(w http.ResponseWriter) {
io.Copy(w, f) io.Copy(w, f)
} }
func internalError(w http.ResponseWriter, format string, args ...any) {
log.Printf(format, args...)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
var ErrIsDirectory = errors.New("is a directory") var ErrIsDirectory = errors.New("is a directory")
func httpError(w http.ResponseWriter, err error) { func httpError(w http.ResponseWriter, err error) {
@@ -141,13 +146,11 @@ func httpError(w http.ResponseWriter, err error) {
http.StatusRequestEntityTooLarge) http.StatusRequestEntityTooLarge)
return return
} }
log.Printf("HTTP server error: %v", err) internalError(w, "HTTP server error: %v", err)
http.Error(w, "Internal server error",
http.StatusInternalServerError)
} }
func methodNotAllowed(w http.ResponseWriter, methods string) { func methodNotAllowed(w http.ResponseWriter, methods string) {
w.Header().Set("Allow", "OPTIONS, " + methods) w.Header().Set("Allow", "OPTIONS, "+methods)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
} }
@@ -409,8 +412,8 @@ func baseURL(r *http.Request) (*url.URL, error) {
func groupStatusHandler(w http.ResponseWriter, r *http.Request) { func groupStatusHandler(w http.ResponseWriter, r *http.Request) {
pth, kind, rest := splitPath(r.URL.Path) pth, kind, rest := splitPath(r.URL.Path)
if kind != ".status" || rest != "" { if kind != ".status" || rest != "" {
http.Error(w, "Internal server error", internalError(w, "groupStatusHandler: this shouldn't happen")
http.StatusInternalServerError) return
} }
name := parseGroupName("/group/", pth) name := parseGroupName("/group/", pth)
if name == "" { if name == "" {
@@ -426,9 +429,7 @@ func groupStatusHandler(w http.ResponseWriter, r *http.Request) {
base, err := baseURL(r) base, err := baseURL(r)
if err != nil { if err != nil {
log.Printf("Parse ProxyURL: %v", err) internalError(w, "Parse ProxyURL: %v", err)
http.Error(w, "Internal server error",
http.StatusInternalServerError)
return return
} }
d := g.Status(false, base) d := g.Status(false, base)
@@ -574,7 +575,7 @@ func recordingsHandler(w http.ResponseWriter, r *http.Request) {
} }
if len(r.URL.Path) < 12 || r.URL.Path[:12] != "/recordings/" { if len(r.URL.Path) < 12 || r.URL.Path[:12] != "/recordings/" {
http.Error(w, "server error", http.StatusInternalServerError) internalError(w, "reconrdingsHandler: this shouldn't happen")
return return
} }