mirror of
https://github.com/ii64/sonic.git
synced 2026-06-22 17:36:48 +08:00
feat: SyntaxError.Error() always calls Description() (#152)
Co-authored-by: duanyi.aster <duanyi.aster@bytedance.com>
This commit is contained in:
parent
d5104197c4
commit
7d3b22100f
3 changed files with 17 additions and 15 deletions
15
README.md
15
README.md
|
|
@ -123,26 +123,23 @@ v, err := encoder.Encode(m, encoder.SortMapKeys)
|
||||||
|
|
||||||
### Print Syntax Error
|
### Print Syntax Error
|
||||||
```go
|
```go
|
||||||
|
import "github.com/bytedance/sonic"
|
||||||
import "github.com/bytedance/sonic/decoder"
|
import "github.com/bytedance/sonic/decoder"
|
||||||
|
|
||||||
var data interface{}
|
var data interface{}
|
||||||
dc := decoder.NewDecoder("[[[}]]")
|
err := sonic.Unmarshal("[[[}]]", &data)
|
||||||
if err := dc.Decode(&data); err != nil {
|
if err != nil {
|
||||||
|
/*one line by default*/
|
||||||
|
println(e.Error())) // "Syntax error at index 3: invalid char\n\n\t[[[}]]\n\t...^..\n"
|
||||||
|
/*pretty print*/
|
||||||
if e, ok := err.(decoder.SyntaxError); ok {
|
if e, ok := err.(decoder.SyntaxError); ok {
|
||||||
|
|
||||||
/*Syntax error at index 3: invalid char
|
/*Syntax error at index 3: invalid char
|
||||||
|
|
||||||
[[[}]]
|
[[[}]]
|
||||||
...^..
|
...^..
|
||||||
*/
|
*/
|
||||||
print(e.Description())
|
print(e.Description())
|
||||||
|
|
||||||
/*"Syntax error at index 3: invalid char\n\n\t[[[}]]\n\t...^..\n"*/
|
|
||||||
println(fmt.Sprintf("%q", e.Description()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*Decode: Syntax error at index 3: invalid char*/
|
|
||||||
t.Fatalf("Decode: %v", err)
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ type SyntaxError struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self SyntaxError) Error() string {
|
func (self SyntaxError) Error() string {
|
||||||
return fmt.Sprintf("Syntax error at index %d: %s", self.Pos, self.Code.Message())
|
return fmt.Sprintf("%q", self.Description())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self SyntaxError) Description() string {
|
func (self SyntaxError) Description() string {
|
||||||
|
|
@ -45,7 +45,7 @@ func (self SyntaxError) Description() string {
|
||||||
|
|
||||||
/* check for empty source */
|
/* check for empty source */
|
||||||
if self.Src == "" {
|
if self.Src == "" {
|
||||||
return self.Error() + " (no sources available)"
|
return fmt.Sprintf("no sources available: %#v", self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* prevent slicing before the beginning */
|
/* prevent slicing before the beginning */
|
||||||
|
|
@ -71,8 +71,9 @@ func (self SyntaxError) Description() string {
|
||||||
|
|
||||||
/* compose the error description */
|
/* compose the error description */
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"%s\n\n\t%s\n\t%s^%s\n",
|
"Syntax error at index %d: %s\n\n\t%s\n\t%s^%s\n",
|
||||||
self.Error(),
|
self.Pos,
|
||||||
|
self.Code.Message(),
|
||||||
self.Src[p:q],
|
self.Src[p:q],
|
||||||
strings.Repeat(".", x),
|
strings.Repeat(".", x),
|
||||||
strings.Repeat(".", y),
|
strings.Repeat(".", y),
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import (
|
||||||
`testing`
|
`testing`
|
||||||
|
|
||||||
`github.com/bytedance/sonic/internal/native/types`
|
`github.com/bytedance/sonic/internal/native/types`
|
||||||
|
`github.com/stretchr/testify/assert`
|
||||||
)
|
)
|
||||||
|
|
||||||
func make_err(src string, pos int) SyntaxError {
|
func make_err(src string, pos int) SyntaxError {
|
||||||
|
|
@ -48,7 +49,10 @@ func TestErrors_AfterRightEdge(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrors_ShortDescription(t *testing.T) {
|
func TestErrors_ShortDescription(t *testing.T) {
|
||||||
println(make_err("hello, world", 5).Description())
|
e := make_err("hello, world", 5)
|
||||||
|
println(e.Description())
|
||||||
|
assert.Equal(t, "Syntax error at index 5: invalid char\n\n\thello, world\n\t.....^......\n", e.Description())
|
||||||
|
assert.Equal(t, `"Syntax error at index 5: invalid char\n\n\thello, world\n\t.....^......\n"`, e.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrors_EmptyDescription(t *testing.T) {
|
func TestErrors_EmptyDescription(t *testing.T) {
|
||||||
|
|
@ -61,7 +65,7 @@ func TestDecoderErrorStackOverflower(t *testing.T) {
|
||||||
for i:=0; i<N; i++ {
|
for i:=0; i<N; i++ {
|
||||||
var obj map[string]string
|
var obj map[string]string
|
||||||
err := NewDecoder(src).Decode(&obj)
|
err := NewDecoder(src).Decode(&obj)
|
||||||
if err == nil || err.Error() != `Syntax error at index 5: invalid char` {
|
if err == nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue