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

libcontainer: rm own error system #3033

Merged
merged 3 commits into from
Jul 1, 2021

Conversation

kolyshkin
Copy link
Contributor

@kolyshkin kolyshkin commented Jun 22, 2021

This removes libcontainer's own error wrapping system, consisting of a
few types and functions, aimed at typization, wrapping and unwrapping
of errors, as well as saving error stack traces.

Since Go 1.13 now provides its own error wrapping mechanism and a few
related functions, it makes sense to switch to it.

A few things worth mentioning:

  1. We lose stack traces (which were never shown anyway).

  2. Users of libcontainer that relied on particular errors (like
    ContainerNotExists) need to switch to using errors.Is with
    the new errors defined in libcontainer/error.go.
    An example of such conversion:

-		if err != nil {
-			var lerr libcontainer.Error
-			if errors.As(err, &lerr) && lerr.Code() == libcontainer.ContainerNotExists {
+			if errors.Is(err, libcontainer.ErrNotExist) {
  1. (Implementation detail) encoding/json is unable to unmarshal the built-in
    error type, so we have to introduce initError and wrap the errors into it
    (basically passing the error as a string). This is the same
    as it was before, just a tad simpler (actually the initError
    itself is a type that got removed in commit afa8443; also suddenly
    ierr variable name makes sense now).

Proposed changelog entry

  • libcontainer.Error type, together with error codes, is removed in favor of new errors. Users relying on removed types and codes should adopt to use errors.Is with new errors (see libcontainer/errors.go). (libcontainer: rm own error system #3033)

@kolyshkin kolyshkin force-pushed the rm-own-errors branch 4 times, most recently from e6fb240 to 35d2615 Compare June 22, 2021 01:36
@kolyshkin kolyshkin marked this pull request as ready for review June 22, 2021 08:43
@kolyshkin kolyshkin changed the title [DNM] libcontainer: rm own error system libcontainer: rm own error system Jun 22, 2021
@kolyshkin kolyshkin added this to the 1.1.0 milestone Jun 22, 2021
@kolyshkin
Copy link
Contributor Author

CI failure is probably a flake (#3035); CI restarted

It is not used since commit 244c9fc.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
Copy link
Contributor Author

Rebased on top of #3011

@zhsj
Copy link
Contributor

zhsj commented Jun 24, 2021

With removal of generic_error.go, probably remove libcontainer/stacktrace together?

@@ -74,71 +74,39 @@ type BaseContainer interface {
ID() string

// Returns the current status of the container.
//
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This documentation (about which errors can be returned) is removed because

  • libcontainer/error.go already list possible errors;
  • it's not easy to keep these lists up to date.

This removes libcontainer's own error wrapping system, consisting of a
few types and functions, aimed at typization, wrapping and unwrapping
of errors, as well as saving error stack traces.

Since Go 1.13 now provides its own error wrapping mechanism and a few
related functions, it makes sense to switch to it.

While doing that, improve some error messages so that they start
with "error", "unable to", or "can't".

A few things that are worth mentioning:

1. We lose stack traces (which were never shown anyway).

2. Users of libcontainer that relied on particular errors (like
   ContainerNotExists) need to switch to using errors.Is with
   the new errors defined in error.go.

3. encoding/json is unable to unmarshal the built-in error type,
   so we have to introduce initError and wrap the errors into it
   (basically passing the error as a string). This is the same
   as it was before, just a tad simpler (actually the initError
   is a type that got removed in commit afa8443; also suddenly
   ierr variable name makes sense now).

Signed-off-by: Kir Kolyshkin <[email protected]>
This removes the libcontainer/stacktrace package, which, as of previous
commit, is no longer used.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
Copy link
Contributor Author

With removal of generic_error.go, probably remove libcontainer/stacktrace together?

Thanks, @zhsj! Removed.

Copy link
Member

@cyphar cyphar left a comment

Choose a reason for hiding this comment

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

LGTM. Let's merge this before #3049 so you don't need to rebase this one.

}
if err := l.validateID(id); err != nil {
return nil, err
}
if err := l.Validator.Validate(config); err != nil {
return nil, newGenericError(err, ConfigInvalid)
return nil, &ConfigError{err.Error()}
Copy link
Member

Choose a reason for hiding this comment

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

Can we use %w here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this looks a tad weird. I might change this to using an error later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please see #3056

@@ -249,28 +251,28 @@ type LinuxFactory struct {

func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, error) {
if l.Root == "" {
return nil, newGenericError(errors.New("invalid root"), ConfigInvalid)
return nil, &ConfigError{"invalid root"}
Copy link
Member

Choose a reason for hiding this comment

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

Do we need &ConfigError{}?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a replacement for "ConfigInvalid" error code, so a caller can distinguish configuration errors from everything else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right @AkihiroSuda, it's better not to have it, but I was not able to come with a good solution before. Now I am; please see #3056

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

Successfully merging this pull request may close these issues.

4 participants