Skip to content

Commit

Permalink
(BIDS-2811) error message improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelsc committed Dec 11, 2023
1 parent b7587f7 commit 11bab81
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)}
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 11bab81

Please sign in to comment.