diff --git a/api/endpoints.go b/api/endpoints.go index c27710c..93f6364 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -145,10 +145,15 @@ func handleAddFriend(w http.ResponseWriter, r *http.Request) { return } - response, err := db.AddFriend(uuid, r.Form.Get("username")) - if err != nil { - httpError(w, r, err, http.StatusInternalServerError) - return + success, err := db.AddFriend(uuid, r.Form.Get("username")) + message := "Success" + if !success { + message = err.Error() + } + + response := defs.GenericResponse{ + Success: success, + Message: message, } jsonResponse(w, r, response) diff --git a/db/account.go b/db/account.go index e028155..0423b97 100644 --- a/db/account.go +++ b/db/account.go @@ -276,15 +276,16 @@ func isFriendWith(sourceUsername string, friendUsername string) (bool, error) { } func AddFriend(uuid []byte, friendUsername string) (bool, error) { + // We are making db errors more generic as error is used in the response data to the client. username, err := FetchUsernameFromUUID(uuid); if err != nil { - return false, err + return false, fmt.Errorf("An error occured, are you connected ?") } var doesUserExist int err = handle.QueryRow("SELECT COUNT(*) FROM accounts WHERE username = ?", friendUsername).Scan(&doesUserExist) if err != nil { - return false, err + return false, fmt.Errorf("An error occured, if this persist, please contact an administrators.") } if doesUserExist == 0 { diff --git a/defs/common.go b/defs/common.go new file mode 100644 index 0000000..dcd8887 --- /dev/null +++ b/defs/common.go @@ -0,0 +1,23 @@ +/* + Copyright (C) 2024 Pagefault Games + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package defs + +type GenericResponse struct { + Success bool `json:"success"` + Message string `json:"message"` +}