Dolt error message when failing parsing JSON could be more helpful
- Dominant language
- Go
- Stars
- 24.4k
- Forks
- 873
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 120
Description
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`
```go
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:
```go
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.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in go-mysql-server/go/sql/types/json.go, at the json.Unmarshal call site described in the issue, and inspect how JSON errors are currently returned. Use the existing json.SyntaxError details and proposed context behavior as guidance. Done means parse failures include the offset and nearby document context without changing the existing handling of other errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100