github.com/puellanivis/breton@v0.2.16/lib/files/errors.go (about)

     1  package files
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"syscall"
     7  )
     8  
     9  // PathError is DEPRECATED, and returns an *os.PathError with appropriate fields set. DO NOT USE.
    10  //
    11  // This is a stop-gap quick-replace to remove `&os.PathError{ op, path, err }`.
    12  // One should use the direct complex literal instruction instead.
    13  //
    14  // Deprecated: use &os.PathError{} directly.
    15  func PathError(op, path string, err error) error {
    16  	return &os.PathError{
    17  		Op:   op,
    18  		Path: path,
    19  		Err:  err,
    20  	}
    21  }
    22  
    23  var (
    24  	// ErrNotSupported should be returned, if a particular feature or option is not supported.
    25  	ErrNotSupported = errors.New("not supported")
    26  
    27  	// ErrNotDirectory should be returned, if a request is made to ReadDir with a non-directory.
    28  	ErrNotDirectory = syscall.ENOTDIR
    29  )
    30  
    31  type invalidURLError struct {
    32  	s string
    33  }
    34  
    35  func (e *invalidURLError) Error() string {
    36  	return e.s
    37  }
    38  
    39  func (e *invalidURLError) Unwrap() error {
    40  	if e == ErrURLInvalid {
    41  		return os.ErrInvalid
    42  	}
    43  
    44  	return ErrURLInvalid
    45  }
    46  
    47  func (e *invalidURLError) Is(target error) bool {
    48  	switch target {
    49  	case ErrURLInvalid, os.ErrInvalid:
    50  		return true
    51  	}
    52  
    53  	return e == target
    54  }
    55  
    56  // NewInvalidURLError returns an error that formats as the given text,
    57  // and where errors.Is will return true for both: files.ErrURLInvalid and os.ErrInvalid.
    58  func NewInvalidURLError(reason string) error {
    59  	return &invalidURLError{
    60  		s: reason,
    61  	}
    62  }
    63  
    64  // A set of Invalid URL Error to better identify and relate specific invalid URL details.
    65  var (
    66  	ErrURLInvalid = NewInvalidURLError("invalid url")
    67  
    68  	ErrURLCannotHaveAuthority = NewInvalidURLError("invalid url: scheme cannot have an authority")
    69  	ErrURLCannotHaveHost      = NewInvalidURLError("invalid url: scheme cannot have a host in authority")
    70  
    71  	ErrURLHostRequired = NewInvalidURLError("invalid url: scheme requires non-empty host in authority")
    72  	ErrURLPathRequired = NewInvalidURLError("invalid url: scheme requires non-empty path")
    73  )