github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/utils/unmarshal.go (about) 1 package utils 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 ) 8 9 // displaySyntaxError will display more information 10 // such as line and error type given an error and 11 // the data that was unmarshalled. 12 // Thanks to https://github.com/markpeek/packer/commit/5bf33a0e91b2318a40c42e9bf855dcc8dd4cdec5 13 func DisplaySyntaxError(data []byte, syntaxError error) (err error) { 14 syntax, ok := syntaxError.(*json.SyntaxError) 15 if !ok { 16 err = syntaxError 17 return 18 } 19 newline := []byte{'\x0a'} 20 space := []byte{' '} 21 22 start, end := bytes.LastIndex(data[:syntax.Offset], newline)+1, len(data) 23 if idx := bytes.Index(data[start:], newline); idx >= 0 { 24 end = start + idx 25 } 26 27 line, pos := bytes.Count(data[:start], newline)+1, int(syntax.Offset)-start-1 28 29 err = fmt.Errorf("\nError in line %d: %s \n%s\n%s^", line, syntaxError, data[start:end], bytes.Repeat(space, pos)) 30 return 31 }