package filesystem
import (
"errors"
"fmt"
)
var (
ErrNotFound = errors.New("not found")
ErrPermissionDenied = errors.New("permission denied")
ErrInvalidArgument = errors.New("invalid argument")
ErrAlreadyExists = errors.New("already exists")
ErrNotDirectory = errors.New("not a directory")
ErrNotSupported = errors.New("operation not supported")
)
type NotFoundError struct {
Path string
Op string
}
func (e *NotFoundError) Error() string {
if e.Op != "" {
return fmt.Sprintf("%s: %s: %s", e.Op, e.Path, "not found")
}
return fmt.Sprintf("%s: not found", e.Path)
}
func (e *NotFoundError) Is(target error) bool {
return target == ErrNotFound
}
type PermissionDeniedError struct {
Path string
Op string
Reason string
}
func (e *PermissionDeniedError) Error() string {
if e.Reason != "" {
return fmt.Sprintf("%s: %s: permission denied (%s)", e.Op, e.Path, e.Reason)
}
if e.Op != "" {
return fmt.Sprintf("%s: %s: permission denied", e.Op, e.Path)
}
return fmt.Sprintf("%s: permission denied", e.Path)
}
func (e *PermissionDeniedError) Is(target error) bool {
return target == ErrPermissionDenied
}
type InvalidArgumentError struct {
Name string
Value interface{}
Reason string
}
func (e *InvalidArgumentError) Error() string {
if e.Value != nil {
return fmt.Sprintf("invalid argument %s=%v: %s", e.Name, e.Value, e.Reason)
}
return fmt.Sprintf("invalid argument %s: %s", e.Name, e.Reason)
}
func (e *InvalidArgumentError) Is(target error) bool {
return target == ErrInvalidArgument
}
type AlreadyExistsError struct {
Path string
Resource string
}
func (e *AlreadyExistsError) Error() string {
if e.Resource != "" {
return fmt.Sprintf("%s already exists: %s", e.Resource, e.Path)
}
return fmt.Sprintf("already exists: %s", e.Path)
}
func (e *AlreadyExistsError) Is(target error) bool {
return target == ErrAlreadyExists
}
type NotDirectoryError struct {
Path string
}
func (e *NotDirectoryError) Error() string {
return fmt.Sprintf("not a directory: %s", e.Path)
}
func (e *NotDirectoryError) Is(target error) bool {
return target == ErrNotDirectory
}
type NotSupportedError struct {
Path string
Op string
}
func (e *NotSupportedError) Error() string {
if e.Op != "" {
return fmt.Sprintf("%s: %s: operation not supported", e.Op, e.Path)
}
return fmt.Sprintf("%s: operation not supported", e.Path)
}
func (e *NotSupportedError) Is(target error) bool {
return target == ErrNotSupported
}
func NewNotFoundError(op, path string) error {
return &NotFoundError{Op: op, Path: path}
}
func NewPermissionDeniedError(op, path, reason string) error {
return &PermissionDeniedError{Op: op, Path: path, Reason: reason}
}
func NewInvalidArgumentError(name string, value interface{}, reason string) error {
return &InvalidArgumentError{Name: name, Value: value, Reason: reason}
}
func NewAlreadyExistsError(resource, path string) error {
return &AlreadyExistsError{Resource: resource, Path: path}
}
func NewNotDirectoryError(path string) error {
return &NotDirectoryError{Path: path}
}
func NewNotSupportedError(op, path string) error {
return &NotSupportedError{Op: op, Path: path}
}