From 82f4e54931fe28f0c5f3ff579b334584ee5dac98 Mon Sep 17 00:00:00 2001 From: Anant Prakash Date: Sat, 30 Jun 2018 18:50:43 +0530 Subject: [PATCH] Handler sql.ErrNoRows and avoid returning nil update federation GetProfile as well Signed-off-by: Anant Prakash --- .../clientapi/auth/storage/accounts/storage.go | 8 +------- .../matrix-org/dendrite/clientapi/routing/profile.go | 12 ++++++------ .../dendrite/federationapi/routing/profile.go | 8 +++++++- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go index cebaecc7c..d696eb657 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go @@ -98,13 +98,7 @@ func (d *Database) GetAccountByPassword( func (d *Database) GetProfileByLocalpart( ctx context.Context, localpart string, ) (*authtypes.Profile, error) { - profile, err := d.profiles.selectProfileByLocalpart(ctx, localpart) - // err == sql.ErrNoRows signifies that this profile is not in the db - if err != nil && err != sql.ErrNoRows { - return nil, err - } - - return profile, nil + return d.profiles.selectProfileByLocalpart(ctx, localpart) } // SetAvatarURL updates the avatar URL of the profile associated with the given diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go index cada11bfb..cc1180b0f 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go @@ -16,6 +16,7 @@ package routing import ( "context" + "database/sql" "net/http" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" @@ -57,6 +58,7 @@ func GetProfile( } } +// getProfileByUserID returns the profile for userID, otherwise returns an error response func getProfileByUserID( req *http.Request, accountDB *accounts.Database, userID string, ) (*authtypes.Profile, *util.JSONResponse) { @@ -67,16 +69,14 @@ func getProfileByUserID( } profile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart) - if err != nil { - resErr := httputil.LogThenError(req, err) - return nil, &resErr - } - - if profile == nil { + if err == sql.ErrNoRows { return nil, &util.JSONResponse{ Code: http.StatusNotFound, JSON: jsonerror.NotFound("no profile information for this user or this user does not exist"), } + } else if err != nil { + resErr := httputil.LogThenError(req, err) + return nil, &resErr } return profile, nil diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go b/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go index a9cbfca4a..c52051380 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go @@ -15,6 +15,7 @@ package routing import ( + "database/sql" "net/http" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" @@ -52,7 +53,12 @@ func GetProfile( } profile, err := accountDB.GetProfileByLocalpart(httpReq.Context(), localpart) - if err != nil { + if err == sql.ErrNoRows { + return util.JSONResponse{ + Code: http.StatusNotFound, + JSON: jsonerror.NotFound("no profile information for this user or this user does not exist"), + } + } else if err != nil { return httputil.LogThenError(httpReq, err) }