diff options
| author | Joe Richey joerichey@google.com <joerichey@google.com> | 2017-06-21 09:52:40 -0700 |
|---|---|---|
| committer | Joe Richey joerichey@google.com <joerichey@google.com> | 2017-06-28 14:06:52 -0700 |
| commit | 77b226a90ef70b77ca556830528c013a23b01e57 (patch) | |
| tree | b351dbb427ed62550f2440b8d56249bdcbbca96a /util | |
| parent | 07341f3966675e4875f8cad3c8d86ae502de6d4d (diff) | |
Change error handling to new package
This commit changes the error handing for the crypto, filesystem,
metadata, pam, and util packages to use the error handling library
github.com/pkg/errors. This means elimination of the FSError type, an
increased use of wrapping errors (as opposed to logging), switching
on the Cause() of an error (as opposed to its value), and improving our
integration tests involving TEST_FILESYSTEM_ROOT.
This commit also fixes a few bugs with the keyring code to ensure that
our {Find|Remove|Insert}PolicyKey functions are always operating on the
same keyring. The check for filesystem support has been moved from the
filesystem package to the metadata package. Finally, the API for the
filesystem package has been slightly modified:
* filesystem.AllFilesystems() now returns all the filesystems in
sorted order
* certain path methods are now public
O_SYNC is also removed for writing the metadata. We don't get that much
from syncing the metadata, as the actual file data could also be
corrupted by and IO error. The sync operation is also occasionally very
slow (~3 seconds) and can be unfriendly to battery life.
Change-Id: I392c2655141714b16dfdbc84ac09780072be2cf0
Diffstat (limited to 'util')
| -rw-r--r-- | util/errors.go | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/util/errors.go b/util/errors.go index e5eea4b..2a865a3 100644 --- a/util/errors.go +++ b/util/errors.go @@ -89,18 +89,12 @@ func (e *ErrWriter) Err() error { return e.err } -// InvalidInput is an error that should indicate either bad input from a caller -// of a public package function. -type InvalidInput string - -func (i InvalidInput) Error() string { - return "invalid input: " + string(i) -} - -// InvalidLengthError indicates name should have had length expected. -func InvalidLengthError(name string, expected int, actual int) InvalidInput { - message := fmt.Sprintf("length of %s: expected=%d, actual=%d", name, expected, actual) - return InvalidInput(message) +// CheckValidLength returns an invalid length error if expected != actual +func CheckValidLength(expected, actual int) error { + if expected == actual { + return nil + } + return fmt.Errorf("expected length of %d, got %d", expected, actual) } // SystemError is an error that should indicate something has gone wrong in the @@ -119,20 +113,17 @@ func NeverError(err error) { } } -// UnderlyingError returns the underlying error for known os error types and -// logs the full error. From: src/os/error.go -func UnderlyingError(err error) error { - var newErr error - switch typedErr := err.(type) { - case *os.PathError: - newErr = typedErr.Err - case *os.LinkError: - newErr = typedErr.Err - case *os.SyscallError: - newErr = typedErr.Err - default: - return err +// TestEnvVarName is the name on an environment variable that should be set to +// an empty mountpoint. This is only used for integration tests. +var TestEnvVarName = "TEST_FILESYSTEM_ROOT" + +// TestPath returns a the path specified by TestEnvVarName. The function +// panics if the environment variable is not set. This function is only used for +// integration tests. +func TestPath() (string, error) { + path := os.Getenv(TestEnvVarName) + if path == "" { + return "", fmt.Errorf("%s: environment variable not set", TestEnvVarName) } - log.Print(err) - return newErr + return path, nil } |