better nbt error message & dimensioncodec decoder

This commit is contained in:
Tnze
2022-06-19 23:05:10 +08:00
parent 30479d6ea5
commit d94993f34f
6 changed files with 67 additions and 142 deletions

View File

@ -39,10 +39,6 @@ func (d *Decoder) Decode(v interface{}) (string, error) {
return tagName, fmt.Errorf("nbt: %w", err)
}
if c := d.checkCompressed(tagType); c != "" {
return tagName, fmt.Errorf("nbt: unknown Tag, maybe compressed by %s, uncompress it first", c)
}
// We decode val not val.Elem because the Unmarshaler interface
// test must be applied at the top level of the value.
err = d.unmarshal(val, tagType)
@ -78,7 +74,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
switch tagType {
default:
return fmt.Errorf("unknown Tag 0x%02x", tagType)
return fmt.Errorf("unknown Tag %#02x", tagType)
case TagEnd:
return ErrEND
@ -505,7 +501,7 @@ func (d *Decoder) rawRead(tagType byte) error {
var buf [8]byte
switch tagType {
default:
return fmt.Errorf("unknown to read 0x%02x", tagType)
return fmt.Errorf("unknown to read %#02x", tagType)
case TagByte:
_, err := d.readByte()
return err
@ -590,7 +586,12 @@ func (d *Decoder) readTag() (tagType byte, tagName string, err error) {
return
}
if tagType != TagEnd { //Read Tag
switch tagType {
case 0x1f, 0x78:
c := d.checkCompressed(tagType)
err = fmt.Errorf("nbt: unknown Tag %#02x, which seems like %s header and you should uncompress it first", tagType, c)
case TagEnd:
default: //Read Tag
tagName, err = d.readString()
}
return

View File

@ -200,6 +200,9 @@ func (e *Encoder) writeValue(val reflect.Value, tagType byte) error {
if tagProps.OmitEmpty && isEmptyValue(v) {
continue
}
if tagProps.Type == TagNone {
return fmt.Errorf("encode %q error: unsupport type %v", tagProps.Name, v.Type())
}
if err := e.marshal(val.Field(i), tagProps.Type, tagProps.Name); err != nil {
return err
@ -216,7 +219,7 @@ func (e *Encoder) writeValue(val reflect.Value, tagType byte) error {
}
tagType, tagValue := getTagType(r.Value())
if tagType == TagNone {
return errors.New("unsupported value " + tagValue.String())
return fmt.Errorf("encoding %q error: unsupport type %v", tagName, tagValue.Type())
}
if err := e.marshal(tagValue, tagType, tagName); err != nil {