fix: align session error responses

This commit is contained in:
OpenClaw Agent
2026-09-21 16:16:14 +08:00
parent 5b2110eb98
commit b593eeaddf
+11 -1
View File
@@ -109,6 +109,12 @@ func writeJSON(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(v)
}
func writeJSONError(w http.ResponseWriter, status int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]string{"error": message})
}
func qpath(r *http.Request) string {
return strings.Trim(strings.ReplaceAll(r.URL.Query().Get("filepath"), "\\", "/"), "/")
}
@@ -263,9 +269,13 @@ func (s *Server) session(w http.ResponseWriter, r *http.Request) {
return
}
user, pwd := r.FormValue("user"), r.FormValue("pwd")
if user == "" && pwd == "" {
writeJSONError(w, http.StatusBadRequest, "invalid parameter")
return
}
a := s.Auth.Authenticate(user, pwd)
if a == nil {
http.Error(w, "invalid account", http.StatusUnauthorized)
writeJSONError(w, http.StatusNotFound, "user or password is wrong")
return
}
token := auth.NewToken()