From 6342d8adf9f33d7613f39e2b930218562343d52e Mon Sep 17 00:00:00 2001 From: Roaster05 Date: Sun, 15 Dec 2024 17:55:45 +0530 Subject: [PATCH] Improved error handling with custom messages in project handler Signed-off-by: Roaster05 --- pkg/api/project_handler.go | 128 ++++++++++++++++++++++++++++--------- 1 file changed, 98 insertions(+), 30 deletions(-) diff --git a/pkg/api/project_handler.go b/pkg/api/project_handler.go index 9b19a468..a3c2283a 100644 --- a/pkg/api/project_handler.go +++ b/pkg/api/project_handler.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "strconv" "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" @@ -15,7 +16,7 @@ import ( func CreateProject(opts create.CreateView) error { ctx, client, err := utils.ContextWithClient() if err != nil { - return err + return fmt.Errorf("Failed to initialize client context for creating project") } registryID := new(int64) *registryID, _ = strconv.ParseInt(opts.RegistryID, 10, 64) @@ -25,15 +26,30 @@ func CreateProject(opts create.CreateView) error { } storageLimit, _ := strconv.ParseInt(opts.StorageLimit, 10, 64) - public := strconv.FormatBool(opts.Public) - response, err := client.Project.CreateProject(ctx, &project.CreateProjectParams{Project: &models.ProjectReq{ProjectName: opts.ProjectName, RegistryID: registryID, StorageLimit: &storageLimit, Public: &opts.Public, Metadata: &models.ProjectMetadata{Public: public}}}) + response, err := client.Project.CreateProject(ctx, &project.CreateProjectParams{Project: &models.ProjectReq{ + ProjectName: opts.ProjectName, + RegistryID: registryID, + StorageLimit: &storageLimit, + Public: &opts.Public, + Metadata: &models.ProjectMetadata{Public: public}, + }}) if err != nil { - return err + switch err.(type) { + case *project.CreateProjectBadRequest: + return fmt.Errorf("Bad request for creating project: %s", opts.ProjectName) + case *project.CreateProjectConflict: + return fmt.Errorf("Project %s already exists", opts.ProjectName) + case *project.CreateProjectInternalServerError: + return fmt.Errorf("Internal server error occurred while creating project: %s", opts.ProjectName) + case *project.CreateProjectUnauthorized: + return fmt.Errorf("Unauthorized to create project: %s", opts.ProjectName) + default: + return fmt.Errorf("Unknown error occurred while creating project %s: %v", opts.ProjectName, err) + } } - if response != nil { log.Info("Project created successfully") } @@ -43,15 +59,22 @@ func CreateProject(opts create.CreateView) error { func GetProject(projectName string) (*project.GetProjectOK, error) { ctx, client, err := utils.ContextWithClient() var response = &project.GetProjectOK{} - if err != nil { - return response, err + return response, fmt.Errorf("Failed to initialize client context for getting project %s", projectName) } response, err = client.Project.GetProject(ctx, &project.GetProjectParams{ProjectNameOrID: projectName}) - if err != nil { - return response, err + switch err.(type) { + case *project.GetProjectInternalServerError: + return response, fmt.Errorf("Internal server error occurred while getting project %s", projectName) + case *project.GetProjectDeletableNotFound: + return response, fmt.Errorf("Project %s not found", projectName) + case *project.GetProjectUnauthorized: + return response, fmt.Errorf("Unauthorized to get project %s", projectName) + default: + return response, fmt.Errorf("Unknown error occurred while getting project %s: %v", projectName, err) + } } return response, nil @@ -60,34 +83,39 @@ func GetProject(projectName string) (*project.GetProjectOK, error) { func DeleteProject(projectName string, forceDelete bool) error { ctx, client, err := utils.ContextWithClient() if err != nil { - return err + return fmt.Errorf("Failed to initialize client context for deleting project %s", projectName) } if forceDelete { var resp repository.ListRepositoriesOK - resp, err = ListRepository(projectName) - if err != nil { - log.Errorf("failed to list repositories: %v", err) - return err + return fmt.Errorf("Failed to list repositories for project %s", projectName) } for _, repo := range resp.Payload { _, repoName := utils.ParseProjectRepo(repo.Name) err = RepoDelete(projectName, repoName) - if err != nil { - log.Errorf("failed to delete repository: %v", err) - return err + return fmt.Errorf("Failed to delete repository %s from project %s", repoName, projectName) } } } _, err = client.Project.DeleteProject(ctx, &project.DeleteProjectParams{ProjectNameOrID: projectName}) - if err != nil { - return err + switch err.(type) { + case *project.DeleteProjectBadRequest: + return fmt.Errorf("Invalid request to delete project %s", projectName) + case *project.DeleteProjectNotFound: + return fmt.Errorf("Project %s not found or already deleted", projectName) + case *project.DeleteProjectForbidden: + return fmt.Errorf("Insufficient permissions to delete project %s", projectName) + case *project.DeleteProjectInternalServerError: + return fmt.Errorf("Internal server error occurred while deleting project %s", projectName) + default: + return fmt.Errorf("Unknown error occurred while deleting project %s: %v", projectName, err) + } } log.Infof("Project %s deleted successfully", projectName) @@ -97,15 +125,29 @@ func DeleteProject(projectName string, forceDelete bool) error { func ListProject(opts ...ListFlags) (project.ListProjectsOK, error) { ctx, client, err := utils.ContextWithClient() if err != nil { - return project.ListProjectsOK{}, err + return project.ListProjectsOK{}, fmt.Errorf("Failed to initialize client context for listing projects") } var listFlags ListFlags if len(opts) > 0 { listFlags = opts[0] } - response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{Page: &listFlags.Page, PageSize: &listFlags.PageSize, Q: &listFlags.Q, Sort: &listFlags.Sort, Name: &listFlags.Name, Public: &listFlags.Public}) + response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{ + Page: &listFlags.Page, + PageSize: &listFlags.PageSize, + Q: &listFlags.Q, + Sort: &listFlags.Sort, + Name: &listFlags.Name, + Public: &listFlags.Public, + }) if err != nil { - return project.ListProjectsOK{}, err + switch err.(type) { + case *project.ListProjectsUnauthorized: + return project.ListProjectsOK{}, fmt.Errorf("Unauthorized access to list the projects") + case *project.ListProjectsInternalServerError: + return project.ListProjectsOK{}, fmt.Errorf("Internal server error occurred while listing projects") + default: + return project.ListProjectsOK{}, fmt.Errorf("Unknown error occurred while listing projects: %v", err) + } } return *response, nil } @@ -113,15 +155,28 @@ func ListProject(opts ...ListFlags) (project.ListProjectsOK, error) { func ListAllProjects(opts ...ListFlags) (project.ListProjectsOK, error) { ctx, client, err := utils.ContextWithClient() if err != nil { - return project.ListProjectsOK{}, err + return project.ListProjectsOK{}, fmt.Errorf("Failed to initialize client context for listing all projects") } var listFlags ListFlags if len(opts) > 0 { listFlags = opts[0] } - response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{Page: &listFlags.Page, PageSize: &listFlags.PageSize, Q: &listFlags.Q, Sort: &listFlags.Sort, Name: &listFlags.Name}) + response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{ + Page: &listFlags.Page, + PageSize: &listFlags.PageSize, + Q: &listFlags.Q, + Sort: &listFlags.Sort, + Name: &listFlags.Name, + }) if err != nil { - return project.ListProjectsOK{}, err + switch err.(type) { + case *project.ListProjectsUnauthorized: + return project.ListProjectsOK{}, fmt.Errorf("Unauthorized access to list the projects") + case *project.ListProjectsInternalServerError: + return project.ListProjectsOK{}, fmt.Errorf("Internal server error occurred while listing all projects") + default: + return project.ListProjectsOK{}, fmt.Errorf("Unknown error occurred while listing all projects: %v", err) + } } return *response, nil } @@ -129,12 +184,17 @@ func ListAllProjects(opts ...ListFlags) (project.ListProjectsOK, error) { func SearchProject(query string) (search.SearchOK, error) { ctx, client, err := utils.ContextWithClient() if err != nil { - return search.SearchOK{}, err + return search.SearchOK{}, fmt.Errorf("Failed to initialize client context for searching projects") } response, err := client.Search.Search(ctx, &search.SearchParams{Q: query}) if err != nil { - return search.SearchOK{}, err + switch err.(type) { + case *search.SearchInternalServerError: + return search.SearchOK{}, fmt.Errorf("Internal server error occurred while searching projects") + default: + return search.SearchOK{}, fmt.Errorf("Unknown error occurred while searching projects: %v", err) + } } return *response, nil } @@ -142,16 +202,24 @@ func SearchProject(query string) (search.SearchOK, error) { func LogsProject(projectName string) (*project.GetLogsOK, error) { ctx, client, err := utils.ContextWithClient() if err != nil { - return nil, err + return nil, fmt.Errorf("Failed to initialize client context for fetching logs for project %s", projectName) } response, err := client.Project.GetLogs(ctx, &project.GetLogsParams{ ProjectName: projectName, Context: ctx, }) - if err != nil { - return nil, err + switch err.(type) { + case *project.GetLogsBadRequest: + return nil, fmt.Errorf("Bad request while fetching logs for project %s", projectName) + case *project.GetLogsInternalServerError: + return nil, fmt.Errorf("Internal server error occurred while fetching logs for project %s", projectName) + case *project.GetLogsUnauthorized: + return nil, fmt.Errorf("Unauthorized to fetch logs for project %s", projectName) + default: + return nil, fmt.Errorf("Unknown error occurred while fetching logs for project %s: %v", projectName, err) + } } return response, nil