github.com/quay/claircore@v1.5.28/java/jar/errors.go (about)

     1  package jar
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  )
     7  
     8  // LocalError is a type for errors returned from this package.
     9  type localError struct {
    10  	inner error
    11  	msg   string
    12  }
    13  
    14  // These are sentinel errors that can be used with errors.Is.
    15  var (
    16  	ErrUnidentified = errors.New("unidentified jar")
    17  	ErrNotAJar      = errors.New("does not seem to be a jar")
    18  )
    19  
    20  func (e *localError) Error() string {
    21  	switch {
    22  	case e.inner == nil && e.msg == "":
    23  		panic("programmer error: no error or message")
    24  	case e.inner == nil && e.msg != "":
    25  		return "jar: " + e.msg
    26  	case e.inner != nil && e.msg == "":
    27  		return fmt.Sprintf("jar: %v", e.inner)
    28  	case e.inner != nil && e.msg != "":
    29  		return fmt.Sprintf("jar: %s: %v", e.msg, e.inner)
    30  	}
    31  	panic("unreachable")
    32  }
    33  
    34  func (e *localError) Unwrap() error {
    35  	return e.inner
    36  }
    37  
    38  func mkErr(msg string, err error) *localError {
    39  	return &localError{msg: msg, inner: err}
    40  }
    41  func archiveErr(m srcPath, err error) *localError {
    42  	return &localError{
    43  		msg:   fmt.Sprintf("at %q", m.String()),
    44  		inner: err,
    45  	}
    46  }
    47  
    48  type errUnidentified struct {
    49  	name string
    50  }
    51  
    52  func unidentified(n string) *errUnidentified {
    53  	return &errUnidentified{n}
    54  }
    55  
    56  func (e *errUnidentified) Is(target error) bool {
    57  	return target == ErrUnidentified || target == e
    58  }
    59  
    60  func (e *errUnidentified) Error() string {
    61  	return fmt.Sprintf("unidentified jar: %s", e.name)
    62  }
    63  
    64  type errNotAJar struct {
    65  	inner error
    66  	name  string
    67  }
    68  
    69  func notAJar(p srcPath, reason error) *errNotAJar {
    70  	return &errNotAJar{name: p.String(), inner: reason}
    71  }
    72  
    73  func (e *errNotAJar) Error() string {
    74  	return fmt.Sprintf("%q not a jar: %v", e.name, e.inner)
    75  }
    76  
    77  func (e *errNotAJar) Unwrap() error {
    78  	return e.inner
    79  }
    80  
    81  func (e *errNotAJar) Is(target error) bool {
    82  	return target == ErrNotAJar || target == e
    83  }