summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-10 21:54:49 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-10 21:54:49 -0700
commitc604347f1950e3dfbef2f524da8a63692848da27 (patch)
tree0ec8a2d602eb234a37c0d39906e4c645e6b8bac8
parentcf8f8527e968c448995bdcaaad9c4351d3a2ab3f (diff)
parent49415d88cdd0eed102627369bfdf9de78d0e8bed (diff)
Merge branch 'lukeshu/panic'
-rw-r--r--decode.go18
-rw-r--r--encode_string.go3
-rw-r--r--internal/jsonparse/parse.go12
-rw-r--r--reencode.go2
4 files changed, 18 insertions, 17 deletions
diff --git a/decode.go b/decode.go
index 976cc6a..1ff8938 100644
--- a/decode.go
+++ b/decode.go
@@ -209,7 +209,7 @@ func (dec *Decoder) DecodeThenEOF(ptr any) (err error) {
}
c, s, t, _ := dec.io.ReadRuneType()
if t != jsonparse.RuneTypeEOF {
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: .ReadRuneType returned non-EOF after decode without .Reset being called: %v", t))
}
if s > 0 {
return &DecodeError{
@@ -283,7 +283,7 @@ func (dec *Decoder) unreadRune() {
if err := dec.io.UnreadRune(); err != nil {
// .UnreadRune() must succeed if the previous call was
// .ReadRune(), which it always is for this code.
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: UnreadRune: %w", err))
}
}
@@ -302,7 +302,7 @@ func (dec *Decoder) expectRune(ec rune, et jsonparse.RuneType) *DecodeError {
return err
}
if ac != ec || at != et {
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: expected %q/%v but got %q/%v", ec, et, ac, at))
}
return nil
}
@@ -964,7 +964,7 @@ func (dec *Decoder) decodeAny() (any, *DecodeError) {
case jsonparse.RuneTypeNullN:
return nil, dec.decodeNull()
default:
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: unexpected runeType at beginning of value: %v", t))
}
}
@@ -1061,10 +1061,10 @@ func (dec *Decoder) decodeObject(gTyp reflect.Type, decodeKey, decodeVal func()
case jsonparse.RuneTypeObjectEnd:
return nil
default:
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: unexpected runeType after k/v pair in object: %v", t))
}
default:
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: unexpected runeType after opening '{' of object: %v", t))
}
}
@@ -1136,7 +1136,7 @@ func (dec *Decoder) decodeArray(gTyp reflect.Type, decodeMember func() *DecodeEr
case jsonparse.RuneTypeArrayEnd:
return nil
default:
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: unexpected runeType after array item: %v", t))
}
}
}
@@ -1175,7 +1175,7 @@ func (dec *Decoder) decodeString(gTyp reflect.Type, out fastio.RuneWriter) *Deco
case 't':
_, _ = out.WriteRune('\t')
default:
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: unexpected rune after backslash: %q", c))
}
case jsonparse.RuneTypeStringEscUA:
uhex[0], _ = jsonparse.HexToInt(c)
@@ -1253,7 +1253,7 @@ func (dec *Decoder) decodeString(gTyp reflect.Type, out fastio.RuneWriter) *Deco
case jsonparse.RuneTypeStringEnd:
return nil
default:
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: unexpected runeType in string: %v", t))
}
}
}
diff --git a/encode_string.go b/encode_string.go
index 328b07a..208aef4 100644
--- a/encode_string.go
+++ b/encode_string.go
@@ -5,6 +5,7 @@
package lowmemjson
import (
+ "fmt"
"io"
"unicode/utf8"
@@ -40,7 +41,7 @@ func writeStringShortEscape(w io.Writer, c rune) (int, error) {
case '\t':
b = 't'
default:
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: writeStringShortEscape called with invalid rune: %q", c))
}
buf := [2]byte{'\\', b}
return w.Write(buf[:])
diff --git a/internal/jsonparse/parse.go b/internal/jsonparse/parse.go
index 7d97be0..73584d9 100644
--- a/internal/jsonparse/parse.go
+++ b/internal/jsonparse/parse.go
@@ -401,7 +401,7 @@ func (par *Parser) PushReadBarrier() {
// Sanity checking.
par.init()
if len(par.stack) == 0 {
- panic(errors.New("illegal PushReadBarrier call: empty stack"))
+ panic(errors.New("should not happen: illegal PushReadBarrier call: empty stack"))
}
curState := par.stack[len(par.stack)-1]
switch curState {
@@ -415,7 +415,7 @@ func (par *Parser) PushReadBarrier() {
RuneTypeNullN:
// OK
default:
- panic(fmt.Errorf("illegal PushReadBarrier call: %q", curState))
+ panic(fmt.Errorf("should not happen: illegal PushReadBarrier call: %q", curState))
}
// Actually push.
par.barriers = append(par.barriers, barrier{
@@ -441,7 +441,7 @@ func (par *Parser) PushReadBarrier() {
func (par *Parser) PushWriteBarrier() {
par.init()
if len(par.stack) == 0 {
- panic(errors.New("illegal PushWriteBarrier call: empty stack"))
+ panic(errors.New("should not happen: illegal PushWriteBarrier call: empty stack"))
}
switch par.stack[len(par.stack)-1] {
case runeTypeAny:
@@ -459,14 +459,14 @@ func (par *Parser) PushWriteBarrier() {
})
par.stack = []RuneType{runeTypeAny}
default:
- panic(fmt.Errorf("illegal PushWriteBarrier call: %q", par.stack[len(par.stack)-1]))
+ panic(fmt.Errorf("should not happen: illegal PushWriteBarrier call: %q", par.stack[len(par.stack)-1]))
}
}
// PopBarrier reverses a call to PushReadBarrier or PushWriteBarrier.
func (par *Parser) PopBarrier() {
if len(par.barriers) == 0 {
- panic(errors.New("illegal PopBarrier call: empty barrier stack"))
+ panic(errors.New("should not happen: illegal PopBarrier call: empty barrier stack"))
}
barrier := par.barriers[len(par.barriers)-1]
par.barriers = par.barriers[:len(par.barriers)-1]
@@ -828,7 +828,7 @@ func (par *Parser) HandleRune(c rune) (RuneType, error) {
case RuneTypeNullL1:
return par.expectRune(c, 'l', RuneTypeNullL2, "null", true)
default:
- panic(fmt.Errorf(`invalid stack: "%s"`, par.stackString()))
+ panic(fmt.Errorf(`should not happen: invalid stack: "%s"`, par.stackString()))
}
}
diff --git a/reencode.go b/reencode.go
index 232d91d..e48c58c 100644
--- a/reencode.go
+++ b/reencode.go
@@ -519,7 +519,7 @@ func (enc *ReEncoder) handleRuneMain(c rune, t jsonparse.RuneType) error {
case 't':
err = enc.emit(writeStringChar(enc.out, '\t', BackslashEscapeShort, enc.BackslashEscape))
default:
- panic("should not happen")
+ panic(fmt.Errorf("should not happen: rune %q is not a RuneTypeStringEsc1", c))
}
case jsonparse.RuneTypeStringEscUA:
enc.uhex[0], _ = jsonparse.HexToInt(c)