From 49415d88cdd0eed102627369bfdf9de78d0e8bed Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 27 Jan 2023 01:24:02 -0700 Subject: tree-wide: Audit panic error messages These shouldn't happen, but if they do, then let's make it easier to debug. --- decode.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'decode.go') 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)) } } } -- cgit v1.2.3-2-g168b