Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Improve server returned error string

天舟af061cf62a81
diff --git a/backend/server/auth.go b/backend/server/auth.go
index 7f33957885..41b2282094 100644
--- a/backend/server/auth.go
+++ b/backend/server/auth.go
@@ -2,6 +2,7 @@ package server
import (
"context"
+ "fmt"
"net/http"
"github.com/bytebase/bytebase"
@@ -15,30 +16,30 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
g.POST("/auth/login", func(c echo.Context) error {
login := &api.Login{}
if err := jsonapi.UnmarshalPayload(c.Request().Body, login); err != nil {
- return echo.NewHTTPError(http.StatusBadRequest, "Malformatted login request.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted login request").SetInternal(err)
}
user, err := s.PrincipalService.FindPrincipalByEmail(context.Background(), login.Email)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
- return echo.NewHTTPError(http.StatusUnauthorized, bytebase.ErrorMessage(err))
+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Email not found: %s", login.Email))
}
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authenticate user.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authenticate user").SetInternal(err)
}
// Compare the stored hashed password, with the hashed version of the password that was received.
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(login.Password)); err != nil {
// If the two passwords don't match, return a 401 status.
- return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect password.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect password").SetInternal(err)
}
// If password is correct, generate tokens and set cookies.
if err := GenerateTokensAndSetCookies(user, c); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate access token.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate access token").SetInternal(err)
}
if err := jsonapi.MarshalPayload(c.Response().Writer, user); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal login response.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal login response").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
@@ -54,7 +55,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
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)
}
principalCreate := &api.PrincipalCreate{
@@ -69,9 +70,9 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
user, err := s.PrincipalService.CreatePrincipal(context.Background(), principalCreate)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ECONFLICT {
- return echo.NewHTTPError(http.StatusConflict, bytebase.ErrorMessage(err))
+ return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Email already exists: %s", signup.Email))
}
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to signup.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to signup").SetInternal(err)
}
memberCreate := &api.MemberCreate{
@@ -84,7 +85,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
_, err = s.MemberService.CreateMember(context.Background(), memberCreate)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ECONFLICT {
- return echo.NewHTTPError(http.StatusConflict, bytebase.ErrorMessage(err))
+ return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Member already exists: %s.", signup.Email))
}
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to signup.").SetInternal(err)
}
diff --git a/backend/server/member.go b/backend/server/member.go
index 0c4f2c8a85..a918a81b65 100644
--- a/backend/server/member.go
+++ b/backend/server/member.go
@@ -16,7 +16,7 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {
g.POST("/member", func(c echo.Context) error {
memberCreate := &api.MemberCreate{WorkspaceId: api.DEFAULT_WORKPSACE_ID}
if err := jsonapi.UnmarshalPayload(c.Request().Body, memberCreate); err != nil {
- return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create member request.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create member request").SetInternal(err)
}
memberCreate.CreatorId = c.Get(GetPrincipalIdContextKey()).(int)
@@ -24,9 +24,9 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {
member, err := s.MemberService.CreateMember(context.Background(), memberCreate)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ECONFLICT {
- return echo.NewHTTPError(http.StatusConflict, bytebase.ErrorMessage(err))
+ return echo.NewHTTPError(http.StatusConflict, "Member already exists")
}
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create member.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create member").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -41,13 +41,13 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {
g.GET("/member", func(c echo.Context) error {
list, err := s.MemberService.FindMemberList(context.Background(), api.DEFAULT_WORKPSACE_ID)
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch member list.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch member list").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal member list response.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal member list response").SetInternal(err)
}
return nil
@@ -56,7 +56,7 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {
g.PATCH("/member/:id", func(c echo.Context) error {
id, 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("ID is not a number: %s", c.Param("id"))).SetInternal(err)
}
memberPatch := &api.MemberPatch{
@@ -64,21 +64,21 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {
UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
}
if err := jsonapi.UnmarshalPayload(c.Request().Body, memberPatch); err != nil {
- return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch member request.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch member request").SetInternal(err)
}
member, err := s.MemberService.PatchMemberByID(context.Background(), id, memberPatch)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
- return echo.NewHTTPError(http.StatusNotFound, bytebase.ErrorMessage(err))
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Member ID not found: %d", id))
}
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch member ID: %v.", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch member ID: %v", id)).SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
if err := jsonapi.MarshalPayload(c.Response().Writer, member); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal member ID response: %v.", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal member ID response: %v", id)).SetInternal(err)
}
return nil
@@ -87,15 +87,15 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {
g.DELETE("/member/:id", func(c echo.Context) error {
id, 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("ID is not a number: %s", c.Param("id"))).SetInternal(err)
}
err = s.MemberService.DeleteMemberByID(context.Background(), id)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
- return echo.NewHTTPError(http.StatusNotFound, bytebase.ErrorMessage(err))
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Member ID not found: %d", id))
}
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete member ID: %v.", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete member ID: %v", id)).SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
diff --git a/backend/server/principal.go b/backend/server/principal.go
index 9d9cc775a4..b3e01c83ed 100644
--- a/backend/server/principal.go
+++ b/backend/server/principal.go
@@ -16,7 +16,7 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
g.POST("/principal", func(c echo.Context) error {
principalCreate := &api.PrincipalCreate{}
if err := jsonapi.UnmarshalPayload(c.Request().Body, principalCreate); err != nil {
- return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create principal request.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create principal request").SetInternal(err)
}
principalCreate.CreatorId = c.Get(GetPrincipalIdContextKey()).(int)
@@ -27,9 +27,9 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
principal, err := s.PrincipalService.CreatePrincipal(context.Background(), principalCreate)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ECONFLICT {
- return echo.NewHTTPError(http.StatusConflict, bytebase.ErrorMessage(err))
+ return echo.NewHTTPError(http.StatusConflict, "User already exists")
}
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create principal.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create principal").SetInternal(err)
}
if err := jsonapi.MarshalPayload(c.Response().Writer, principal); err != nil {
@@ -44,11 +44,11 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
g.GET("/principal", func(c echo.Context) error {
list, err := s.PrincipalService.FindPrincipalList(context.Background())
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch principal list.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch principal list").SetInternal(err)
}
if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal principal list response.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal principal list response").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
@@ -59,19 +59,19 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
g.GET("/principal/:id", func(c echo.Context) error {
id, 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("ID is not a number: %s", c.Param("id"))).SetInternal(err)
}
principal, err := s.PrincipalService.FindPrincipalByID(context.Background(), id)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
- return echo.NewHTTPError(http.StatusNotFound, bytebase.ErrorMessage(err))
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("User ID not found: %d", id))
}
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch principal ID: %v.", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch principal ID: %v", id)).SetInternal(err)
}
if err := jsonapi.MarshalPayload(c.Response().Writer, principal); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal principal ID response: %v.", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal principal ID response: %v", id)).SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
@@ -82,24 +82,24 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
g.PATCH("/principal/:id", func(c echo.Context) error {
id, 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("ID is not a number: %s", c.Param("id"))).SetInternal(err)
}
principalPatch := &api.PrincipalPatch{UpdaterId: c.Get(GetPrincipalIdContextKey()).(int)}
if err := jsonapi.UnmarshalPayload(c.Request().Body, principalPatch); err != nil {
- return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch principal request.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch principal request").SetInternal(err)
}
principal, err := s.PrincipalService.PatchPrincipalByID(context.Background(), id, principalPatch)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
- return echo.NewHTTPError(http.StatusNotFound, bytebase.ErrorMessage(err))
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("User ID not found: %d", id))
}
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch principal ID: %v.", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch principal ID: %v", id)).SetInternal(err)
}
if err := jsonapi.MarshalPayload(c.Response().Writer, principal); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal principal ID response: %v.", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal principal ID response: %v", id)).SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
diff --git a/backend/store/member.go b/backend/store/member.go
index 78cb28e240..41b065cc7b 100644
--- a/backend/store/member.go
+++ b/backend/store/member.go
@@ -2,6 +2,7 @@ package store
import (
"context"
+ "fmt"
"strings"
"github.com/bytebase/bytebase"
@@ -152,7 +153,7 @@ func findMemberByID(ctx context.Context, tx *Tx, id int) (*api.Member, error) {
if err != nil {
return nil, err
} else if len(list) == 0 {
- return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: "member not found."}
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("member ID not found: %d", id)}
}
return list[0], nil
}
@@ -261,7 +262,7 @@ func patchMember(ctx context.Context, tx *Tx, id int, patch *api.MemberPatch) (*
return &member, nil
}
- return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: "member not found."}
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("member ID not found: %d", id)}
}
// deleteMember permanently deletes a member by ID.
diff --git a/backend/store/principal.go b/backend/store/principal.go
index f3dc059756..8512aae8b3 100644
--- a/backend/store/principal.go
+++ b/backend/store/principal.go
@@ -2,6 +2,7 @@ package store
import (
"context"
+ "fmt"
"strings"
"github.com/bytebase/bytebase"
@@ -56,7 +57,7 @@ func (s *PrincipalService) FindPrincipalByEmail(ctx context.Context, email strin
if err != nil {
return nil, err
} else if len(list) == 0 {
- return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: "principal not found."}
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("principal email not found: %s", email)}
}
return list[0], nil
}
@@ -233,7 +234,7 @@ func findPrincipalByID(ctx context.Context, tx *Tx, id int) (*api.Principal, err
if err != nil {
return nil, err
} else if len(list) == 0 {
- return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: "principal not found."}
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("principal ID not found: %d", id)}
}
return list[0], nil
}
@@ -282,5 +283,5 @@ func patchPrincipal(ctx context.Context, tx *Tx, id int, patch *api.PrincipalPat
return &principal, nil
}
- return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: "principal not found."}
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("principal ID not found: %d", id)}
}
diff --git a/backend/store/sqlite.go b/backend/store/sqlite.go
index 3163095a0f..9845131714 100644
--- a/backend/store/sqlite.go
+++ b/backend/store/sqlite.go
@@ -246,7 +246,7 @@ func FormatError(err error) error {
switch err.Error() {
case "UNIQUE constraint failed: principal.email":
- return bytebase.Errorf(bytebase.ECONFLICT, "Email already exists.")
+ return bytebase.Errorf(bytebase.ECONFLICT, "email already exists")
default:
return err
}