Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/containerd-shim-runhcs-v1/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"path/filepath"
"time"

"errors"

task "github.com/containerd/containerd/api/runtime/task/v2"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"google.golang.org/protobuf/proto"
Expand All @@ -27,7 +28,7 @@ import (
func limitedRead(filePath string, readLimitBytes int64) ([]byte, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, errors.Wrapf(err, "limited read failed to open file: %s", filePath)
return nil, fmt.Errorf("limited read failed to open file: %s: %w", filePath, err)
}
defer f.Close()
if fi, err := f.Stat(); err == nil {
Expand All @@ -37,11 +38,11 @@ func limitedRead(filePath string, readLimitBytes int64) ([]byte, error) {
buf := make([]byte, readLimitBytes)
_, err := f.Read(buf)
if err != nil {
return []byte{}, errors.Wrapf(err, "limited read failed during file read: %s", filePath)
return []byte{}, fmt.Errorf("limited read failed during file read: %s: %w", filePath, err)
}
return buf, nil
}
return []byte{}, errors.Wrapf(err, "limited read failed during file stat: %s", filePath)
return []byte{}, fmt.Errorf("limited read failed during file stat: %s: %w", filePath, err)
}

var deleteCommand = cli.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/ncproxy/computeagent_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package main
import (
"sync"

"github.com/pkg/errors"
"errors"
)

var errNilCache = errors.New("cannot access a nil cache")
Expand Down
3 changes: 2 additions & 1 deletion cmd/runhcs/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"os"
"syscall"

"errors"

"github.com/Microsoft/go-winio"
"github.com/Microsoft/hcsshim/internal/appargs"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/Microsoft/hcsshim/internal/runhcs"
"github.com/Microsoft/hcsshim/internal/uvm"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
Expand Down
117 changes: 35 additions & 82 deletions internal/bridgeutils/gcserr/errors.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package gcserr

import (
"errors"
"fmt"
"io"

"github.com/pkg/errors"
)

// Hresult is a type corresponding to the HRESULT error type used on Windows.
type Hresult int32

// ! Must match error values in internal\hcs\errors.go
// from
// - https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/705fb797-2175-4a90-b5a3-3918024b10b8
// - https://docs.microsoft.com/en-us/virtualization/api/hcs/reference/hcshresult
// HRESULT constants matching internal\hcs\errors.go
// from MS-ERREF and HCS docs
const (
// HrNotImpl is the HRESULT for a not implemented function.
HrNotImpl = Hresult(-2147467263) // 0x80004001
Expand Down Expand Up @@ -55,128 +52,84 @@ const (
HrVmcomputeSystemAlreadyStopped = Hresult(-1070137072) // 0xC0370110
)

// TODO: update implementation to use go1.13 style errors with `errors.As` and co.

// StackTracer is an interface originating (but not exported) from the
// github.com/pkg/errors package. It defines something which can return a stack
// trace.
type StackTracer interface {
StackTrace() errors.StackTrace
}

// BaseStackTrace gets the earliest errors.StackTrace in the given error's cause
// stack. This will be the stack trace which reaches closest to the error's
// actual origin. It returns nil if no stack trace is found in the cause stack.
func BaseStackTrace(e error) errors.StackTrace {
Comment on lines -58 to -70
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW; this means we'd lose the stack-traces?

Probably relevant for this context; while in many cases stdlib will work, there's situations where it doesn't provide the same functionality; we recently got communication open with the pkg/ folks, and the pkg/errors repository has been un-archived so that it can be used for situations where stdlib is not sufficient;

cc @tonistiigi

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stack traces won't be lost, it can be retrieved with the runtime/debug, debug.Stack() method.
but yes, that would require a middleware or manual changes

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on the positive side, this would also mean that only panic'able errors should occur, atleast that's what i see emerging as a pattern out of this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @tonistiigi can provide more context; I know that BuildKit is a heavy user of pkg/errors (and stdlib errors didn't fit the bill there), but I don't know the full context where stdlib lacked the features needed.

type causer interface {
Cause() error
}
cause := e
var tracer StackTracer
for cause != nil {
serr, ok := cause.(StackTracer)
if ok {
tracer = serr
}
cerr, ok := cause.(causer)
if !ok {
break
}
cause = cerr.Cause()
}
if tracer == nil {
return nil
}
return tracer.StackTrace()
// Hresulter interface for errors with Hresult().
type Hresulter interface {
Hresult() Hresult
}

// baseHresultError is a basic HRESULT error.
type baseHresultError struct {
hresult Hresult
}

func (e *baseHresultError) Error() string {
return fmt.Sprintf("HRESULT: 0x%x", uint32(e.Hresult()))
}

func (e *baseHresultError) Hresult() Hresult {
return e.hresult
}

func (e *baseHresultError) Unwrap() error {
return nil
}

// wrappingHresultError wraps another error with an HRESULT.
type wrappingHresultError struct {
cause error
hresult Hresult
}

func (e *wrappingHresultError) Error() string {
return fmt.Sprintf("HRESULT 0x%x", uint32(e.Hresult())) + ": " + e.Cause().Error()
return fmt.Sprintf("HRESULT 0x%x: %s", uint32(e.Hresult()), e.cause.Error())
}

func (e *wrappingHresultError) Hresult() Hresult {
return e.hresult
}

func (e *wrappingHresultError) Cause() error {
return e.cause
}

func (e *wrappingHresultError) Unwrap() error {
return e.cause
}

func (e *wrappingHresultError) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", e.Cause())
fmt.Fprintf(s, "%+v", e.Unwrap())
return
}
fallthrough
case 's':
case 's', 'q':
_, _ = io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}
func (e *wrappingHresultError) StackTrace() errors.StackTrace {
type stackTracer interface {
StackTrace() errors.StackTrace
}
serr, ok := e.Cause().(stackTracer)
if !ok {
return nil
}
return serr.StackTrace()
}

// NewHresultError produces a new error with the given HRESULT.
func NewHresultError(hresult Hresult) error {
return &baseHresultError{hresult: hresult}
}

// WrapHresult produces a new error with the given HRESULT and wrapping the
// given error.
func WrapHresult(e error, hresult Hresult) error {
return &wrappingHresultError{
cause: e,
hresult: hresult,
}
// WrapHresult produces a new error with the given HRESULT wrapping the given error.
func WrapHresult(cause error, hresult Hresult) error {
return &wrappingHresultError{cause: cause, hresult: hresult}
}

// GetHresult iterates through the error's cause stack (similar to how the
// Cause function in github.com/pkg/errors operates). At the first error it
// encounters which implements the Hresult() method, it return's that error's
// HRESULT. This allows errors higher up in the cause stack to shadow the
// HRESULTs of errors lower down.
// GetHresult finds the first Hresult in the error chain using errors.As compatible loop.
func GetHresult(e error) (Hresult, error) {
type hresulter interface {
Hresult() Hresult
}
type causer interface {
Cause() error
}
cause := e
for cause != nil {
herr, ok := cause.(hresulter)
if ok {
for e != nil {
if herr, ok := e.(Hresulter); ok {
return herr.Hresult(), nil
}
cerr, ok := cause.(causer)
if !ok {
break
}
cause = cerr.Cause()
e = errors.Unwrap(e)
}
return -1, errors.Errorf("no HRESULT found in cause stack for error %s", e)
return 0, fmt.Errorf("no HRESULT found in error chain: %w", e)
}

// BaseStackTrace is removed as pkg/errors.StackTrace is deprecated.
// Stack traces can be obtained using %+v formatting on wrapped errors.
// TODO: if runtime/callers stack needed, add custom stack capturer.
3 changes: 2 additions & 1 deletion internal/gcs/guestconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"sync"

"errors"

"github.com/Microsoft/go-winio"
"github.com/Microsoft/go-winio/pkg/guid"
"github.com/Microsoft/hcsshim/internal/cow"
Expand All @@ -21,7 +23,6 @@ import (
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/guest/storage/crypt/crypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"testing"

"github.com/pkg/errors"
"errors"
)

const tempDir = "/tmp/dir/"
Expand Down
3 changes: 2 additions & 1 deletion internal/jobcontainers/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"unicode/utf16"
"unsafe"

"github.com/pkg/errors"
"errors"

"golang.org/x/sys/windows"
)

Expand Down
3 changes: 2 additions & 1 deletion internal/layers/lcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"path/filepath"
"strings"

"errors"

"github.com/Microsoft/go-winio/pkg/fs"
"github.com/containerd/containerd/api/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/Microsoft/hcsshim/internal/guestpath"
Expand Down
2 changes: 1 addition & 1 deletion internal/memory/pool.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package memory

import (
"github.com/pkg/errors"
"errors"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion internal/regopolicyinterpreter/regopolicyinterpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"os"
"sync"

"errors"

"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage/inmem"
"github.com/open-policy-agent/opa/topdown"
"github.com/pkg/errors"
)

type LogLevel int
Expand Down
3 changes: 2 additions & 1 deletion internal/shim/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (
"syscall"
"time"

"errors"

winio "github.com/Microsoft/go-winio"
"github.com/pkg/errors"
)

const shimBinaryFormat = "containerd-shim-%s-%s.exe"
Expand Down
3 changes: 1 addition & 2 deletions internal/verity/verity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/Microsoft/hcsshim/ext4/tar2ext4"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand All @@ -36,7 +35,7 @@ func ReadVeritySuperBlock(ctx context.Context, layerPath string) (*guestresource

dmvsb, err := dmverity.ReadDMVerityInfo(layerPath, ext4SizeInBytes)
if err != nil {
return nil, errors.Wrap(err, "failed to read dm-verity super block")
return nil, fmt.Errorf("failed to read dm-verity super block: %w", err)
}
log.G(ctx).WithFields(logrus.Fields{
"layerPath": layerPath,
Expand Down
3 changes: 2 additions & 1 deletion internal/vm/vmutils/gcs_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/logfields"

"github.com/pkg/errors"
"errors"

"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/securitypolicy/rego_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import (
"testing"
"time"

"errors"

"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
rpi "github.com/Microsoft/hcsshim/internal/regopolicyinterpreter"
"github.com/blang/semver/v4"
"github.com/open-policy-agent/opa/rego"
oci "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion pkg/securitypolicy/securitypolicy_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
"sync"
"time"

"errors"

"github.com/Microsoft/cosesign1go/pkg/cosesign1"
didx509resolver "github.com/Microsoft/didx509go/pkg/did-x509-resolver"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
"github.com/Microsoft/hcsshim/pkg/amdsevsnp"
"github.com/Microsoft/hcsshim/pkg/annotations"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand Down
3 changes: 2 additions & 1 deletion pkg/securitypolicy/securitypolicyenforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"fmt"
"syscall"

"errors"

"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
oci "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)

type createEnforcerFunc func(base64EncodedPolicy string, criMounts, criPrivilegedMounts []oci.Mount, maxErrorMessageLength int) (SecurityPolicyEnforcer, error)
Expand Down
Loading