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

Provide better errors caused by context cancellation #107

Open
radeksimko opened this issue Nov 18, 2020 · 3 comments
Open

Provide better errors caused by context cancellation #107

radeksimko opened this issue Nov 18, 2020 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@radeksimko
Copy link
Member

#91 was closed partially because we were worried about implications in terms of backwards incompatibility.

I do however believe that the library should provide decent error messages - i.e. not context deadline exceeded by default, so I'm hoping this can be revisited before v1 as that gives us chance to make some breaking changes.

@radeksimko radeksimko added the enhancement New feature or request label Nov 18, 2020
@radeksimko radeksimko added this to the v1.0.0 milestone Nov 18, 2020
@paultyng
Copy link
Contributor

#94 would probably solve this I think?

@radeksimko
Copy link
Member Author

AFAICT that would still require the caller to add errors.Is/errors.As on their end, which seems appropriate for handling some command-specific errors, but not so much for handling every error?

@paultyng
Copy link
Contributor

paultyng commented Nov 18, 2020

That is in fact what Go encourages so that people don't rely on direct casting but use those functions always:

var ErrPermission = errors.New("permission denied")

// DoSomething returns an error wrapping ErrPermission if the user
// does not have permission to do something.
func DoSomething() error {
    if !userHasPermission() {
        // If we return ErrPermission directly, callers might come
        // to depend on the exact error value, writing code like this:
        //
        //     if err := pkg.DoSomething(); err == pkg.ErrPermission { … }
        //
        // This will cause problems if we want to add additional
        // context to the error in the future. To avoid this, we
        // return an error wrapping the sentinel so that users must
        // always unwrap it:
        //
        //     if err := pkg.DoSomething(); errors.Is(err, pkg.ErrPermission) { ... }
        return fmt.Errorf("%w", ErrPermission)
    }
    // ...
}

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

No branches or pull requests

2 participants