Skip to content

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

Convert to bytebase domain specific error if violating db constraints

天舟922e5bd41d73
diff --git a/backend/server/auth.go b/backend/server/auth.go
index 473bf6090b..be25202e26 100644
--- a/backend/server/auth.go
+++ b/backend/server/auth.go
@@ -21,7 +21,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
user, err := s.PrincipalService.FindPrincipalByEmail(context.Background(), login.Email)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
- return echo.NewHTTPError(http.StatusUnauthorized).SetInternal(err)
+ return echo.NewHTTPError(http.StatusUnauthorized, bytebase.ErrorMessage(err))
}
return echo.NewHTTPError(http.StatusInternalServerError, "failed to authenticate user").SetInternal(err)
}
@@ -69,7 +69,7 @@ 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).SetInternal(err)
+ return echo.NewHTTPError(http.StatusConflict, bytebase.ErrorMessage(err))
}
return echo.NewHTTPError(http.StatusInternalServerError, "failed to signup user").SetInternal(err)
}
diff --git a/backend/server/principal.go b/backend/server/principal.go
index 4b24723b83..84b3c39adb 100644
--- a/backend/server/principal.go
+++ b/backend/server/principal.go
@@ -38,7 +38,7 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
principal, err := s.PrincipalService.FindPrincipalByID(context.Background(), id)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("failed to find principal ID: %v", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusNotFound, bytebase.ErrorMessage(err))
}
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch principal ID: %v", id)).SetInternal(err)
}
@@ -67,7 +67,7 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
principal, err := s.PrincipalService.PatchPrincipalByID(context.Background(), id, principalPatch)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("failed to find principal ID: %v", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusNotFound, bytebase.ErrorMessage(err))
}
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to patch principal ID: %v", id)).SetInternal(err)
}
diff --git a/backend/sqlite/sqlite.go b/backend/sqlite/sqlite.go
index e2247ff797..72e483bc2d 100644
--- a/backend/sqlite/sqlite.go
+++ b/backend/sqlite/sqlite.go
@@ -14,6 +14,7 @@ import (
"strings"
"time"
+ "github.com/bytebase/bytebase"
"github.com/golang-migrate/migrate/v4/source/httpfs"
_ "github.com/mattn/go-sqlite3"
@@ -242,8 +243,8 @@ func FormatError(err error) error {
}
switch err.Error() {
- // case "UNIQUE constraint failed: dial_memberships.dial_id, dial_memberships.user_id":
- // return bytebase.Errorf(bytebase.ECONFLICT, "Dial membership already exists.")
+ case "UNIQUE constraint failed: principal.email":
+ return bytebase.Errorf(bytebase.ECONFLICT, "email already exists")
default:
return err
}