Skip to content

Commit

Permalink
More detailed error handling when we fail to load JSON files
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
motns committed May 14, 2020
1 parent 18a788c commit 12a6fc9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
11 changes: 8 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func loadOverrides(paths []string) (map[string]string, error) {
for _, path := range paths {
m, err := loadOverride(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w; Failed to load override from file: %s", err, path)
}

for k, v := range m {
Expand All @@ -44,11 +44,16 @@ func loadOverride(path string) (map[string]string, error) {

jsonStr, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w; Failed to read override file", err)
}

if err := json.Unmarshal(jsonStr, &overrides); err != nil {
return nil, err
switch t := err.(type) {
case *json.SyntaxError:
return nil, fmt.Errorf("%w; Failed to unmarshal json, error at position %d (\"%s\")", err, t.Offset, SafeSlice(string(jsonStr), int(t.Offset - 10), int(t.Offset + 10)))
default:
return nil, fmt.Errorf("%w; Failed to unmarshal json", err)
}
}

return overrides, nil
Expand Down
10 changes: 8 additions & 2 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
)

Expand Down Expand Up @@ -59,11 +60,16 @@ func loadDB(dbFile string) (ConfigstoreDB, error) {

jsonStr, err := ioutil.ReadFile(dbFile)
if err != nil {
return ConfigstoreDB{}, err
return ConfigstoreDB{}, fmt.Errorf("%w; Failed to load DB file: %s", err, dbFile)
}

if err := json.Unmarshal(jsonStr, &db); err != nil {
return ConfigstoreDB{}, err
switch t := err.(type) {
case *json.SyntaxError:
return ConfigstoreDB{}, fmt.Errorf("%w; Failed to unmarshal json from DB file \"%s\", error at position %d (\"%s\")", err, dbFile, t.Offset, SafeSlice(string(jsonStr), int(t.Offset - 10), int(t.Offset + 10)))
default:
return ConfigstoreDB{}, fmt.Errorf("%w; Failed to unmarshal json from DB file \"%s\"", err, dbFile)
}
}

return db.validate()
Expand Down
19 changes: 19 additions & 0 deletions client/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package client

func SafeSlice(str string, start int, end int) string {
if len(str) == 0 {
return str
}

s := start
if s < 0 {
s = 0
}

e := end
if e > len(str) {
e = len(str)
}

return str[s:e]
}
12 changes: 9 additions & 3 deletions cmd/configstore/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,19 @@ func ParseEnv(s string, basedir string, validate bool) (Env, error) {
func LoadEnvOverride(basedir string) (map[string]string, error) {
var overrides = make(map[string]string)

jsonStr, err := ioutil.ReadFile(basedir + "/override.json")
path := basedir + "/override.json"
jsonStr, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w; Failed to load override file: %s", err, path)
}

if err := json.Unmarshal(jsonStr, &overrides); err != nil {
return nil, err
switch t := err.(type) {
case *json.SyntaxError:
return nil, fmt.Errorf("%w; Failed to unmarshal json for subenv \"%s\", error at position %d (\"%s\")", err, basedir, t.Offset, client.SafeSlice(string(jsonStr), int(t.Offset - 10), int(t.Offset + 10)))
default:
return nil, fmt.Errorf("%w; Failed to unmarshal json for subenv \"%s\"", err)
}
}

return overrides, nil
Expand Down

0 comments on commit 12a6fc9

Please sign in to comment.