Skip to content

Commit 3158ba3

Browse files
authored
Merge pull request #385 from TomWright/issue-384/improve-file-open-errors
Make open errors more descriptive
2 parents ec9b19c + f4b4e1f commit 3158ba3

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Support for `--indent` flag.
13+
- More descriptive errors when dasel fails to open a file.
1314

1415
## [v2.5.0] - 2023-11-28
1516

internal/command/options.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package command
22

33
import (
44
"fmt"
5+
"io"
6+
"os"
7+
"strings"
8+
59
"github.com/spf13/cobra"
610
"github.com/tomwright/dasel/v2"
711
"github.com/tomwright/dasel/v2/dencoding"
812
"github.com/tomwright/dasel/v2/storage"
9-
"io"
10-
"os"
11-
"strings"
1213
)
1314

1415
type readOptions struct {
@@ -70,7 +71,7 @@ func (o *readOptions) rootValue(cmd *cobra.Command) (dasel.Value, error) {
7071
} else {
7172
f, err := os.Open(o.FilePath)
7273
if err != nil {
73-
return dasel.Value{}, fmt.Errorf("could not open file: %s: %w", o.FilePath, err)
74+
return dasel.Value{}, fmt.Errorf("could not open file for reading: %s: %w", o.FilePath, err)
7475
}
7576
defer f.Close()
7677
reader = f
@@ -177,7 +178,7 @@ func (o *writeOptions) writeValues(cmd *cobra.Command, readOptions *readOptions,
177178
} else {
178179
f, err := os.Create(o.FilePath)
179180
if err != nil {
180-
return fmt.Errorf("could not open file: %s: %w", o.FilePath, err)
181+
return fmt.Errorf("could not open file for writing: %s: %w", o.FilePath, err)
181182
}
182183
defer f.Close()
183184
writer = f

storage/parser.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package storage
22

33
import (
44
"fmt"
5-
"github.com/tomwright/dasel/v2"
65
"io"
76
"os"
87
"path/filepath"
98
"strings"
9+
10+
"github.com/tomwright/dasel/v2"
1011
)
1112

1213
var readParsersByExtension = map[string]ReadParser{}
@@ -107,7 +108,7 @@ func NewWriteParserFromString(parser string) (WriteParser, error) {
107108
func LoadFromFile(filename string, p ReadParser, options ...ReadWriteOption) (dasel.Value, error) {
108109
f, err := os.Open(filename)
109110
if err != nil {
110-
return dasel.Value{}, fmt.Errorf("could not open file: %w", err)
111+
return dasel.Value{}, fmt.Errorf("could not open file for reading: %w", err)
111112
}
112113
return Load(p, f, options...)
113114
}

storage/parser_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package storage_test
33
import (
44
"bytes"
55
"errors"
6-
"github.com/tomwright/dasel/v2"
7-
"github.com/tomwright/dasel/v2/dencoding"
8-
"github.com/tomwright/dasel/v2/storage"
96
"reflect"
107
"strings"
118
"testing"
9+
10+
"github.com/tomwright/dasel/v2"
11+
"github.com/tomwright/dasel/v2/dencoding"
12+
"github.com/tomwright/dasel/v2/storage"
1213
)
1314

1415
func TestUnknownParserErr_Error(t *testing.T) {
@@ -205,7 +206,7 @@ func TestLoadFromFile(t *testing.T) {
205206
})
206207
t.Run("BaseFilePath", func(t *testing.T) {
207208
_, err := storage.LoadFromFile("x.json", &storage.JSONParser{})
208-
if err == nil || !strings.Contains(err.Error(), "could not open file") {
209+
if err == nil || !strings.Contains(err.Error(), "could not open file for reading") {
209210
t.Errorf("unexpected error: %v", err)
210211
return
211212
}

0 commit comments

Comments
 (0)