Closed geeknik closed 4 years ago
Standard lib which is used here for unmarshalling doesn't make this problem easy to solve. It's possible to get some details from the error, but not a line number. Plus not every JSON has line breaks. I would propose something like below as a solution here. It'll give an error message plus the 20 bytes chunk of JSON which is causing problem.
func getDetailedError(data []byte, err error) error {
var limit int64 = 20
syntaxError, ok := err.(*json.SyntaxError)
if ok {
end := syntaxError.Offset - 1 + limit
dataLength := int64(len(data))
if end > dataLength {
end = dataLength
}
badPart := string(data[syntaxError.Offset-1 : end])
return fmt.Errorf("%s:\n%s", err.Error(), badPart)
}
typeError, ok := err.(*json.UnmarshalTypeError)
if ok {
start := typeError.Offset - limit
if start < 0 {
start = 0
}
badPart := string(data[start:typeError.Offset])
return fmt.Errorf("%s:\n%s", err.Error(), badPart)
}
return err
}
Thank you, I'll give it a shot.
@pczajkowski Thank you once again for the pull request :)
Hello and thank you again for this great tool. Would it be possible to add some error handling for the provider JSON file so that if there is an error like
invalid character '[' in string escape code
, it will print the line # of the JSON where the error occurred? This would be a great help for folks with extra-large provider files. Thank you.