Skip to content

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

chore: clean up wrong usage of NotFound error (#1251)

Yilong Liceac62e55093
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.go
index 3991b6032c..f5318f02ae 100644
--- a/plugin/vcs/gitlab/gitlab.go
+++ b/plugin/vcs/gitlab/gitlab.go
@@ -478,7 +478,7 @@ func (p *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtx
return nil, errors.Wrap(err, "GET")
}
- if code == 404 {
+ if code == http.StatusNotFound {
return nil, common.Errorf(common.NotFound, fmt.Errorf("failed to fetch repository members from GitLab instance %s", instanceURL))
} else if code >= 300 {
return nil, fmt.Errorf("failed to read repository members from GitLab instance %s, status code: %d",
diff --git a/server/instance.go b/server/instance.go
index 997ab2ef2f..15d78cacb8 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -90,11 +90,11 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
instance, err := s.store.GetInstanceByID(ctx, id)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
}
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
+ }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, instance); err != nil {
@@ -117,6 +117,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
if err := jsonapi.UnmarshalPayload(c.Request().Body, instancePatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch instance request").SetInternal(err)
}
+
instance, err := s.store.GetInstanceByID(ctx, id)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get instance ID: %v", id)).SetInternal(err)
@@ -124,6 +125,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
if instance == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
}
+
host, port := instance.Host, instance.Port
if instancePatch.Host != nil {
host = *instancePatch.Host
@@ -203,11 +205,11 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
instance, err := s.store.GetInstanceByID(ctx, id)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
}
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
+ }
resultSet := &api.SQLResultSet{}
db, err := getAdminDatabaseDriver(ctx, instance, "", s.l)
@@ -236,11 +238,11 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
instance, err := s.store.GetInstanceByID(ctx, id)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
}
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
+ }
instanceMigration := &api.InstanceMigration{}
db, err := getAdminDatabaseDriver(ctx, instance, "", s.l)
@@ -281,11 +283,11 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
instance, err := s.store.GetInstanceByID(ctx, id)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
}
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
+ }
find := &db.MigrationHistoryFind{ID: &historyID}
driver, err := getAdminDatabaseDriver(ctx, instance, "", s.l)
@@ -339,11 +341,11 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
instance, err := s.store.GetInstanceByID(ctx, id)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
}
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
+ }
find := &db.MigrationHistoryFind{}
databaseStr := c.QueryParams().Get("database")
diff --git a/server/principal.go b/server/principal.go
index 12d3592333..17ee4db9f1 100644
--- a/server/principal.go
+++ b/server/principal.go
@@ -68,11 +68,11 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
principal, err := s.store.GetPrincipalByID(ctx, id)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- 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)
}
+ if principal == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("User ID not found: %d", id))
+ }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, principal); err != nil {
diff --git a/server/sql.go b/server/sql.go
index 9b85854c82..2b086ca4cb 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -86,11 +86,11 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {
instance, err := s.store.GetInstanceByID(ctx, sync.InstanceID)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", sync.InstanceID))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", sync.InstanceID)).SetInternal(err)
}
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", sync.InstanceID))
+ }
resultSet := s.syncEngineVersionAndSchema(ctx, instance)
@@ -123,11 +123,11 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {
instance, err := s.store.GetInstanceByID(ctx, exec.InstanceID)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", exec.InstanceID))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", exec.InstanceID)).SetInternal(err)
}
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", exec.InstanceID))
+ }
start := time.Now().UnixNano()