From 5beddd91a35fadbcc7a10888cd6a920fa9151010 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 14 Sep 2016 23:08:19 -0400 Subject: [PATCH] error handling --- ent/dict.go | 34 +++++++++++++++++++++++----------- ent/selection.go | 4 +--- ent/slotted.go | 5 +++-- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/ent/dict.go b/ent/dict.go index 594a8a3..bc742b7 100644 --- a/ent/dict.go +++ b/ent/dict.go @@ -75,7 +75,10 @@ func (d *Dict) createEntity(id int) error { e := class.New(serial, false) d.entities[id] = e Debug.Printf("create entity id: %d serial: %d classId: %d className: %v class: %v\n", id, serial, classId, className, class) - return fillSlots(e, class.Name.String(), d.sr, d.br) + if err := fillSlots(e, class.Name.String(), d.sr, d.br); err != nil { + return fmt.Errorf("failed to create entity %d (%s): %v", id, className, err) + } + return nil } func (d *Dict) getEntity(id int) *Entity { @@ -92,13 +95,23 @@ func (d *Dict) updateEntity(id int) error { if e == nil { return fmt.Errorf("update entity %d refused: no such entity", id) } - return fillSlots(e, e.Class.String(), d.sr, d.br) + if err := fillSlots(e, e.Class.String(), d.sr, d.br); err != nil { + return fmt.Errorf("failed to update entity %d (%s): %v", id, e.Class.Name.String(), err) + } + return nil } func (d *Dict) deleteEntity(id int) error { Debug.Printf("delete entity id: %d\n", id) if id < 0 || id >= e_limit { - return fmt.Errorf("delete entity %d refused: no such entity", id) + // we're probably reading corrupt shit if we get here + return fmt.Errorf("delete entity %d refused: id is out of bounds", id) + } + if d.entities[id] == nil { + // this one's probably ok to let fly, but it's probably a sign that + // something is wrong elsewhere. + Debug.Printf("delete entity %d refused: no such entity", id) + return nil } d.entities[id] = nil return nil @@ -122,7 +135,7 @@ func (d *Dict) Handle(m proto.Message) { case *dota.CSVCMsg_PacketEntities: if err := d.mergeEntities(v); err != nil { - Debug.Printf("merge entities error: %v", err) + Info.Fatalf("merge entities error: %v", err) } } } @@ -154,7 +167,7 @@ func (d *Dict) mergeEntities(m *dota.CSVCMsg_PacketEntities) error { } if err := fn(id); err != nil { - return fmt.Errorf("entity merge error: %v", err) + return fmt.Errorf("error on entity %d: %v", id, err) } } return nil @@ -165,15 +178,14 @@ func (d *Dict) updateBaselines(t *stbl.Table) { d.syncBaselines() } -func (d *Dict) syncBaselines() { +func (d *Dict) syncBaselines() error { if !d.hasClassinfo() { Debug.Printf("syncBaselines skip: no classInfo yet") - return + return nil } Debug.Printf("syncBaselines start") if d.base == nil { - Debug.Printf("syncBaselines failed: reference to baseline string table is nil") - return + return fmt.Errorf("syncBaselines failed: reference to baseline string table is nil") } for _, e := range d.base.Entries() { @@ -201,8 +213,8 @@ func (d *Dict) syncBaselines() { d.br.SetSource(e.Value) Debug.Printf("syncBaselines has new baseline for class %v", c) if err := fillSlots(c.baseline, c.Name.String(), d.sr, d.br); err != nil { - Debug.Printf("syncBaselines failed to fill a baseline: %v", err) - continue + return fmt.Errorf("syncBaselines failed to fill a baseline: %v", err) } } + return nil } diff --git a/ent/selection.go b/ent/selection.go index 2243a5c..b5aa476 100644 --- a/ent/selection.go +++ b/ent/selection.go @@ -20,7 +20,6 @@ func (s selection) String() string { return fmt.Sprint(s.path()) } func (s selection) path() []int { return s.vals[:s.count] } func (s selection) fill(offset int, displayPath string, dest slotted, br bit.Reader) error { - Debug.Printf("fill selection: %s offset: %d displayPath: %s dest: %v", s, offset, displayPath, dest) slot := s.vals[offset] if s.count-offset <= 0 { panic("selection makes no sense") @@ -48,8 +47,7 @@ func (s selection) fill(offset int, displayPath string, dest slotted, br bit.Rea v := dest.slotValue(slot) vs, ok := v.(slotted) if !ok { - Info.Printf("child selection %s at offset %d refers to a slot (%d: %s) that contains a non-slotted type: %s with value: %v", s, offset, slot, fmt.Sprintf("%s.%s", displayPath, dest.slotName(slot)), dest.slotType(slot), v) - return fmt.Errorf("child selection refers to a slot that doesn't contain a slotted value") + return fmt.Errorf("selection %s at offset %d (%d) refers to a slot (%s) that contains a non-slotted type (%s) with value %v", s, offset, slot, fmt.Sprintf("%s.%s", displayPath, dest.slotName(slot)), dest.slotType(slot), v) } return s.fill(offset+1, fmt.Sprintf("%s.%s", displayPath, dest.slotName(slot)), vs, br) } diff --git a/ent/slotted.go b/ent/slotted.go index e913277..ecf53db 100644 --- a/ent/slotted.go +++ b/ent/slotted.go @@ -1,6 +1,7 @@ package ent import ( + "fmt" "github.com/jordanorelli/hyperstone/bit" ) @@ -15,12 +16,12 @@ type slotted interface { func fillSlots(dest slotted, displayPath string, sr *selectionReader, br bit.Reader) error { selections, err := sr.readSelections(br, htree) if err != nil { - return err + return fmt.Errorf("error filling slots: %v", err) } for _, s := range selections { if err := s.fill(0, displayPath, dest, br); err != nil { - return err + return fmt.Errorf("error filling slots: %v", err) } } return nil