Skip to content

Commit

Permalink
Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
annasong20 committed Dec 13, 2022
1 parent eb2fc55 commit 59470ac
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
9 changes: 3 additions & 6 deletions api/internal/localizer/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ type ResourceLoadError struct {
FileError error
}

var _ error = ResourceLoadError{}

func (rle ResourceLoadError) Error() string {
return fmt.Sprintf(`when parsing as inline received error: %s
when parsing as filepath received error: %s`, rle.InlineError, rle.FileError)
}

type PathLocalizeError struct {
Path string
FileError error
RootError error
}

var _ error = PathLocalizeError{}

func (ple PathLocalizeError) Error() string {
return fmt.Sprintf(`when localizing as file received error: %s
when localizing as directory received error: %s`, ple.FileError, ple.RootError)
return fmt.Sprintf(`could not localize path %q as file: %s; could not localize path %q as directory: %s`,
ple.Path, ple.FileError, ple.Path, ple.RootError)
}
3 changes: 2 additions & 1 deletion api/internal/localizer/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,11 @@ func (lc *localizer) localizePath(path string) (string, error) {
locPath, rootErr = lc.localizeDir(path)
if rootErr != nil {
err := PathLocalizeError{
Path: path,
FileError: fileErr,
RootError: rootErr,
}
return "", errors.WrapPrefixf(err, "unable to localize path %q", path)
return "", err
}
}
return locPath, nil
Expand Down
7 changes: 4 additions & 3 deletions api/internal/localizer/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,12 +927,13 @@ resources:
const expectedRootErr = `unable to localize root "b": unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory '/a/b'`
var actualErr PathLocalizeError
require.ErrorAs(t, err, &actualErr)
require.Equal(t, "b", actualErr.Path)
require.EqualError(t, actualErr.FileError, expectedFileErr)
require.EqualError(t, actualErr.RootError, expectedRootErr)

const expectedErrPrefix = `unable to localize target "/a": unable to localize resources entry: unable to localize path "b"`
require.EqualError(t, err, fmt.Sprintf(`%s: when localizing as file received error: %s
when localizing as directory received error: %s`, expectedErrPrefix, expectedFileErr, expectedRootErr))
const expectedErrPrefix = `unable to localize target "/a": unable to localize resources entry`
require.EqualError(t, err, fmt.Sprintf(`%s: could not localize path "b" as file: %s; could not localize path "b" as directory: %s`,
expectedErrPrefix, expectedFileErr, expectedRootErr))

checkFSys(t, expected, actual)
}

0 comments on commit 59470ac

Please sign in to comment.