Skip to content

Instantly share code, notes, and snippets.

@mrunalp
Created March 18, 2020 21:30
Show Gist options
  • Save mrunalp/413eebe74607699cc965e55f6102365d to your computer and use it in GitHub Desktop.
Save mrunalp/413eebe74607699cc965e55f6102365d to your computer and use it in GitHub Desktop.
package crierrors
import "errors"
import "google.golang.org/grpc/codes"
import "google.golang.org/grpc/status"
var (
// ErrNotFound is a an error epresenting an object not found
// like a pod or a container.
ErrNotFound = errors.New("cri: not found")
// ErrUnknown is for errors that don't fit other classification
ErrUnknown = errors.New("cri: unknown error")
)
// IsNotFound is a helper to check if an error is ErrNotFound.
func IsNotFound(err error) bool {
return errors.Is(err, ErrNotFound)
}
// FromGRPCError maps grpc error codes to CRI errors.
func FromGRPCError(err error) (cerr error) {
if err == nil {
return nil
}
s, ok := status.FromError(err)
if !ok {
return err
}
switch s.Code() {
case codes.NotFound:
cerr = ErrNotFound
case codes.Unknown:
cerr = ErrUnknown
}
return cerr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment