From 0216f229e0953fb477880842947d0622362d2882 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 2 Jul 2023 22:50:32 +0800 Subject: [PATCH] chore: update error message --- api/v1/auth.go | 26 ++++++++--------- api/v1/common.go | 10 ++----- api/v1/redirector.go | 10 ++++--- api/v1/shortcut.go | 69 ++++++++++++++++++++------------------------ api/v1/url_util.go | 5 ++-- api/v1/user.go | 36 +++++++++++------------ api/v1/workspace.go | 18 ++++++------ 7 files changed, 83 insertions(+), 91 deletions(-) diff --git a/api/v1/auth.go b/api/v1/auth.go index c155db6..f588d25 100644 --- a/api/v1/auth.go +++ b/api/v1/auth.go @@ -28,28 +28,28 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) { ctx := c.Request().Context() signin := &SignInRequest{} if err := json.NewDecoder(c.Request().Body).Decode(signin); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signin request").SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("malformatted signin request, err: %s", err)) } user, err := s.Store.GetUser(ctx, &store.FindUser{ Email: &signin.Email, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by email %s", signin.Email)).SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to find user by email %s", signin.Email)).SetInternal(err) } if user == nil { - return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found with email %s", signin.Email)) + return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("user not found with email %s", signin.Email)) } else if user.RowStatus == store.Archived { - return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with email %s", signin.Email)) + return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("user has been archived with email %s", signin.Email)) } // Compare the stored hashed password, with the hashed version of the password that was received. if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(signin.Password)); err != nil { - return echo.NewHTTPError(http.StatusUnauthorized, "Unmatched email and password").SetInternal(err) + return echo.NewHTTPError(http.StatusUnauthorized, "unmatched email and password") } if err := auth.GenerateTokensAndSetCookies(c, user, secret); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to generate tokens, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, convertUserFromStore(user)) }) @@ -60,20 +60,20 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) { Key: store.WorkspaceDisallowSignUp, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get workspace setting").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get workspace setting, err: %s", err)).SetInternal(err) } if disallowSignUpSetting != nil && disallowSignUpSetting.Value == "true" { - return echo.NewHTTPError(http.StatusForbidden, "Sign up is not allowed") + return echo.NewHTTPError(http.StatusForbidden, "sign up has been disabled") } signup := &SignUpRequest{} if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("malformatted signup request, err: %s", err)).SetInternal(err) } passwordHash, err := bcrypt.GenerateFromPassword([]byte(signup.Password), bcrypt.DefaultCost) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate password hash").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, "failed to generate password hash").SetInternal(err) } create := &store.User{ @@ -83,7 +83,7 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) { } existingUsers, err := s.Store.ListUsers(ctx, &store.FindUser{}) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find existing users").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to find existing users, err: %s", err)).SetInternal(err) } // The first user to sign up is an admin by default. if len(existingUsers) == 0 { @@ -94,11 +94,11 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) { user, err := s.Store.CreateUser(ctx, create) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to create user, err: %s", err)).SetInternal(err) } if err := auth.GenerateTokensAndSetCookies(c, user, secret); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to generate tokens, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, convertUserFromStore(user)) diff --git a/api/v1/common.go b/api/v1/common.go index 2fc252f..a471fb1 100644 --- a/api/v1/common.go +++ b/api/v1/common.go @@ -10,12 +10,6 @@ const ( Archived RowStatus = "ARCHIVED" ) -func (status RowStatus) String() string { - switch status { - case Normal: - return "NORMAL" - case Archived: - return "ARCHIVED" - } - return "" +func (s RowStatus) String() string { + return string(s) } diff --git a/api/v1/redirector.go b/api/v1/redirector.go index 867dc02..2f5ea94 100644 --- a/api/v1/redirector.go +++ b/api/v1/redirector.go @@ -2,6 +2,7 @@ package v1 import ( "encoding/json" + "fmt" "net/http" "net/url" @@ -14,17 +15,18 @@ func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) { g.GET("/*", func(c echo.Context) error { ctx := c.Request().Context() if len(c.ParamValues()) == 0 { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid shortcut name") + return echo.NewHTTPError(http.StatusBadRequest, "invalid shortcut name") } + shortcutName := c.ParamValues()[0] shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{ Name: &shortcutName, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get shortcut, err: %s", err)).SetInternal(err) } if shortcut == nil { - return echo.NewHTTPError(http.StatusNotFound, "Shortcut not found") + return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with name: %s", shortcutName)) } if shortcut.Visibility != store.VisibilityPublic { userID, ok := c.Get(getUserIDContextKey()).(int) @@ -37,7 +39,7 @@ func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) { } if err := s.createShortcutViewActivity(c, shortcut); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to create activity, err: %s", err)).SetInternal(err) } if isValidURLString(shortcut.Link) { diff --git a/api/v1/shortcut.go b/api/v1/shortcut.go index 9e2035f..99a7203 100644 --- a/api/v1/shortcut.go +++ b/api/v1/shortcut.go @@ -27,15 +27,7 @@ const ( ) func (v Visibility) String() string { - switch v { - case VisibilityPublic: - return "PUBLIC" - case VisibilityWorkspace: - return "WORKSPACE" - case VisibilityPrivate: - return "PRIVATE" - } - return "PRIVATE" + return string(v) } type Shortcut struct { @@ -79,11 +71,11 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { ctx := c.Request().Context() userID, ok := c.Get(getUserIDContextKey()).(int) if !ok { - return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } create := &CreateShortcutRequest{} if err := json.NewDecoder(c.Request().Body).Decode(create); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post shortcut request").SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("malformatted post shortcut request, err: %s", err)).SetInternal(err) } shortcut, err := s.Store.CreateShortcut(ctx, &store.Shortcut{ @@ -95,16 +87,16 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { Tag: strings.Join(create.Tags, " "), }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to create shortcut, err: %s", err)).SetInternal(err) } if err := s.createShortcutCreateActivity(ctx, shortcut); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut activity").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to create shortcut activity, err: %s", err)).SetInternal(err) } shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut)) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, shortcutMessage) }) @@ -113,35 +105,35 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { ctx := c.Request().Context() shortcutID, err := strconv.Atoi(c.Param("shortcutId")) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("shortcut ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err) } userID, ok := c.Get(getUserIDContextKey()).(int) if !ok { - return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } currentUser, err := s.Store.GetUser(ctx, &store.FindUser{ ID: &userID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to find user, err: %s", err)).SetInternal(err) } shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{ ID: &shortcutID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to find shortcut, err: %s", err)).SetInternal(err) } if shortcut == nil { - return echo.NewHTTPError(http.StatusNotFound, "Shortcut not found") + return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with id: %d", shortcutID)) } if shortcut.CreatorID != userID && currentUser.Role != store.RoleAdmin { - return echo.NewHTTPError(http.StatusForbidden, "Unauthorized to update shortcut") + return echo.NewHTTPError(http.StatusForbidden, "unauthorized to update shortcut") } patch := &PatchShortcutRequest{} if err := json.NewDecoder(c.Request().Body).Decode(patch); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("failed to decode patch shortcut request, err: %s", err)).SetInternal(err) } if patch.Name != nil { name := strings.ToLower(*patch.Name) @@ -166,12 +158,12 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { } shortcut, err = s.Store.UpdateShortcut(ctx, shortcutUpdate) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to patch shortcut, err: %s", err)).SetInternal(err) } shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut)) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, shortcutMessage) }) @@ -180,14 +172,14 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { ctx := c.Request().Context() userID, ok := c.Get(getUserIDContextKey()).(int) if !ok { - return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } find := &store.FindShortcut{} if creatorIDStr := c.QueryParam("creatorId"); creatorIDStr != "" { creatorID, err := strconv.Atoi(creatorIDStr) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Unwanted creator id string") + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unwanted creator id string: %s", creatorIDStr)) } find.CreatorID = &creatorID } @@ -199,7 +191,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { find.VisibilityList = []store.Visibility{store.VisibilityWorkspace, store.VisibilityPublic} visibleShortcutList, err := s.Store.ListShortcuts(ctx, find) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch shortcut list").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch shortcut list, err: %s", err)).SetInternal(err) } list = append(list, visibleShortcutList...) @@ -207,7 +199,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { find.CreatorID = &userID privateShortcutList, err := s.Store.ListShortcuts(ctx, find) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch private shortcut list").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch private shortcut list, err: %s", err)).SetInternal(err) } list = append(list, privateShortcutList...) @@ -215,7 +207,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { for _, shortcut := range list { shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut)) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err) } shortcutMessageList = append(shortcutMessageList, shortcutMessage) } @@ -226,19 +218,22 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { ctx := c.Request().Context() shortcutID, err := strconv.Atoi(c.Param("id")) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("shortcut id is not a number: %s", c.Param("id"))).SetInternal(err) } shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{ ID: &shortcutID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", shortcutID)).SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch shortcut by id, err: %s", err)).SetInternal(err) + } + if shortcut == nil { + return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with id: %d", shortcutID)) } shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut)) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, shortcutMessage) }) @@ -247,27 +242,27 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { ctx := c.Request().Context() shortcutID, err := strconv.Atoi(c.Param("id")) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("shortcut id is not a number: %s", c.Param("id"))).SetInternal(err) } userID, ok := c.Get(getUserIDContextKey()).(int) if !ok { - return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } currentUser, err := s.Store.GetUser(ctx, &store.FindUser{ ID: &userID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to find user, err: %s", err)).SetInternal(err) } shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{ ID: &shortcutID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch shortcut by id, err: %s", err)).SetInternal(err) } if shortcut == nil { - return echo.NewHTTPError(http.StatusNotFound, "Shortcut not found") + return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with id: %d", shortcutID)) } if shortcut.CreatorID != userID && currentUser.Role != store.RoleAdmin { return echo.NewHTTPError(http.StatusForbidden, "Unauthorized to delete shortcut") @@ -276,7 +271,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { if err := s.Store.DeleteShortcut(ctx, &store.DeleteShortcut{ ID: shortcutID, }); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to delete shortcut, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, true) diff --git a/api/v1/url_util.go b/api/v1/url_util.go index 22013b0..8e65e6d 100644 --- a/api/v1/url_util.go +++ b/api/v1/url_util.go @@ -1,6 +1,7 @@ package v1 import ( + "fmt" "net/http" "github.com/labstack/echo/v4" @@ -12,7 +13,7 @@ func (*APIV1Service) registerURLUtilRoutes(g *echo.Group) { url := c.QueryParam("url") icons, err := favicon.Find(url) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find favicon") + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("failed to find favicon, err: %s", err)) } availableIcons := []*favicon.Icon{} @@ -22,7 +23,7 @@ func (*APIV1Service) registerURLUtilRoutes(g *echo.Group) { } } if len(availableIcons) == 0 { - return echo.NewHTTPError(http.StatusNotFound, "No favicon found") + return echo.NewHTTPError(http.StatusNotFound, "no favicon found") } return c.JSON(http.StatusOK, availableIcons[0].URL) }) diff --git a/api/v1/user.go b/api/v1/user.go index 2e12c12..d10e683 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -89,7 +89,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { ctx := c.Request().Context() list, err := s.Store.ListUsers(ctx, &store.FindUser{}) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user list").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to list users, err: %s", err)).SetInternal(err) } userList := []*User{} @@ -104,14 +104,14 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { ctx := c.Request().Context() userID, ok := c.Get(getUserIDContextKey()).(int) if !ok { - return echo.NewHTTPError(http.StatusUnauthorized, "Missing auth session") + return echo.NewHTTPError(http.StatusUnauthorized, "missing auth session") } user, err := s.Store.GetUser(ctx, &store.FindUser{ ID: &userID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, convertUserFromStore(user)) @@ -121,14 +121,14 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { ctx := c.Request().Context() userID, err := strconv.Atoi(c.Param("id")) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Malformatted user id").SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("user id is not a number: %s", c.Param("id"))).SetInternal(err) } user, err := s.Store.GetUser(ctx, &store.FindUser{ ID: &userID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, convertUserFromStore(user)) @@ -138,19 +138,19 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { ctx := c.Request().Context() userID, err := strconv.Atoi(c.Param("id")) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("user id is not a number: %s", c.Param("id"))).SetInternal(err) } currentUserID, ok := c.Get(getUserIDContextKey()).(int) if !ok { - return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } if currentUserID != userID { - return echo.NewHTTPError(http.StatusForbidden, "Access forbidden for current session user").SetInternal(err) + return echo.NewHTTPError(http.StatusForbidden, "access forbidden for current session user").SetInternal(err) } userPatch := &PatchUserRequest{} if err := json.NewDecoder(c.Request().Body).Decode(userPatch); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("failed to decode request body, err: %s", err)).SetInternal(err) } updateUser := &store.UpdateUser{ @@ -158,7 +158,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { } if userPatch.Email != nil { if !validateEmail(*userPatch.Email) { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid email format") + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid email format: %s", *userPatch.Email)) } updateUser.Email = userPatch.Email @@ -169,7 +169,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { if userPatch.Password != nil && *userPatch.Password != "" { passwordHash, err := bcrypt.GenerateFromPassword([]byte(*userPatch.Password), bcrypt.DefaultCost) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate password hash").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to hash password, err: %s", err)).SetInternal(err) } passwordHashStr := string(passwordHash) @@ -178,7 +178,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { user, err := s.Store.UpdateUser(ctx, updateUser) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to update user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update user, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, convertUserFromStore(user)) @@ -188,30 +188,30 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { ctx := c.Request().Context() currentUserID, ok := c.Get(getUserIDContextKey()).(int) if !ok { - return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } currentUser, err := s.Store.GetUser(ctx, &store.FindUser{ ID: ¤tUserID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to find current session user, err: %s", err)).SetInternal(err) } if currentUser == nil { - return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Current session user not found with ID: %d", currentUserID)).SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("current session user not found with ID: %d", currentUserID)).SetInternal(err) } if currentUser.Role != store.RoleAdmin { - return echo.NewHTTPError(http.StatusForbidden, "Access forbidden for current session user").SetInternal(err) + return echo.NewHTTPError(http.StatusForbidden, "access forbidden for current session user").SetInternal(err) } userID, err := strconv.Atoi(c.Param("id")) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("user id is not a number: %s", c.Param("id"))).SetInternal(err) } if err := s.Store.DeleteUser(ctx, &store.DeleteUser{ ID: userID, }); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete user, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, true) diff --git a/api/v1/workspace.go b/api/v1/workspace.go index 563ae70..d91d766 100644 --- a/api/v1/workspace.go +++ b/api/v1/workspace.go @@ -51,7 +51,7 @@ func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) { Key: store.WorkspaceDisallowSignUp, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get workspace setting") + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to find workspace setting, err: %s", err)).SetInternal(err) } if disallowSignUpSetting != nil { workspaceProfile.DisallowSignUp = disallowSignUpSetting.Value == "true" @@ -64,14 +64,14 @@ func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) { ctx := c.Request().Context() userID, ok := c.Get(getUserIDContextKey()).(int) if !ok { - return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } user, err := s.Store.GetUser(ctx, &store.FindUser{ ID: &userID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to find user, err: %s", err)).SetInternal(err) } if user == nil || user.Role != store.RoleAdmin { return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") @@ -79,10 +79,10 @@ func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) { upsert := &WorkspaceSettingUpsert{} if err := json.NewDecoder(c.Request().Body).Decode(upsert); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post workspace setting request").SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("failed to decode request body, err: %s", err)).SetInternal(err) } if err := upsert.Validate(); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid system setting key or value").SetInternal(err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid request body, err: %s", err)).SetInternal(err) } workspaceSetting, err := s.Store.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{ @@ -90,7 +90,7 @@ func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) { Value: upsert.Value, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert system setting").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to upsert workspace setting, err: %s", err)).SetInternal(err) } return c.JSON(http.StatusOK, convertWorkspaceSettingFromStore(workspaceSetting)) }) @@ -99,14 +99,14 @@ func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) { ctx := c.Request().Context() userID, ok := c.Get(getUserIDContextKey()).(int) if !ok { - return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } user, err := s.Store.GetUser(ctx, &store.FindUser{ ID: &userID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to find user, err: %s", err)).SetInternal(err) } if user == nil || user.Role != store.RoleAdmin { return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") @@ -114,7 +114,7 @@ func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) { list, err := s.Store.ListWorkspaceSettings(ctx, &store.FindWorkspaceSetting{}) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting list").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to list workspace settings, err: %s", err)).SetInternal(err) } workspaceSettingList := []*WorkspaceSetting{}