Return typed error when Mount Fails

Mount can fail for a variety of reasons and caller might want to know
why mount failed. Using untyped string based error does not provide
enough granularity to make that verification.
This commit is contained in:
Hemant Kumar
2019-12-17 22:59:32 -05:00
committed by Srini Brahmaroutu
parent 0e8dbc2c1e
commit 51e2ee9753
2 changed files with 37 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ limitations under the License.
package mount
import (
"fmt"
"os"
"path/filepath"
"strings"
@@ -76,6 +77,36 @@ type MountPoint struct {
Pass int
}
type MountErrorType string
const (
FilesystemMismatch MountErrorType = "FilesystemMismatch"
HasFilesystemErrors MountErrorType = "HasFilesystemErrors"
UnformattedReadOnly MountErrorType = "UnformattedReadOnly"
FormatFailed MountErrorType = "FormatFailed"
)
type MountError struct {
Type MountErrorType
Message string
}
func (mountError *MountError) String() string {
return mountError.Message
}
func (mountError *MountError) Error() string {
return mountError.Message
}
func NewMountError(mountErrorValue MountErrorType, format string, args ...interface{}) error {
mountError := &MountError{
Type: mountErrorValue,
Message: fmt.Sprintf(format, args...),
}
return mountError
}
// SafeFormatAndMount probes a device to see if it is formatted.
// Namely it checks to see if a file system is present. If so it
// mounts it otherwise the device is formatted first then mounted.