Better error handling

This commit is contained in:
Mike Farah 2018-06-15 16:11:13 +10:00
parent 2f5a481cc3
commit 94b217984c
2 changed files with 12 additions and 5 deletions

6
vendor/vendor.json vendored
View File

@ -20,6 +20,12 @@
"revision": "970db520ece77730c7e4724c61121037378659d9", "revision": "970db520ece77730c7e4724c61121037378659d9",
"revisionTime": "2016-03-15T20:05:05Z" "revisionTime": "2016-03-15T20:05:05Z"
}, },
{
"checksumSHA1": "ljd3FhYRJ91cLZz3wsH9BQQ2JbA=",
"path": "github.com/pkg/errors",
"revision": "816c9085562cd7ee03e7f8188a1cfd942858cded",
"revisionTime": "2018-03-11T21:45:15Z"
},
{ {
"checksumSHA1": "xPKgXygsORkmXnLdtFaFmipYKaA=", "checksumSHA1": "xPKgXygsORkmXnLdtFaFmipYKaA=",
"path": "github.com/spf13/cobra", "path": "github.com/spf13/cobra",

11
yq.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -11,6 +10,8 @@ import (
"strconv" "strconv"
"strings" "strings"
errors "github.com/pkg/errors"
logging "github.com/op/go-logging" logging "github.com/op/go-logging"
"github.com/spf13/cobra" "github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
@ -288,14 +289,14 @@ func mapYamlDecoder(updateData updateDataFn, encoder *yaml.Encoder) yamlDecoderF
} }
return nil return nil
} else if errorReading != nil { } else if errorReading != nil {
return fmt.Errorf("Error reading document at index %v, %v", currentIndex, errorReading) return errors.Wrapf(errorReading, "Error reading document at index %v, %v", currentIndex, errorReading)
} }
dataBucket = updateData(dataBucket, currentIndex) dataBucket = updateData(dataBucket, currentIndex)
errorWriting = encoder.Encode(dataBucket) errorWriting = encoder.Encode(dataBucket)
if errorWriting != nil { if errorWriting != nil {
return fmt.Errorf("Error writing document at index %v, %v", currentIndex, errorWriting) return errors.Wrapf(errorWriting, "Error writing document at index %v, %v", currentIndex, errorWriting)
} }
currentIndex = currentIndex + 1 currentIndex = currentIndex + 1
} }
@ -481,7 +482,7 @@ func marshalContext(context interface{}) (string, error) {
out, err := yaml.Marshal(context) out, err := yaml.Marshal(context)
if err != nil { if err != nil {
return "", fmt.Errorf("error printing yaml: %v", err) return "", errors.Wrap(err, "error printing yaml")
} }
outStr := string(out) outStr := string(out)
@ -543,7 +544,7 @@ func readData(filename string, indexToRead int, parsedData interface{}) error {
for currentIndex := 0; currentIndex < indexToRead; currentIndex++ { for currentIndex := 0; currentIndex < indexToRead; currentIndex++ {
errorSkipping := decoder.Decode(parsedData) errorSkipping := decoder.Decode(parsedData)
if errorSkipping != nil { if errorSkipping != nil {
return fmt.Errorf("Error processing document at index %v, %v", currentIndex, errorSkipping) return errors.Wrapf(errorSkipping, "Error processing document at index %v, %v", currentIndex, errorSkipping)
} }
} }
return decoder.Decode(parsedData) return decoder.Decode(parsedData)