tools: display error context when it exists (#749)

For example when failing to decode toml, display the context around the
error and the location of the problem.
This commit is contained in:
Thomas Pelletier
2022-04-07 20:33:09 -04:00
committed by GitHub
parent 9804fc57e0
commit 88a8aecdd4
2 changed files with 29 additions and 1 deletions
+13 -1
View File
@@ -2,11 +2,14 @@ package cli
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/pelletier/go-toml/v2"
)
type ConvertFn func(r io.Reader, w io.Writer) error
@@ -28,7 +31,16 @@ func (p *Program) Execute() {
func (p *Program) main(files []string, input io.Reader, output, error io.Writer) int {
err := p.run(files, input, output)
if err != nil {
fmt.Fprintln(error, err.Error())
var derr *toml.DecodeError
if errors.As(err, &derr) {
fmt.Fprintln(error, derr.String())
row, col := derr.Position()
fmt.Fprintln(error, "error occurred at row", row, "column", col)
} else {
fmt.Fprintln(error, err.Error())
}
return -1
}
return 0