Skip to content

Commit 466ad84

Browse files
mmorel-35ndeloof
authored andcommitted
use standard errors package
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 3c8d69f commit 466ad84

27 files changed

+107
-103
lines changed

.golangci.yml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
run:
2-
deadline: 2m
3-
1+
issues:
2+
max-issues-per-linter: 0
3+
max-same-issues: 0
44
linters:
55
disable-all: true
66
enable:
7+
- errorlint
78
- gocritic
89
- gofmt
910
- goimports
10-
- revive
11+
- gomodguard
1112
- gosimple
13+
- govet
1214
- ineffassign
1315
- misspell
1416
- nakedret
15-
- govet
17+
- revive
1618
linters-settings:
1719
gocritic:
1820
# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks.
@@ -25,3 +27,12 @@ linters-settings:
2527
- paramTypeCombine
2628
- unnamedResult
2729
- whyNoLint
30+
gomodguard:
31+
blocked:
32+
modules:
33+
- github.com/pkg/errors:
34+
recommendations:
35+
- errors
36+
- fmt
37+
run:
38+
timeout: 2m

cli/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ package cli
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"io"
2223
"os"
2324
"path/filepath"
2425
"strconv"
2526
"strings"
2627

27-
"github.com/pkg/errors"
2828
"github.com/sirupsen/logrus"
2929

3030
"github.com/compose-spec/compose-go/v2/consts"
@@ -472,7 +472,7 @@ func getConfigPathsFromOptions(options *ProjectOptions) ([]string, error) {
472472
if len(options.ConfigPaths) != 0 {
473473
return absolutePaths(options.ConfigPaths)
474474
}
475-
return nil, errors.Wrap(errdefs.ErrNotFound, "no configuration file provided")
475+
return nil, fmt.Errorf("no configuration file provided: %w", errdefs.ErrNotFound)
476476
}
477477

478478
func findFiles(names []string, pwd string) []string {

dotenv/env.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ package dotenv
1818

1919
import (
2020
"bytes"
21+
"fmt"
2122
"os"
2223
"path/filepath"
23-
24-
"github.com/pkg/errors"
2524
)
2625

2726
func GetEnvFromFile(currentEnv map[string]string, filenames []string) (map[string]string, error) {
@@ -36,7 +35,7 @@ func GetEnvFromFile(currentEnv map[string]string, filenames []string) (map[strin
3635

3736
s, err := os.Stat(dotEnvFile)
3837
if os.IsNotExist(err) {
39-
return envMap, errors.Errorf("Couldn't find env file: %s", dotEnvFile)
38+
return envMap, fmt.Errorf("Couldn't find env file: %s", dotEnvFile)
4039
}
4140
if err != nil {
4241
return envMap, err
@@ -46,12 +45,12 @@ func GetEnvFromFile(currentEnv map[string]string, filenames []string) (map[strin
4645
if len(filenames) == 0 {
4746
return envMap, nil
4847
}
49-
return envMap, errors.Errorf("%s is a directory", dotEnvFile)
48+
return envMap, fmt.Errorf("%s is a directory", dotEnvFile)
5049
}
5150

5251
b, err := os.ReadFile(dotEnvFile)
5352
if os.IsNotExist(err) {
54-
return nil, errors.Errorf("Couldn't read env file: %s", dotEnvFile)
53+
return nil, fmt.Errorf("Couldn't read env file: %s", dotEnvFile)
5554
}
5655
if err != nil {
5756
return envMap, err
@@ -66,7 +65,7 @@ func GetEnvFromFile(currentEnv map[string]string, filenames []string) (map[strin
6665
return v, ok
6766
})
6867
if err != nil {
69-
return envMap, errors.Wrapf(err, "failed to read %s", dotEnvFile)
68+
return envMap, fmt.Errorf("failed to read %s: %w", dotEnvFile, err)
7069
}
7170
for k, v := range env {
7271
envMap[k] = v

dotenv/godotenv_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotenv
22

33
import (
44
"bytes"
5+
"errors"
56
"os"
67
"path/filepath"
78
"strings"
@@ -48,7 +49,8 @@ func loadEnvAndCompareValues(t *testing.T, loader func(files ...string) error, e
4849

4950
func TestLoadWithNoArgsLoadsDotEnv(t *testing.T) {
5051
err := Load()
51-
pathError := err.(*os.PathError)
52+
var pathError *os.PathError
53+
errors.As(err, &pathError)
5254
if pathError == nil || pathError.Op != "open" || pathError.Path != ".env" {
5355
t.Errorf("Didn't try and open .env by default")
5456
}

format/volume.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
package format
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"strings"
2123
"unicode"
2224
"unicode/utf8"
2325

2426
"github.com/compose-spec/compose-go/v2/types"
25-
"github.com/pkg/errors"
2627
)
2728

2829
const endOfSpec = rune(0)
@@ -48,7 +49,7 @@ func ParseVolume(spec string) (types.ServiceVolumeConfig, error) {
4849
case char == ':' || char == endOfSpec:
4950
if err := populateFieldFromBuffer(char, buffer, &volume); err != nil {
5051
populateType(&volume)
51-
return volume, errors.Wrapf(err, "invalid spec: %s", spec)
52+
return volume, fmt.Errorf("invalid spec: %s: %w", spec, err)
5253
}
5354
buffer = nil
5455
default:

interpolation/interpolation.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
package interpolation
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"os"
2123

2224
"github.com/compose-spec/compose-go/v2/template"
2325
"github.com/compose-spec/compose-go/v2/tree"
24-
"github.com/pkg/errors"
2526
)
2627

2728
// Options supported by Interpolate
@@ -80,7 +81,10 @@ func recursiveInterpolate(value interface{}, path tree.Path, opts Options) (inte
8081
return newValue, nil
8182
}
8283
casted, err := caster(newValue)
83-
return casted, newPathError(path, errors.Wrap(err, "failed to cast to expected type"))
84+
if err != nil {
85+
return casted, newPathError(path, fmt.Errorf("failed to cast to expected type: %w", err))
86+
}
87+
return casted, nil
8488

8589
case map[string]interface{}:
8690
out := map[string]interface{}{}
@@ -110,15 +114,16 @@ func recursiveInterpolate(value interface{}, path tree.Path, opts Options) (inte
110114
}
111115

112116
func newPathError(path tree.Path, err error) error {
113-
switch err := err.(type) {
114-
case nil:
117+
var ite *template.InvalidTemplateError
118+
switch {
119+
case err == nil:
115120
return nil
116-
case *template.InvalidTemplateError:
117-
return errors.Errorf(
121+
case errors.As(err, &ite):
122+
return fmt.Errorf(
118123
"invalid interpolation format for %s.\nYou may need to escape any $ with another $.\n%s",
119-
path, err.Template)
124+
path, ite.Template)
120125
default:
121-
return errors.Wrapf(err, "error while interpolating %s", path)
126+
return fmt.Errorf("error while interpolating %s: %w", path, err)
122127
}
123128
}
124129

loader/include.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/compose-spec/compose-go/v2/dotenv"
2828
interp "github.com/compose-spec/compose-go/v2/interpolation"
2929
"github.com/compose-spec/compose-go/v2/types"
30-
"github.com/pkg/errors"
3130
)
3231

3332
// loadIncludeConfig parse the require config from raw yaml
@@ -64,7 +63,7 @@ func ApplyInclude(ctx context.Context, configDetails types.ConfigDetails, model
6463
for _, f := range included {
6564
if f == mainFile {
6665
included = append(included, mainFile)
67-
return errors.Errorf("include cycle detected:\n%s\n include %s", included[0], strings.Join(included[1:], "\n include "))
66+
return fmt.Errorf("include cycle detected:\n%s\n include %s", included[0], strings.Join(included[1:], "\n include "))
6867
}
6968
}
7069

loader/interpolate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package loader
1818

1919
import (
20+
"fmt"
2021
"strconv"
2122
"strings"
2223

2324
interp "github.com/compose-spec/compose-go/v2/interpolation"
2425
"github.com/compose-spec/compose-go/v2/tree"
25-
"github.com/pkg/errors"
2626
"github.com/sirupsen/logrus"
2727
)
2828

@@ -112,6 +112,6 @@ func toBoolean(value string) (interface{}, error) {
112112
logrus.Warnf("%q for boolean is not supported by YAML 1.2, please use `false`", value)
113113
return false, nil
114114
default:
115-
return nil, errors.Errorf("invalid boolean: %s", value)
115+
return nil, fmt.Errorf("invalid boolean: %s", value)
116116
}
117117
}

loader/loader.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package loader
1919
import (
2020
"bytes"
2121
"context"
22+
"errors"
2223
"fmt"
2324
"io"
2425
"os"
@@ -39,7 +40,6 @@ import (
3940
"github.com/compose-spec/compose-go/v2/types"
4041
"github.com/compose-spec/compose-go/v2/validation"
4142
"github.com/mitchellh/mapstructure"
42-
"github.com/pkg/errors"
4343
"gopkg.in/yaml.v3"
4444
)
4545

@@ -226,7 +226,7 @@ func parseYAML(decoder *yaml.Decoder) (map[string]interface{}, PostProcessor, er
226226
}
227227
cfgMap, ok := cfg.(map[interface{}]interface{})
228228
if !ok {
229-
return nil, nil, errors.Errorf("Top-level object must be a mapping")
229+
return nil, nil, errors.New("Top-level object must be a mapping")
230230
}
231231
converted, err := convertToStringKeysRecursive(cfgMap, "")
232232
if err != nil {
@@ -244,7 +244,7 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
244244
// LoadWithContext reads a ConfigDetails and returns a fully loaded configuration
245245
func LoadWithContext(ctx context.Context, configDetails types.ConfigDetails, options ...func(*Options)) (*types.Project, error) {
246246
if len(configDetails.ConfigFiles) < 1 {
247-
return nil, errors.Errorf("No files specified")
247+
return nil, errors.New("No files specified")
248248
}
249249

250250
opts := &Options{
@@ -351,7 +351,7 @@ func loadYamlModel(ctx context.Context, config types.ConfigDetails, opts *Option
351351
var raw interface{}
352352
processor := &ResetProcessor{target: &raw}
353353
err := decoder.Decode(processor)
354-
if err == io.EOF {
354+
if err != nil && errors.Is(err, io.EOF) {
355355
break
356356
}
357357
if err != nil {
@@ -402,7 +402,7 @@ func load(ctx context.Context, configDetails types.ConfigDetails, opts *Options,
402402
for _, f := range loaded {
403403
if f == mainFile {
404404
loaded = append(loaded, mainFile)
405-
return nil, errors.Errorf("include cycle detected:\n%s\n include %s", loaded[0], strings.Join(loaded[1:], "\n include "))
405+
return nil, fmt.Errorf("include cycle detected:\n%s\n include %s", loaded[0], strings.Join(loaded[1:], "\n include "))
406406
}
407407
}
408408
loaded = append(loaded, mainFile)
@@ -692,7 +692,7 @@ func formatInvalidKeyError(keyPrefix string, key interface{}) error {
692692
} else {
693693
location = fmt.Sprintf("in %s", keyPrefix)
694694
}
695-
return errors.Errorf("Non-string key %s: %#v", location, key)
695+
return fmt.Errorf("Non-string key %s: %#v", location, key)
696696
}
697697

698698
// Windows path, c:\\my\\path\\shiny, need to be changed to be compatible with

loader/normalize.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/compose-spec/compose-go/v2/errdefs"
2424
"github.com/compose-spec/compose-go/v2/types"
25-
"github.com/pkg/errors"
2625
"github.com/sirupsen/logrus"
2726
)
2827

@@ -246,7 +245,7 @@ func relocateLogOpt(s *types.ServiceConfig) error {
246245
if _, ok := s.Logging.Options[k]; !ok {
247246
s.Logging.Options[k] = v
248247
} else {
249-
return errors.Wrap(errdefs.ErrInvalid, "can't use both 'log_opt' (deprecated) and 'logging.options'")
248+
return fmt.Errorf("can't use both 'log_opt' (deprecated) and 'logging.options': %w", errdefs.ErrInvalid)
250249
}
251250
}
252251
}
@@ -262,7 +261,7 @@ func relocateLogDriver(s *types.ServiceConfig) error {
262261
if s.Logging.Driver == "" {
263262
s.Logging.Driver = s.LogDriver
264263
} else {
265-
return errors.Wrap(errdefs.ErrInvalid, "can't use both 'log_driver' (deprecated) and 'logging.driver'")
264+
return fmt.Errorf("can't use both 'log_driver' (deprecated) and 'logging.driver': %w", errdefs.ErrInvalid)
266265
}
267266
}
268267
return nil
@@ -277,7 +276,7 @@ func relocateDockerfile(s *types.ServiceConfig) error {
277276
if s.Dockerfile == "" {
278277
s.Build.Dockerfile = s.Dockerfile
279278
} else {
280-
return errors.Wrap(errdefs.ErrInvalid, "can't use both 'dockerfile' (deprecated) and 'build.dockerfile'")
279+
return fmt.Errorf("can't use both 'dockerfile' (deprecated) and 'build.dockerfile': %w", errdefs.ErrInvalid)
281280
}
282281
}
283282
return nil

0 commit comments

Comments
 (0)