Skip to content

Commit

Permalink
simplifying dir_not_exist error (#249)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update pkg/netpol/scan/error_types.go

Co-authored-by: Adi Sosnovich <[email protected]>

---------

Co-authored-by: Adi Sosnovich <[email protected]>
  • Loading branch information
shireenf-ibm and adisos authored Oct 22, 2023
1 parent 7544707 commit 2f0e52a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/netpol/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions pkg/netpol/scan/error_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scan
import (
"errors"
"fmt"
"os"
)

// FileProcessingError holds all information about a single error/warning that occurred during
Expand Down Expand Up @@ -47,6 +48,7 @@ type FailedReadingFileError struct {

type FailedAccessingDirError struct {
origErr error
dirPath string
}

func (err *NoYamlsFoundError) Error() string {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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}
}

0 comments on commit 2f0e52a

Please sign in to comment.