From 6c8332af293776a2efd1a3a16531137293f02940 Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Sun, 29 Sep 2024 18:22:18 -0400 Subject: [PATCH] style: unify error msg --- internal/stage/util.go | 8 ++++++-- pkg/healthcheck/commit.go | 2 +- pkg/healthcheck/forbidden.go | 6 +++--- pkg/healthcheck/nonascii.go | 6 +++--- pkg/healthcheck/reposize.go | 2 +- pkg/healthcheck/tag.go | 2 +- pkg/healthcheck/utils.go | 2 +- pkg/healthcheck/verify.go | 6 +++--- 8 files changed, 19 insertions(+), 15 deletions(-) diff --git a/internal/stage/util.go b/internal/stage/util.go index 4c9f084..a061a1e 100644 --- a/internal/stage/util.go +++ b/internal/stage/util.go @@ -1,12 +1,16 @@ package stage -import "github.com/mitchellh/mapstructure" +import ( + "fmt" + + "github.com/mitchellh/mapstructure" +) func DecodeConf[T any](confAny any) (*T, error) { var conf T err := mapstructure.Decode(confAny, &conf) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to decode conf: %w", err) } return &conf, nil } diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index 375046e..18ef47c 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -72,7 +72,7 @@ func NonAsciiMsg(root string) error { } if !isCommitLegal { - return fmt.Errorf("Non-ASCII characters in commit messages:\n%s", msg) + return fmt.Errorf("non-ASCII characters in commit messages:\n%s", msg) } return nil } diff --git a/pkg/healthcheck/forbidden.go b/pkg/healthcheck/forbidden.go index 1a46c70..c3c11f5 100644 --- a/pkg/healthcheck/forbidden.go +++ b/pkg/healthcheck/forbidden.go @@ -26,7 +26,7 @@ func getForbiddens(root string, fileList []string, localList string) ([]string, if localList != "" { file, err := os.Open(localList) if err != nil { - return nil, fmt.Errorf("Failed to open file %s: %v\n", localList, err) + return nil, fmt.Errorf("failed to open file %s: %v\n", localList, err) } defer file.Close() @@ -35,7 +35,7 @@ func getForbiddens(root string, fileList []string, localList string) ([]string, dirs = append(dirs, scanner.Text()) } if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("Error reading file %s: %v\n", localList, err) + return nil, fmt.Errorf("error reading file %s: %v\n", localList, err) } } @@ -78,7 +78,7 @@ func ForbiddenCheck(rootDir string, regexList []string, localList string, repo s } if len(forbids) > 0 { - return fmt.Errorf("The following forbidden files were found: %s\n\nTo fix it, first make a backup of your repository and then run the following commands:\nfor i in %s%s", + return fmt.Errorf("the following forbidden files were found: %s\n\nTo fix it, first make a backup of your repository and then run the following commands:\nfor i in %s%s", strings.Join(forbids, ", "), strings.Join(forbids, " "), fmt.Sprint( diff --git a/pkg/healthcheck/nonascii.go b/pkg/healthcheck/nonascii.go index 0204c3c..d050c9f 100644 --- a/pkg/healthcheck/nonascii.go +++ b/pkg/healthcheck/nonascii.go @@ -20,7 +20,7 @@ func getNonAscii(root string, localList string) ([]string, error) { if localList != "" { file, err := os.Open(localList) if err != nil { - return nil, fmt.Errorf("Failed to open file %s: %v\n", localList, err) + return nil, fmt.Errorf("failed to open file %s: %v\n", localList, err) } defer file.Close() @@ -29,7 +29,7 @@ func getNonAscii(root string, localList string) ([]string, error) { dirs = append(dirs, scanner.Text()) } if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("Error reading file %s: %v\n", localList, err) + return nil, fmt.Errorf("error reading file %s: %v\n", localList, err) } } @@ -86,7 +86,7 @@ func NonAsciiFiles(root string, localList string) error { return fmt.Errorf("error getting non-ascii: %w", err) } if len(nonAscii) > 0 { - return fmt.Errorf("Non-ASCII characters found in the following files:\n%s", + return fmt.Errorf("non-ASCII characters found in the following files:\n%s", strings.Join(nonAscii, "\n")) } return nil diff --git a/pkg/healthcheck/reposize.go b/pkg/healthcheck/reposize.go index dd17d06..2bd297c 100644 --- a/pkg/healthcheck/reposize.go +++ b/pkg/healthcheck/reposize.go @@ -34,7 +34,7 @@ func RepoSize() error { } } if sum > 2048 { - return fmt.Errorf("Repository larger than 2MB. Please clean up or contact the teaching team.") + return fmt.Errorf("repository larger than 2MB. Please clean up or contact the teaching team.") } return nil } diff --git a/pkg/healthcheck/tag.go b/pkg/healthcheck/tag.go index d0f7f76..167164c 100644 --- a/pkg/healthcheck/tag.go +++ b/pkg/healthcheck/tag.go @@ -56,7 +56,7 @@ func CheckTags(repoPath string, category string, n int) error { } } if !found { - return fmt.Errorf("Wrong release tag '%s' or missing release tags. Please use one of '%s'.", target, strings.Join(tags, "', '")) + return fmt.Errorf("wrong release tag '%s' or missing release tags. Please use one of '%s'.", target, strings.Join(tags, "', '")) } return nil } diff --git a/pkg/healthcheck/utils.go b/pkg/healthcheck/utils.go index 005379a..ec55264 100644 --- a/pkg/healthcheck/utils.go +++ b/pkg/healthcheck/utils.go @@ -29,7 +29,7 @@ func getRegex(fileList []string) ([]*regexp.Regexp, error) { for _, pattern := range fileList { regex, err := regexp.Compile("(?i)" + pattern) if err != nil { - return nil, fmt.Errorf("Error compiling regex:%w", err) + return nil, fmt.Errorf("error compiling regex: %w", err) } regexList = append(regexList, regex) } diff --git a/pkg/healthcheck/verify.go b/pkg/healthcheck/verify.go index 9357679..f35d539 100644 --- a/pkg/healthcheck/verify.go +++ b/pkg/healthcheck/verify.go @@ -33,10 +33,10 @@ func checkFileChecksum(rootDir, fileName, expectedChecksum string) error { filePath := filepath.Join(rootDir, strings.TrimSpace(fileName)) actualChecksum, err := getChecksum(filePath) if err != nil { - return fmt.Errorf("Error reading file %s: %v", filePath, err) + return fmt.Errorf("error reading file %s: %v", filePath, err) } if actualChecksum != expectedChecksum { - return fmt.Errorf("Checksum for %s failed. Expected %s, but got %s. Please revert your changes or contact the teaching team if you have a valid reason for adjusting them.", filePath, expectedChecksum, actualChecksum) + return fmt.Errorf("checksum for %s failed. Expected %s, but got %s. Please revert your changes or contact the teaching team if you have a valid reason for adjusting them.", filePath, expectedChecksum, actualChecksum) } return nil } @@ -49,7 +49,7 @@ func VerifyFiles(rootDir string, checkFileNameList string, checkFileSumList stri checkSums := strings.Split(checkFileSumList, ",") // Check if the number of files matches the number of checksums if len(fileNames) != len(checkSums) { - return fmt.Errorf("Error: The number of files and checksums do not match.") + return fmt.Errorf("the number of files and checksums do not match") } // Check each file's checksum for i, fileName := range fileNames {