Skip to content

Commit 6f0a94d

Browse files
committed
libct/error.go: rm ConfigError, add ErrInvalidConfig
Replace ConfigError type with ErrInvalidConfig variable. This is somewhat complicated, as we need to make all errors returned from config validator to be ErrInvalidConfig, while keeping them unwrappable. This is implemented by introducing a type which holds an underlying error, and adding Is method that answers that the error is ErrInvalidConfig. [v2: silence the linter] Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent 9a1b209 commit 6f0a94d

5 files changed

Lines changed: 40 additions & 22 deletions

File tree

libcontainer/configs/validate/validator.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"golang.org/x/sys/unix"
1717
)
1818

19+
var ErrInvalidConfig = errors.New("invalid configuration")
20+
1921
type Validator interface {
2022
Validate(*configs.Config) error
2123
}
@@ -26,6 +28,24 @@ func New() Validator {
2628

2729
type ConfigValidator struct{}
2830

31+
// invalidConfig type is needed to make any error returned from Validator
32+
// to be ErrInvalidConfig.
33+
type invalidConfig struct {
34+
err error
35+
}
36+
37+
func (e *invalidConfig) Error() string {
38+
return ErrInvalidConfig.Error() + ": " + e.err.Error()
39+
}
40+
41+
func (e *invalidConfig) Is(target error) bool {
42+
return target == ErrInvalidConfig //nolint:errorlint // this method assumes direct comparison
43+
}
44+
45+
func (e *invalidConfig) Unwrap() error {
46+
return e.err
47+
}
48+
2949
type check func(config *configs.Config) error
3050

3151
func (v *ConfigValidator) Validate(config *configs.Config) error {
@@ -43,7 +63,7 @@ func (v *ConfigValidator) Validate(config *configs.Config) error {
4363
}
4464
for _, c := range checks {
4565
if err := c(config); err != nil {
46-
return err
66+
return &invalidConfig{err: err}
4767
}
4868
}
4969
// Relaxed validation rules for backward compatibility
@@ -52,7 +72,7 @@ func (v *ConfigValidator) Validate(config *configs.Config) error {
5272
}
5373
for _, c := range warns {
5474
if err := c(config); err != nil {
55-
logrus.WithError(err).Warnf("invalid configuration")
75+
logrus.WithError(err).Warn(ErrInvalidConfig)
5676
}
5777
}
5878
return nil

libcontainer/configs/validate/validator_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestValidateWithInvalidRootfs(t *testing.T) {
3535

3636
validator := validate.New()
3737
err := validator.Validate(config)
38+
t.Logf("error: %v", err)
3839
if err == nil {
3940
t.Error("Expected error to occur but it was nil")
4041
}

libcontainer/container_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func (c *linuxContainer) Start(process *Process) error {
231231
c.m.Lock()
232232
defer c.m.Unlock()
233233
if c.config.Cgroups.Resources.SkipDevices {
234-
return &ConfigError{"can't start container with SkipDevices set"}
234+
return fmt.Errorf("%w: can't start container with SkipDevices set", ErrInvalidConfig)
235235
}
236236
if process.Init {
237237
if err := c.createExecFifo(); err != nil {

libcontainer/error.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package libcontainer
22

3-
import "errors"
3+
import (
4+
"errors"
45

5-
var (
6-
ErrExist = errors.New("container with given ID already exists")
7-
ErrInvalidID = errors.New("invalid container ID format")
8-
ErrNotExist = errors.New("container does not exist")
9-
ErrPaused = errors.New("container paused")
10-
ErrRunning = errors.New("container still running")
11-
ErrNotRunning = errors.New("container not running")
12-
ErrNotPaused = errors.New("container not paused")
6+
"github.com/opencontainers/runc/internal/configs/validate"
137
)
148

15-
type ConfigError struct {
16-
details string
17-
}
18-
19-
func (e *ConfigError) Error() string {
20-
return "invalid configuration: " + e.details
21-
}
9+
var (
10+
ErrExist = errors.New("container with given ID already exists")
11+
ErrInvalidID = errors.New("invalid container ID format")
12+
ErrNotExist = errors.New("container does not exist")
13+
ErrPaused = errors.New("container paused")
14+
ErrRunning = errors.New("container still running")
15+
ErrNotRunning = errors.New("container not running")
16+
ErrNotPaused = errors.New("container not paused")
17+
ErrInvalidConfig = validate.ErrInvalidConfig
18+
)

libcontainer/factory_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ type LinuxFactory struct {
251251

252252
func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, error) {
253253
if l.Root == "" {
254-
return nil, &ConfigError{"invalid root"}
254+
return nil, fmt.Errorf("%w: invalid root", ErrInvalidConfig)
255255
}
256256
if err := l.validateID(id); err != nil {
257257
return nil, err
258258
}
259259
if err := l.Validator.Validate(config); err != nil {
260-
return nil, &ConfigError{err.Error()}
260+
return nil, fmt.Errorf("%w: %v", ErrInvalidConfig, err)
261261
}
262262
containerRoot, err := securejoin.SecureJoin(l.Root, id)
263263
if err != nil {
@@ -294,7 +294,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
294294

295295
func (l *LinuxFactory) Load(id string) (Container, error) {
296296
if l.Root == "" {
297-
return nil, &ConfigError{"invalid root"}
297+
return nil, fmt.Errorf("%w: invalid root", ErrInvalidConfig)
298298
}
299299
// when load, we need to check id is valid or not.
300300
if err := l.validateID(id); err != nil {

0 commit comments

Comments
 (0)