github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/internal/err_helper.go (about)

     1  package internal
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  
     9  	"github.com/metasources/buildx/internal/log"
    10  )
    11  
    12  // CloseAndLogError closes the given io.Closer and reports any errors found as a warning in the log
    13  func CloseAndLogError(closer io.Closer, location string) {
    14  	if err := closer.Close(); err != nil {
    15  		log.Warnf("unable to close file for location=%q: %+v", location, err)
    16  	}
    17  }
    18  
    19  type ErrPath struct {
    20  	Context string
    21  	Path    string
    22  	Err     error
    23  }
    24  
    25  func (e ErrPath) Error() string {
    26  	return fmt.Sprintf("%s unable to observe contents of %+v: %v", e.Context, e.Path, e.Err)
    27  }
    28  
    29  func IsErrPath(err error) bool {
    30  	var pathErr ErrPath
    31  	return errors.As(err, &pathErr)
    32  }
    33  
    34  func IsErrPathPermission(err error) bool {
    35  	var pathErr ErrPath
    36  	if errors.As(err, &pathErr) {
    37  		return os.IsPermission(pathErr.Err)
    38  	}
    39  	return false
    40  }