Skip to content

Adding "user not found" page#572

Closed
astrosnat wants to merge 5 commits intomainfrom
fix/usernotfound
Closed

Adding "user not found" page#572
astrosnat wants to merge 5 commits intomainfrom
fix/usernotfound

Conversation

@astrosnat
Copy link
Collaborator

Aimed at fixing issue 569 - on development I can now make users get this page

image

Still needs to be tested on localhost and staging.

Not a complete fix as it doesn't yet handle an edge case where an API can return empty user data.

Adding 2 new checkpoints for breaking down the solution to having a "user not found" page, step by step.
Adding validation logic to `User.jsx`
@pwdel
Copy link
Member

pwdel commented Oct 18, 2025

@astrosnat if we can get #573 done, then I can test this on my local MacOS.

@pwdel
Copy link
Member

pwdel commented Oct 20, 2025

OK, tested on localhost.

image

So basically, you created a frontend page it seems which will display when UserNotFound.

A couple things:

  1. I wonder if there is a way within react to have a sort of, "thing not found," page, e.g. using a 404 Error Code ... but then perhaps give more rich information about why the thing wasn't found?

  2. Just creating a user not found page seems like it could be more generalized. E.g., "Thing Not Found," and then give the response from the backend as to why.

  3. On the backend, on the server you're hitting here:

router.Handle("/v0/userinfo/{username}", securityMiddleware(http.HandlerFunc(publicuser.GetPublicUserResponse))).Methods("GET")

Which then goes to GetPublicUserResponse:

func GetPublicUserResponse(w http.ResponseWriter, r *http.Request) {

Which then just does a query to the database with this function:

func GetPublicUserInfo(db *gorm.DB, username string) models.PublicUser {
	var user models.User
	db.Where("username = ?", username).First(&user)

	return user.PublicUser
}

This is returning the type PublicUser, which is designed to only feed out public information about a user, nothing private, which is defined in the user model here:

type PublicUser struct {

So maybe we could do something like this:

  1. Function to get the public user by Username:
import (
	"context"

	"gorm.io/gorm"
	"socialpredict/models"
)

func GetPublicUserByUsername(ctx context.Context, db *gorm.DB, username string) (models.PublicUser, error) {
	var u models.User
	if err := db.WithContext(ctx).
		Where("username = ?", username).
		First(&u).Error; err != nil {
		return models.PublicUser{}, err
	}
	return u.PublicUser, nil
}

Then the handler;

package usershandlers

import (
	"encoding/json"
	stderrors "errors"
	"net/http"

	"github.com/gorilla/mux"
	"gorm.io/gorm"

	"socialpredict/handlers/usersvc"
	"socialpredict/util"
	"socialpredict/logger"
)

type httpError struct {
	Error string `json:"error"`
}

func GetPublicUserResponse(w http.ResponseWriter, r *http.Request) {
	username := mux.Vars(r)["username"]
	db := util.GetDB()

	pub, err := usersvc.GetPublicUserByUsername(r.Context(), db, username)

	w.Header().Set("Content-Type", "application/json")

	if err != nil {
		// Normal control path: user missing → 404, no error log spam.
		if stderrors.Is(err, gorm.ErrRecordNotFound) {
			w.WriteHeader(http.StatusNotFound)
			_ = json.NewEncoder(w).Encode(httpError{Error: "user not found"})
			return
		}

		// Unexpected DB error → log & 500
		logger.LogError("users", "GetPublicUserResponse", err)
		w.WriteHeader(http.StatusInternalServerError)
		_ = json.NewEncoder(w).Encode(httpError{Error: "internal error"})
		return
	}

	w.WriteHeader(http.StatusOK)
	_ = json.NewEncoder(w).Encode(pub)
}

The idea here is to align with our new simple logging convention.

https://github.com/openpredictionmarkets/socialpredict/blob/main/backend/logger/simplelogging.go

Now how to deal with this on the front end I'm not sure. I know we already have a not found page.

https://github.com/openpredictionmarkets/socialpredict/tree/main/frontend/src/pages/notfound

And here is our user page:

https://github.com/openpredictionmarkets/socialpredict/blob/main/frontend/src/pages/user/User.jsx

So ideally, I would think there would be a way to redirect from the user page to the 404 page with some additional information/specific error contained. However I don't have time to dig into that at the moment, just a thought.

Copy link
Member

@pwdel pwdel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments

@astrosnat
Copy link
Collaborator Author

Update:

Still going to fix 569, but this comes after the backend refactor

@pwdel
Copy link
Member

pwdel commented Nov 4, 2025

Can we close this since the fix is more complicated and since we're refactoring the backend toward a services architecture?

@astrosnat astrosnat closed this Nov 5, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Done in SocialPredict Toward v3.0.0 Nov 5, 2025
@astrosnat astrosnat deleted the fix/usernotfound branch November 5, 2025 03:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Development

Successfully merging this pull request may close these issues.

Default Response upon visiting /user/${WHATEVER} for non-existing user

2 participants