dolt
dolt copied to clipboard
Dolt error message when failing parsing JSON could be more helpful
When Dolt fails to parse a JSON value, its error message is somewhat barebones:
> CREATE TABLE json_t (v JSON);
> INSERT INTO json_t (v) VALUES ('[NaN]');
Invalid JSON text: invalid character 'N' looking for beginning of value
In this case, the document is small so it is easy to find the cause by inspection. For large documents and when dealing with log lines which might include their own escaping for the JSON document, it becomes quite difficult and noisey.
It would be useful if we included the column number in the error message and even a bit of context around the document position.
Both can be extracted at the call site by using errors.As() on a *json.SyntaxError
err = json.Unmarshal(v, &doc)
if err != nil {
var se *json.SyntaxError
if errors.As(err, &se) {
return nil, sql.OutOfRange, fmt.Errorf("%w: offset: %d, document context: %s", sql.ErrInvalidJson.New(err.Error()), se.Offset, getSyntaxErrorContext(v, se.Offset))
}
return nil, sql.OutOfRange, sql.ErrInvalidJson.New(err.Error())
}
where getSyntaxErrorContext is something like:
func getSyntaxErrorContext(bs []byte, offset int64) string {
const defaultContextBytes = 32
start := max(0, offset-defaultContextBytes)
end := min(offset+defaultContextBytes, len(bs))
return string(bs[start:end])
}
These changes, or similar, belong in go-mysql-server go/sql/types/json.go if we want to implement this.