Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github.com/pkg/errors is archived #1614

Open
Tracked by #114942
pacoxu opened this issue Jan 9, 2023 · 5 comments · May be fixed by #2290
Open
Tracked by #114942

github.com/pkg/errors is archived #1614

pacoxu opened this issue Jan 9, 2023 · 5 comments · May be fixed by #2290

Comments

@pacoxu
Copy link

pacoxu commented Jan 9, 2023

follow up of kubernetes/kubernetes#114912 and see kubernetes/kubernetes#113627 for some details.

@kolyshkin
Copy link
Contributor

There are 419 cases of using errors.Wrapf which should be converted to fmt.Errorf in the following manner:

-               return errors.Wrapf(errdefs.ErrNotFound, "exec: '%s' in task: '%s' not found", wpse.tid, wpse.tid)
+               return fmt.Errorf("exec: %q in task: %q: %w", wpse.tid, wpse.tid, errdefs.ErrNotFound)
        if sid != req.ID {
-               return nil, errors.Wrapf(
-                       errdefs.ErrFailedPrecondition,
-                       "expected annotation '%s': '%s' got '%s'",
+               return nil, fmt.Errorf(
+                       "expected annotation %q: %q, got %q: %w",
                        annotations.KubernetesSandboxID,
                        req.ID,
-                       sid)
+                       sid,
+                       errdefs.ErrFailedPrecondition)
        }

(I also took a liberty to add a missing comma, and change %s to %q which will add double quotes around the string and also escape non-printable characters which usually helps in debugging.

@kolyshkin
Copy link
Contributor

@katiewasnothere @kevpar @helsaawy PTAL 🙏🏻

@kolyshkin
Copy link
Contributor

Also, in some cases when an error being wrapped already contains the text we want, it's better to take that into account to avoid stuttering, e.g.:

-               return nil, errors.Wrapf(errdefs.ErrAlreadyExists, "task with id: '%s' already exists", req.ID)
+               return nil, fmt.Errorf("task with id: %q: %w", req.ID, errdefs.ErrAlreadyExists)

@helsaawy
Copy link
Contributor

helsaawy commented Oct 3, 2024

unfortunately a simple replace won't work, since we rely on the stack traces that "github.com/pkg/errors" adds to the errors in certain cases, so we would need to reimplement the functionality:

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

if stack := gcserr.BaseStackTrace(errForResponse); stack != nil {
bottomFrame := stack[0]
stackString = fmt.Sprintf("%+v", stack)
fileName = fmt.Sprintf("%s", bottomFrame)
lineNumberStr := fmt.Sprintf("%d", bottomFrame)
if n, err := strconv.ParseUint(lineNumberStr, 10, 32); err == nil {
lineNumber = uint32(n)
} else {
logrus.WithFields(logrus.Fields{
"line-number": lineNumberStr,
logrus.ErrorKey: err,
}).Error("opengcs::bridge::setErrorForResponseBase - failed to parse line number, using -1 instead")
}
functionName = fmt.Sprintf("%n", bottomFrame)
}

@kolyshkin kolyshkin linked a pull request Oct 9, 2024 that will close this issue
@kolyshkin
Copy link
Contributor

unfortunately a simple replace won't work, since we rely on the stack traces that "github.com/pkg/errors" adds to the errors in certain cases, so we would need to reimplement the functionality:

I don't think this is a "must have" functionality here. It is only used from a single place (setErrorForResponseBase in internal/guest/bridge/bridge.go), and in there it seems to be optional. In any case, if an error stack trace is needed, it is to be reimplemented in some other way (say, whoever returns an error adds a stack trace explicitly).

I am (trying to) remove this as part of #2290.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants