From 2f0e52aea9766af0eaec30ec2c9f89e2a521ed5a Mon Sep 17 00:00:00 2001 From: shireenf-ibm <82180114+shireenf-ibm@users.noreply.github.com> Date: Sun, 22 Oct 2023 11:20:35 +0300 Subject: [PATCH] simplifying dir_not_exist error (#249) * returning an err string describing the go generated error instead of returning the original error * Update pkg/netpol/scan/error_types.go Co-authored-by: Adi Sosnovich <82078442+adisos@users.noreply.github.com> * Update pkg/netpol/scan/error_types.go Co-authored-by: Adi Sosnovich <82078442+adisos@users.noreply.github.com> --------- Co-authored-by: Adi Sosnovich <82078442+adisos@users.noreply.github.com> --- pkg/netpol/diff/diff_test.go | 2 +- pkg/netpol/scan/error_types.go | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/netpol/diff/diff_test.go b/pkg/netpol/diff/diff_test.go index 45fbfe4e..063fc493 100644 --- a/pkg/netpol/diff/diff_test.go +++ b/pkg/netpol/diff/diff_test.go @@ -377,7 +377,7 @@ func TestFatalErrors(t *testing.T) { name: "dir 1 does not exists", dir1: filepath.Join("bad_yamls", "subdir3"), dir2: "ipblockstest", - firstErrStr: "error accessing directory:", + firstErrStr: "was not found", }, { name: "dir 1 includes illegal pods list", diff --git a/pkg/netpol/scan/error_types.go b/pkg/netpol/scan/error_types.go index 49c64bcb..c4a076c1 100644 --- a/pkg/netpol/scan/error_types.go +++ b/pkg/netpol/scan/error_types.go @@ -3,6 +3,7 @@ package scan import ( "errors" "fmt" + "os" ) // FileProcessingError holds all information about a single error/warning that occurred during @@ -47,6 +48,7 @@ type FailedReadingFileError struct { type FailedAccessingDirError struct { origErr error + dirPath string } func (err *NoYamlsFoundError) Error() string { @@ -98,7 +100,11 @@ func (err *FailedReadingFileError) Unwrap() error { } func (err *FailedAccessingDirError) Error() string { - return fmt.Sprintf("error accessing directory: %v", err.origErr) + if errors.Is(err.origErr, os.ErrNotExist) { + return fmt.Sprintf("directory %s was not found", err.dirPath) + } + + return fmt.Sprintf("failed reading contents of directory %s ", err.dirPath) } func (err *FailedAccessingDirError) Unwrap() error { @@ -190,5 +196,5 @@ func failedReadingFile(filePath string, err error) *FileProcessingError { } func failedAccessingDir(dirPath string, err error, isSubDir bool) *FileProcessingError { - return &FileProcessingError{&FailedAccessingDirError{err}, dirPath, 0, -1, !isSubDir, true} + return &FileProcessingError{&FailedAccessingDirError{origErr: err, dirPath: dirPath}, dirPath, 0, -1, !isSubDir, true} }