From 11bab814be03c6938bd4f0c08d5ae142e075592b Mon Sep 17 00:00:00 2001 From: Manuel <5877862+manuelsc@users.noreply.github.com> Date: Mon, 11 Dec 2023 14:24:41 +0100 Subject: [PATCH] (BIDS-2811) error message improvement --- handlers/user.go | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/handlers/user.go b/handlers/user.go index af50fb65f2..09c57cdbda 100644 --- a/handlers/user.go +++ b/handlers/user.go @@ -1728,12 +1728,18 @@ func internUserNotificationsSubscribe(event, filter string, threshold float64, w errFields["event_name"] = eventName - if valid, err := isValidSubscriptionFilter(user.UserID, eventName, filter); err != nil || !valid { - utils.LogError(err, "error invalid filter: not pubkey or client for subscription", 0, errFields) + valid, err := isValidSubscriptionFilter(user.UserID, eventName, filter) + if err != nil { + utils.LogError(err, "error validating filter", 0, errFields) ErrorOrJSONResponse(w, r, "Internal server error", http.StatusInternalServerError) return false } + if !valid { + ErrorOrJSONResponse(w, r, "Invalid filter, only pubkey, client or machine name is valid.", http.StatusBadRequest) + return false + } + userPremium := getUserPremium(r) filterWatchlist := db.WatchlistFilter{ @@ -1917,13 +1923,19 @@ func internUserNotificationsUnsubscribe(event, filter string, w http.ResponseWri } errFields["event_name"] = eventName + valid, err := isValidSubscriptionFilter(user.UserID, eventName, filter) - if valid, err := isValidSubscriptionFilter(user.UserID, eventName, filter); err != nil || !valid { - utils.LogError(err, "error invalid filter: not pubkey or client for unsubscription", 0, errFields) + if err != nil { + utils.LogError(err, "error validating filter", 0, errFields) ErrorOrJSONResponse(w, r, "Internal server error", http.StatusInternalServerError) return false } + if !valid { + ErrorOrJSONResponse(w, r, "Invalid filter, only pubkey, client or machine name is valid.", http.StatusBadRequest) + return false + } + filterWatchlist := db.WatchlistFilter{ UserId: user.UserID, Validators: nil, @@ -2001,8 +2013,9 @@ func UserNotificationsUnsubscribe(w http.ResponseWriter, r *http.Request) { return } - if valid, err := isValidSubscriptionFilter(user.UserID, eventName, filter); err != nil || !valid { - errMsg := fmt.Errorf("error invalid filter, not pubkey or client") + valid, err := isValidSubscriptionFilter(user.UserID, eventName, filter) + if err != nil { + errMsg := fmt.Errorf("error validating filter") errFields := map[string]interface{}{ "filter": filter, "filter_len": len(filter)} @@ -2012,6 +2025,11 @@ func UserNotificationsUnsubscribe(w http.ResponseWriter, r *http.Request) { return } + if !valid { + ErrorOrJSONResponse(w, r, "Invalid filter, only pubkey, client or machine name is valid.", http.StatusBadRequest) + return + } + filterLen := len(filter) if filterLen == 0 && !types.IsUserIndexed(eventName) { // no filter = add all my watched validators @@ -2093,7 +2111,7 @@ func isValidSubscriptionFilter(userID uint64, eventName types.EventName, filter } // While the above works fine for active machines (adding a new notification to an active machine) - // It does not work for a machine that is offline and where the user wants to unsubscribe from this machine. + // It does not work for a machine that is offline and where the user wants to subscribe/unsubscribe from this machine. // So check the db for any machine names as well if !isValidMachine { machines := make([]string, 0)