From 6741fd7264c5ac1e2326d26c2283d5f3e7337083 Mon Sep 17 00:00:00 2001 From: Justin Sherrill Date: Mon, 18 Nov 2024 07:30:32 -0500 Subject: [PATCH] Fixes 4925: fixup DBErrorToApi funcs --- pkg/dao/admin_tasks.go | 10 ++--- pkg/dao/admin_tasks_test.go | 2 +- pkg/dao/environments.go | 2 +- pkg/dao/package_groups.go | 2 +- pkg/dao/repository_configs.go | 73 +++++++++++++++++------------------ pkg/dao/rpms.go | 2 +- pkg/dao/snapshots.go | 34 ++++++++++++---- pkg/dao/snapshots_test.go | 2 +- pkg/dao/task_info.go | 36 +++++++++++++---- pkg/dao/templates.go | 66 +++++++++++++++---------------- pkg/dao/templates_test.go | 2 +- 11 files changed, 134 insertions(+), 97 deletions(-) diff --git a/pkg/dao/admin_tasks.go b/pkg/dao/admin_tasks.go index 3ff2578f2..bbf38e7a6 100644 --- a/pkg/dao/admin_tasks.go +++ b/pkg/dao/admin_tasks.go @@ -34,11 +34,7 @@ func (a adminTaskInfoDaoImpl) Fetch(ctx context.Context, id string) (api.AdminTa taskInfoResponse := api.AdminTaskInfoResponse{} if result.Error != nil { - if result.Error == gorm.ErrRecordNotFound { - return taskInfoResponse, &ce.DaoError{NotFound: true, Message: "Could not find task with UUID " + id} - } else { - return taskInfoResponse, DBErrorToApi(result.Error) - } + return taskInfoResponse, TasksDBToApiError(result.Error, &id) } if taskInfo.Typename == payloads.Snapshot { @@ -95,13 +91,13 @@ func (a adminTaskInfoDaoImpl) List( result := filteredDB.Model(&tasks).Count(&totalTasks) if result.Error != nil { - return api.AdminTaskInfoCollectionResponse{}, totalTasks, DBErrorToApi(filteredDB.Error) + return api.AdminTaskInfoCollectionResponse{}, totalTasks, TasksDBToApiError(filteredDB.Error, nil) } result = filteredDB.Offset(pageData.Offset).Limit(pageData.Limit).Order(order).Find(&tasks) if result.Error != nil { - return api.AdminTaskInfoCollectionResponse{}, totalTasks, DBErrorToApi(filteredDB.Error) + return api.AdminTaskInfoCollectionResponse{}, totalTasks, TasksDBToApiError(filteredDB.Error, nil) } taskResponses := convertAdminTaskInfoToResponses(tasks) diff --git a/pkg/dao/admin_tasks_test.go b/pkg/dao/admin_tasks_test.go index 5e81e4f93..6c50335aa 100644 --- a/pkg/dao/admin_tasks_test.go +++ b/pkg/dao/admin_tasks_test.go @@ -89,7 +89,7 @@ func (suite *AdminTaskSuite) TestFetchInvalidUUID() { daoError, ok := err.(*ce.DaoError) assert.True(t, ok) assert.True(t, daoError.NotFound) - assert.Equal(t, "Could not find task with UUID "+invalidUUID, daoError.Message) + assert.Equal(t, "Task with UUID "+invalidUUID+" not found", daoError.Message) } // Occurs if repository/repository configuration associated with task is deleted diff --git a/pkg/dao/environments.go b/pkg/dao/environments.go index 206c76524..19ad4ff87 100644 --- a/pkg/dao/environments.go +++ b/pkg/dao/environments.go @@ -38,7 +38,7 @@ func (r environmentDaoImpl) List(ctx context.Context, orgID string, repositoryCo if err != nil { return api.RepositoryEnvironmentCollectionResponse{}, totalEnvironments, - DBErrorToApi(err) + RepositoryDBErrorToApi(err, &repositoryConfigUUID) } return api.RepositoryEnvironmentCollectionResponse{}, totalEnvironments, diff --git a/pkg/dao/package_groups.go b/pkg/dao/package_groups.go index ec4d91766..43a964442 100644 --- a/pkg/dao/package_groups.go +++ b/pkg/dao/package_groups.go @@ -40,7 +40,7 @@ func (r packageGroupDaoImpl) List(ctx context.Context, orgID string, repositoryC if err != nil { return api.RepositoryPackageGroupCollectionResponse{}, totalPackageGroups, - DBErrorToApi(err) + RepositoryDBErrorToApi(err, &repositoryConfigUUID) } return api.RepositoryPackageGroupCollectionResponse{}, totalPackageGroups, diff --git a/pkg/dao/repository_configs.go b/pkg/dao/repository_configs.go index bbfb2e5ab..cb2772451 100644 --- a/pkg/dao/repository_configs.go +++ b/pkg/dao/repository_configs.go @@ -39,7 +39,7 @@ func GetRepositoryConfigDao(db *gorm.DB, pulpClient pulp_client.PulpClient) Repo } } -func DBErrorToApi(e error) *ce.DaoError { +func RepositoryDBErrorToApi(e error, uuid *string) *ce.DaoError { var dupKeyName string if e == nil { return nil @@ -69,9 +69,21 @@ func DBErrorToApi(e error) *ce.DaoError { return &ce.DaoError{BadValidation: dbError.Validation, Message: dbError.Message} } - daoErr := ce.DaoError{ - Message: "Database Error", - NotFound: ce.HttpCodeForDaoError(e) == 404, // Check if isNotFoundError + daoErr := ce.DaoError{} + if errors.Is(e, gorm.ErrRecordNotFound) { + msg := "Repository not found" + if uuid != nil { + msg = fmt.Sprintf("Repository with UUID %s not found", *uuid) + } + daoErr = ce.DaoError{ + Message: msg, + NotFound: true, + } + } else { + daoErr = ce.DaoError{ + Message: "Database Error", + NotFound: ce.HttpCodeForDaoError(e) == 404, // Check if isNotFoundError + } } daoErr.Wrap(e) @@ -98,13 +110,13 @@ func (r repositoryConfigDaoImpl) Create(ctx context.Context, newRepoReq api.Repo if newRepo.URL == "" || newRepo.Origin == config.OriginUpload { if err := r.db.WithContext(ctx).Create(&newRepo).Error; err != nil { - return api.RepositoryResponse{}, DBErrorToApi(err) + return api.RepositoryResponse{}, RepositoryDBErrorToApi(err, nil) } } else if newRepo.URL != "" { cleanedUrl := models.CleanupURL(newRepo.URL) // Repo configs with the same URL share a repository object if err := r.db.WithContext(ctx).Where("url = ?", cleanedUrl).FirstOrCreate(&newRepo).Error; err != nil { - return api.RepositoryResponse{}, DBErrorToApi(err) + return api.RepositoryResponse{}, RepositoryDBErrorToApi(err, nil) } } if newRepoReq.OrgID != nil { @@ -116,13 +128,13 @@ func (r repositoryConfigDaoImpl) Create(ctx context.Context, newRepoReq api.Repo newRepoConfig.RepositoryUUID = newRepo.Base.UUID if err := r.db.WithContext(ctx).Create(&newRepoConfig).Error; err != nil { - return api.RepositoryResponse{}, DBErrorToApi(err) + return api.RepositoryResponse{}, RepositoryDBErrorToApi(err, nil) } // reload the repoConfig to fetch repository info too newRepoConfig, err := r.fetchRepoConfig(ctx, newRepoConfig.OrgID, newRepoConfig.UUID, false) if err != nil { - return api.RepositoryResponse{}, DBErrorToApi(err) + return api.RepositoryResponse{}, RepositoryDBErrorToApi(err, nil) } var created api.RepositoryResponse @@ -208,14 +220,14 @@ func (r repositoryConfigDaoImpl) bulkCreate(tx *gorm.DB, newRepositories []api.R } if err != nil { - dbErr = DBErrorToApi(err) + dbErr = RepositoryDBErrorToApi(err, nil) errorList[i] = dbErr tx.RollbackTo("beforecreate") continue } newRepoConfigs[i].RepositoryUUID = newRepos[i].UUID if err := tx.Create(&newRepoConfigs[i]).Error; err != nil { - dbErr = DBErrorToApi(err) + dbErr = RepositoryDBErrorToApi(err, nil) errorList[i] = dbErr tx.RollbackTo("beforecreate") continue @@ -556,10 +568,7 @@ func (r repositoryConfigDaoImpl) fetchRepoConfig(ctx context.Context, orgID stri First(&found) if result.Error != nil { - if result.Error == gorm.ErrRecordNotFound { - return found, &ce.DaoError{NotFound: true, Message: "Could not find repository with UUID " + uuid} - } - return found, DBErrorToApi(result.Error) + return found, RepositoryDBErrorToApi(result.Error, nil) } return found, nil } @@ -575,10 +584,7 @@ func (r repositoryConfigDaoImpl) FetchByRepoUuid(ctx context.Context, orgID stri First(&repoConfig) if result.Error != nil { - if result.Error == gorm.ErrRecordNotFound { - return repo, &ce.DaoError{NotFound: true, Message: "Could not find repository with UUID " + repoUuid} - } - return repo, DBErrorToApi(result.Error) + return repo, RepositoryDBErrorToApi(result.Error, &repoUuid) } ModelToApiFields(repoConfig, &repo) @@ -594,11 +600,7 @@ func (r repositoryConfigDaoImpl) FetchWithoutOrgID(ctx context.Context, uuid str First(&found) if result.Error != nil { - if result.Error == gorm.ErrRecordNotFound { - return repo, &ce.DaoError{NotFound: true, Message: "Could not find repository with UUID " + uuid} - } else { - return repo, DBErrorToApi(result.Error) - } + return repo, RepositoryDBErrorToApi(result.Error, &uuid) } ModelToApiFields(found, &repo) return repo, nil @@ -633,7 +635,7 @@ func (r repositoryConfigDaoImpl) Update(ctx context.Context, orgID, uuid string, cleanedUrl := models.CleanupURL(*repoParams.URL) err = tx.FirstOrCreate(&repo, "url = ?", cleanedUrl).Error if err != nil { - return DBErrorToApi(err) + return RepositoryDBErrorToApi(err, nil) } repoConfig.RepositoryUUID = repo.UUID updatedUrl = repoConfig.Repository.URL != cleanedUrl @@ -641,7 +643,7 @@ func (r repositoryConfigDaoImpl) Update(ctx context.Context, orgID, uuid string, repoConfig.Repository = models.Repository{} if err := tx.Model(&repoConfig).Omit("LastSnapshot").Updates(repoConfig.MapForUpdate()).Error; err != nil { - return DBErrorToApi(err) + return RepositoryDBErrorToApi(err, nil) } repositoryResponse := api.RepositoryResponse{} @@ -669,7 +671,7 @@ func (r repositoryConfigDaoImpl) Update(ctx context.Context, orgID, uuid string, repoConfig.Repository = models.Repository{} if err := r.db.WithContext(ctx).Model(&repoConfig).Omit("LastSnapshot").Updates(repoConfig.MapForUpdate()).Error; err != nil { - return updatedUrl, DBErrorToApi(err) + return updatedUrl, RepositoryDBErrorToApi(err, nil) } return updatedUrl, nil @@ -746,10 +748,7 @@ func (r repositoryConfigDaoImpl) Delete(ctx context.Context, orgID string, uuid err := r.db.WithContext(ctx).Unscoped().Where("uuid = ? AND org_id = ?", UuidifyString(uuid), orgID).First(&repoConfig).Error if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return &ce.DaoError{NotFound: true, Message: "Could not find repository with UUID " + uuid} - } - return DBErrorToApi(err) + return RepositoryDBErrorToApi(err, &uuid) } if err = r.db.WithContext(ctx).Unscoped().Delete(&repoConfig).Error; err != nil { @@ -796,14 +795,14 @@ func (r repositoryConfigDaoImpl) bulkDelete(ctx context.Context, tx *gorm.DB, or var repoConfig models.RepositoryConfiguration if repoConfig, err = r.fetchRepoConfig(ctx, orgID, uuids[i], false); err != nil { - dbErr = DBErrorToApi(err) + dbErr = RepositoryDBErrorToApi(err, nil) errors[i] = dbErr tx.RollbackTo(save) continue } if err = tx.Delete(&repoConfig).Error; err != nil { - dbErr = DBErrorToApi(err) + dbErr = RepositoryDBErrorToApi(err, nil) errors[i] = dbErr tx.RollbackTo(save) continue @@ -916,7 +915,7 @@ func (r repositoryConfigDaoImpl) bulkImport(tx *gorm.DB, reposToImport []api.Rep Where("repositories.url = ? and repository_configurations.org_id = ?", cleanedUrl, newRepoConfigs[i].OrgID). First(&existingRepo).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - dbErr = DBErrorToApi(err) + dbErr = RepositoryDBErrorToApi(err, nil) errorList[i] = dbErr tx.RollbackTo("beforeimport") continue @@ -935,14 +934,14 @@ func (r repositoryConfigDaoImpl) bulkImport(tx *gorm.DB, reposToImport []api.Rep } else { // no existing repo, create (or find) repo and create repo config if err = tx.Where("url = ?", cleanedUrl).FirstOrCreate(&newRepos[i]).Error; err != nil { - dbErr = DBErrorToApi(err) + dbErr = RepositoryDBErrorToApi(err, nil) errorList[i] = dbErr tx.RollbackTo("beforeimport") continue } newRepoConfigs[i].RepositoryUUID = newRepos[i].UUID if err = tx.Create(&newRepoConfigs[i]).Error; err != nil { - dbErr = DBErrorToApi(err) + dbErr = RepositoryDBErrorToApi(err, nil) errorList[i] = dbErr tx.RollbackTo("beforeimport") continue @@ -1293,7 +1292,7 @@ func (r repositoryConfigDaoImpl) validateName(ctx context.Context, orgId string, } if err := query.Find(&found).Error; err != nil { response.Valid = false - return DBErrorToApi(err) + return RepositoryDBErrorToApi(err, nil) } if found.UUID != "" { @@ -1325,7 +1324,7 @@ func (r repositoryConfigDaoImpl) validateUrl(ctx context.Context, orgId string, if err := query.Find(&found).Error; err != nil { response.URL.Valid = false - return DBErrorToApi(err) + return RepositoryDBErrorToApi(err, nil) } if found.UUID != "" { diff --git a/pkg/dao/rpms.go b/pkg/dao/rpms.go index 92a42a0c4..0b4465b54 100644 --- a/pkg/dao/rpms.go +++ b/pkg/dao/rpms.go @@ -50,7 +50,7 @@ func (r *rpmDaoImpl) List( if err != nil { return api.RepositoryRpmCollectionResponse{}, totalRpms, - DBErrorToApi(err) + RepositoryDBErrorToApi(err, &repositoryConfigUUID) } return api.RepositoryRpmCollectionResponse{}, totalRpms, diff --git a/pkg/dao/snapshots.go b/pkg/dao/snapshots.go index c6166bdd6..8e239897d 100644 --- a/pkg/dao/snapshots.go +++ b/pkg/dao/snapshots.go @@ -2,6 +2,7 @@ package dao import ( "context" + "errors" "fmt" "regexp" "strings" @@ -73,13 +74,7 @@ func (sDao *snapshotDaoImpl) List( First(&repoConfig) if result.Error != nil { - if result.Error == gorm.ErrRecordNotFound { - return api.SnapshotCollectionResponse{}, totalSnaps, &ce.DaoError{ - Message: "Could not find repository with UUID " + repoConfigUUID, - NotFound: true, - } - } - return api.SnapshotCollectionResponse{}, totalSnaps, DBErrorToApi(result.Error) + return api.SnapshotCollectionResponse{}, totalSnaps, RepositoryDBErrorToApi(result.Error, &repoConfigUUID) } sortMap := map[string]string{ "created_at": "created_at", @@ -208,6 +203,31 @@ func (sDao *snapshotDaoImpl) fetch(ctx context.Context, uuid string) (models.Sna return snapshot, nil } +func SnapshotsDBToApiError(e error, uuid *string) *ce.DaoError { + if e == nil { + return nil + } + + daoError := ce.DaoError{} + if errors.Is(e, gorm.ErrRecordNotFound) { + msg := "Task not found" + if uuid != nil { + msg = fmt.Sprintf("Snapshot with UUID %s not found", *uuid) + } + daoError = ce.DaoError{ + Message: msg, + NotFound: true, + } + } else { + daoError = ce.DaoError{ + Message: e.Error(), + NotFound: ce.HttpCodeForDaoError(e) == 404, // Check if isNotFoundError + } + } + daoError.Wrap(e) + return &daoError +} + func (sDao *snapshotDaoImpl) GetRepositoryConfigurationFile(ctx context.Context, orgID, snapshotUUID string, isLatest bool) (string, error) { var repoID string snapshot, err := sDao.fetch(ctx, snapshotUUID) diff --git a/pkg/dao/snapshots_test.go b/pkg/dao/snapshots_test.go index b19dbba81..7ebd74972 100644 --- a/pkg/dao/snapshots_test.go +++ b/pkg/dao/snapshots_test.go @@ -434,7 +434,7 @@ func (s *SnapshotsSuite) TestListNotFoundBadOrgId() { assert.True(t, daoError.NotFound) assert.Equal(t, int64(0), total) assert.Equal(t, 0, len(collection.Data)) - assert.ErrorContains(t, err, "Could not find repository with UUID "+rConfig.UUID) + assert.ErrorContains(t, err, "Repository with UUID "+rConfig.UUID+" not found") } func (s *SnapshotsSuite) TestFetchForRepoUUID() { diff --git a/pkg/dao/task_info.go b/pkg/dao/task_info.go index 9fff385d9..0fc345427 100644 --- a/pkg/dao/task_info.go +++ b/pkg/dao/task_info.go @@ -2,6 +2,7 @@ package dao import ( "context" + "errors" "fmt" "time" @@ -57,11 +58,7 @@ func (t taskInfoDaoImpl) Fetch(ctx context.Context, orgID string, id string) (ap Where("t.id = ? AND t.org_id in (?) AND rc.deleted_at is NULL", UuidifyString(id), orgIDs).First(&taskInfo) if result.Error != nil { - if result.Error == gorm.ErrRecordNotFound { - return taskInfoResponse, &ce.DaoError{NotFound: true, Message: "Could not find task with UUID " + id} - } else { - return taskInfoResponse, DBErrorToApi(result.Error) - } + return taskInfoResponse, TasksDBToApiError(result.Error, &id) } taskInfoModelToApiFields(&taskInfo, &taskInfoResponse) return taskInfoResponse, nil @@ -115,14 +112,14 @@ func (t taskInfoDaoImpl) List( filteredDB.Model(&tasks).Count(&totalTasks) if filteredDB.Error != nil { - return api.TaskInfoCollectionResponse{}, totalTasks, DBErrorToApi(filteredDB.Error) + return api.TaskInfoCollectionResponse{}, totalTasks, TasksDBToApiError(filteredDB.Error, nil) } // Most recently queued (created) first filteredDB.Order("queued_at DESC").Offset(pageData.Offset).Limit(pageData.Limit).Find(&tasks) if filteredDB.Error != nil { - return api.TaskInfoCollectionResponse{}, totalTasks, DBErrorToApi(filteredDB.Error) + return api.TaskInfoCollectionResponse{}, totalTasks, TasksDBToApiError(filteredDB.Error, nil) } taskResponses := convertTaskInfoToResponses(tasks) return api.TaskInfoCollectionResponse{Data: taskResponses}, totalTasks, nil @@ -157,6 +154,31 @@ func (t taskInfoDaoImpl) Cleanup(ctx context.Context) error { return nil } +func TasksDBToApiError(e error, uuid *string) *ce.DaoError { + if e == nil { + return nil + } + + daoError := ce.DaoError{} + if errors.Is(e, gorm.ErrRecordNotFound) { + msg := "Task not found" + if uuid != nil { + msg = fmt.Sprintf("Task with UUID %s not found", *uuid) + } + daoError = ce.DaoError{ + Message: msg, + NotFound: true, + } + } else { + daoError = ce.DaoError{ + Message: e.Error(), + NotFound: ce.HttpCodeForDaoError(e) == 404, // Check if isNotFoundError + } + } + daoError.Wrap(e) + return &daoError +} + func (t taskInfoDaoImpl) FetchActiveTasks(ctx context.Context, orgID string, objectUUID string, taskTypes ...string) ([]string, error) { taskInfo := make([]models.TaskInfo, 0) result := t.db.WithContext(ctx). diff --git a/pkg/dao/templates.go b/pkg/dao/templates.go index 01145f4ce..dba11ae61 100644 --- a/pkg/dao/templates.go +++ b/pkg/dao/templates.go @@ -2,6 +2,7 @@ package dao import ( "context" + "errors" "fmt" "time" @@ -21,7 +22,7 @@ type templateDaoImpl struct { db *gorm.DB } -func (t templateDaoImpl) DBToApiError(e error) *ce.DaoError { +func (t templateDaoImpl) DBToApiError(e error, uuid *string) *ce.DaoError { var dupKeyName string if e == nil { return nil @@ -46,10 +47,21 @@ func (t templateDaoImpl) DBToApiError(e error) *ce.DaoError { daoError.Wrap(e) return &daoError } - - daoError := ce.DaoError{ - Message: e.Error(), - NotFound: ce.HttpCodeForDaoError(e) == 404, // Check if isNotFoundError + daoError := ce.DaoError{} + if errors.Is(e, gorm.ErrRecordNotFound) { + msg := "Template not found" + if uuid != nil { + msg = fmt.Sprintf("Template with UUID %s not found", *uuid) + } + daoError = ce.DaoError{ + Message: msg, + NotFound: true, + } + } else { + daoError = ce.DaoError{ + Message: e.Error(), + NotFound: ce.HttpCodeForDaoError(e) == 404, // Check if isNotFoundError + } } daoError.Wrap(e) return &daoError @@ -78,7 +90,7 @@ func (t templateDaoImpl) create(ctx context.Context, tx *gorm.DB, reqTemplate ap templatesCreateApiToModel(reqTemplate, &modelTemplate) err := tx.Create(&modelTemplate).Error if err != nil { - return api.TemplateResponse{}, t.DBToApiError(err) + return api.TemplateResponse{}, t.DBToApiError(err, nil) } // Associate the template to repositories @@ -160,7 +172,7 @@ func (t templateDaoImpl) insertTemplateRepoConfigsAndSnapshots(tx *gorm.DB, ctx DoUpdates: clause.AssignmentColumns([]string{"deleted_at", "snapshot_uuid"}), }).Create(&templateRepoConfigs).Error if err != nil { - return t.DBToApiError(err) + return t.DBToApiError(err, nil) } return nil } @@ -170,7 +182,7 @@ func (t templateDaoImpl) DeleteTemplateRepoConfigs(ctx context.Context, template Delete(models.TemplateRepositoryConfiguration{}).Error if err != nil { - return t.DBToApiError(err) + return t.DBToApiError(err, nil) } return nil } @@ -180,7 +192,7 @@ func (t templateDaoImpl) softDeleteTemplateRepoConfigs(tx *gorm.DB, templateUUID Delete(&models.TemplateRepositoryConfiguration{}).Error if err != nil { - return t.DBToApiError(err) + return t.DBToApiError(err, nil) } return nil } @@ -204,10 +216,7 @@ func (t templateDaoImpl) fetch(ctx context.Context, orgID string, uuid string, i err := query.Where("uuid = ? AND org_id = ?", UuidifyString(uuid), orgID). Preload("RepositoryConfigurations").Preload("LastUpdateTask").First(&modelTemplate).Error if err != nil { - if err == gorm.ErrRecordNotFound { - return modelTemplate, &ce.DaoError{NotFound: true, Message: "Could not find template with UUID " + uuid} - } - return modelTemplate, t.DBToApiError(err) + return modelTemplate, t.DBToApiError(err, &uuid) } return modelTemplate, nil } @@ -254,12 +263,12 @@ func (t templateDaoImpl) update(ctx context.Context, tx *gorm.DB, orgID string, tx = tx.WithContext(ctx) if err := tx.Model(&validateTemplate).Where("uuid = ?", UuidifyString(uuid)).Updates(dbTempl.MapForUpdate()).Error; err != nil { - return DBErrorToApi(err) + return t.DBToApiError(err, &uuid) } var existingRepoConfigUUIDs []string if err := tx.Model(&models.TemplateRepositoryConfiguration{}).Select("repository_configuration_uuid").Where("template_uuid = ?", dbTempl.UUID).Find(&existingRepoConfigUUIDs).Error; err != nil { - return DBErrorToApi(err) + return RepositoryDBErrorToApi(err, nil) } if templParams.RepositoryUUIDS != nil { @@ -300,7 +309,7 @@ func (t templateDaoImpl) List(ctx context.Context, orgID string, paginationData Model(&templates). Distinct("uuid"). Count(&totalTemplates).Error != nil { - return api.TemplateCollectionResponse{}, totalTemplates, t.DBToApiError(filteredDB.Error) + return api.TemplateCollectionResponse{}, totalTemplates, t.DBToApiError(filteredDB.Error, nil) } if filteredDB. @@ -311,7 +320,7 @@ func (t templateDaoImpl) List(ctx context.Context, orgID string, paginationData Limit(paginationData.Limit). Offset(paginationData.Offset). Find(&templates).Error != nil { - return api.TemplateCollectionResponse{}, totalTemplates, t.DBToApiError(filteredDB.Error) + return api.TemplateCollectionResponse{}, totalTemplates, t.DBToApiError(filteredDB.Error, nil) } responses := templatesConvertToResponses(templates) @@ -325,10 +334,7 @@ func (t templateDaoImpl) InternalOnlyFetchByName(ctx context.Context, name strin Where("name = ? ", name). Preload("RepositoryConfigurations").First(&modelTemplate).Error if err != nil { - if err == gorm.ErrRecordNotFound { - return modelTemplate, &ce.DaoError{NotFound: true, Message: "Could not find template with name " + name} - } - return modelTemplate, t.DBToApiError(err) + return modelTemplate, t.DBToApiError(err, nil) } return modelTemplate, nil } @@ -365,10 +371,7 @@ func (t templateDaoImpl) SoftDelete(ctx context.Context, orgID string, uuid stri err := t.db.WithContext(ctx).Where("uuid = ? AND org_id = ?", UuidifyString(uuid), orgID).First(&modelTemplate).Error if err != nil { - if err == gorm.ErrRecordNotFound { - return &ce.DaoError{NotFound: true, Message: "Could not find template with UUID " + uuid} - } - return t.DBToApiError(err) + return t.DBToApiError(err, &uuid) } if err = t.db.WithContext(ctx).Delete(&modelTemplate).Error; err != nil { @@ -387,10 +390,7 @@ func (t templateDaoImpl) Delete(ctx context.Context, orgID string, uuid string) err := t.db.WithContext(ctx).Unscoped().Where("uuid = ? AND org_id = ?", UuidifyString(uuid), orgID).First(&modelTemplate).Error if err != nil { - if err == gorm.ErrRecordNotFound { - return &ce.DaoError{NotFound: true, Message: "Could not find template with UUID " + uuid} - } - return t.DBToApiError(err) + return t.DBToApiError(err, &uuid) } if err = t.db.WithContext(ctx).Unscoped().Delete(&modelTemplate).Error; err != nil { @@ -420,7 +420,7 @@ func (t templateDaoImpl) ClearDeletedAt(ctx context.Context, orgID string, uuid func (t templateDaoImpl) GetRepoChanges(ctx context.Context, templateUUID string, newRepoConfigUUIDs []string) ([]string, []string, []string, []string, error) { var templateRepoConfigs []models.TemplateRepositoryConfiguration if err := t.db.WithContext(ctx).Model(&models.TemplateRepositoryConfiguration{}).Unscoped().Where("template_uuid = ?", templateUUID).Find(&templateRepoConfigs).Error; err != nil { - return nil, nil, nil, nil, t.DBToApiError(err) + return nil, nil, nil, nil, t.DBToApiError(err, nil) } // if the repo is being added, it's in the request and the distribution_href is nil @@ -470,7 +470,7 @@ func (t templateDaoImpl) UpdateDistributionHrefs(ctx context.Context, templateUU DoUpdates: clause.AssignmentColumns([]string{"distribution_href"}), }).Create(&templateRepoConfigs).Error if err != nil { - return t.DBToApiError(err) + return t.DBToApiError(err, nil) } return nil } @@ -544,7 +544,7 @@ func (t templateDaoImpl) UpdateSnapshots(ctx context.Context, templateUUID strin DoUpdates: clause.AssignmentColumns([]string{"snapshot_uuid"}), }).Create(&templateRepoConfigs).Error if err != nil { - return t.DBToApiError(err) + return t.DBToApiError(err, nil) } } return nil @@ -555,7 +555,7 @@ func (t templateDaoImpl) DeleteTemplateSnapshot(ctx context.Context, snapshotUUI Delete(models.TemplateRepositoryConfiguration{}).Error if err != nil { - return t.DBToApiError(err) + return t.DBToApiError(err, nil) } return nil } diff --git a/pkg/dao/templates_test.go b/pkg/dao/templates_test.go index b1f4712aa..0aa0f270f 100644 --- a/pkg/dao/templates_test.go +++ b/pkg/dao/templates_test.go @@ -159,7 +159,7 @@ func (s *TemplateSuite) TestFetchSoftDeleted() { assert.NoError(s.T(), err) resp, err = templateDao.Fetch(context.Background(), orgIDTest, found.UUID, false) - assert.ErrorContains(s.T(), err, "Could not find template") + assert.ErrorContains(s.T(), err, "not found") resp, err = templateDao.Fetch(context.Background(), orgIDTest, found.UUID, true) assert.NoError(s.T(), err)