Skip to content

Commit

Permalink
Merge pull request #48 from ysim/improve-debugging
Browse files Browse the repository at this point in the history
Improved error handling for front matter parsing
  • Loading branch information
ysim committed Nov 5, 2020
2 parents 2c8a36d + 7513c1e commit 487f2df
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.4.0 (xxxx-xx-xx)

* Improve error handling for front matter parsing. (#47)

v0.3.0 (2020-10-20)

* Introduction of `list -key=...` subcommand that lists values for a given key. (#23)
Expand Down
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func ParseFrontMatter(fmBytes []byte) (map[string][]string, error) {
var rfm map[string]interface{}
err := yaml.Unmarshal([]byte(fmBytes), &rfm)
if err != nil {
return nil, err
return nil, errors.New(fmt.Sprintf("Error while unmarshalling yaml: %s", err))
}

// Now make a type assertion into map[string][]string to make querying
Expand All @@ -71,7 +71,12 @@ func ParseFrontMatter(fmBytes []byte) (map[string][]string, error) {
coercedArray := v.([]interface{})
vArray := make([]string, len(coercedArray))
for _, item := range coercedArray {
vArray = append(vArray, item.(string))
switch item.(type) {
case string:
vArray = append(vArray, item.(string))
default:
return nil, errors.New(fmt.Sprintf("Was not able to process item of type %T in an array. This file's front matter was not parsed further.", item))
}
}
// Get a new slice with the empty strings removed
fm[k] = CleanFields(vArray)
Expand Down Expand Up @@ -185,7 +190,7 @@ func init() {
// Log levels
log.SetFormatter(&log.TextFormatter{PadLevelText: true})
log.SetOutput(os.Stdout)
log.SetLevel(log.FatalLevel)
log.SetLevel(log.WarnLevel)
}

type flagArray []string
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (w walk) WalkFrontMatter(f func(fmwalk), params ...interface{}) error {
if err != nil {
log.WithFields(log.Fields{
"file": fullFilepath,
}).Warn("Unknown type detected in front matter")
}).Warn(fmt.Sprintf("An error occurred while parsing front matter: %s", err.Error()))
}

fmwalkData := fmwalk{fm: frontMatter, fullFilepath: fullFilepath}
Expand Down

0 comments on commit 487f2df

Please sign in to comment.