Repository navigation
fix(json): preserve sentinel hierarchy (#59) #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,9 @@ type SerializedError struct { | |
| // Sentinels lists all classification sentinel texts found in this error | ||
| Sentinels []string `json:"sentinels,omitempty"` | ||
|
|
||
| // SentinelAncestors lists the deduplicated parent sentinel texts for this error level. | ||
| SentinelAncestors []string `json:"sentinel_ancestors,omitempty"` | ||
|
|
||
| // Attributes contains structured key-value pairs attached to this error | ||
| Attributes []SerializedAttr `json:"attributes,omitempty"` | ||
|
|
||
|
|
@@ -271,6 +274,7 @@ func toSerializedError(err error, cfg *config, visited *visitedSet, depth int) * | |
| // classifications and the node itself (if it's a pure sentinel) count. | ||
| if cfg.includeSentinels { | ||
| result.Sentinels = sentinelsForLevel(levelCls, node) | ||
| result.SentinelAncestors = sentinelAncestorsForLevel(levelCls, node) | ||
| } | ||
|
|
||
| // Attributes: chain-wide (errx.ExtractAttrs walks the chain). | ||
|
|
@@ -362,6 +366,61 @@ func sentinelsForLevel(classifications []errx.Classified, node error) []string { | |
| return sentinels | ||
| } | ||
|
|
||
| // sentinelAncestorsForLevel returns the pure-sentinel parents of the | ||
| // classifications attached to this error level or represented by its node. | ||
| // Parents are traversed depth-first in declaration order and deduplicated by text. | ||
| func sentinelAncestorsForLevel(classifications []errx.Classified, node error) []string { | ||
| var ancestors []string | ||
| var seenTexts map[string]bool | ||
| var visited map[errx.Classified]bool | ||
|
|
||
| for _, cls := range classifications { | ||
| if isPureSentinel(cls) { | ||
| addSentinelParents(cls, &ancestors, &seenTexts, &visited) | ||
| } | ||
| } | ||
| if cls, ok := node.(errx.Classified); ok && isPureSentinel(cls) { | ||
| addSentinelParents(cls, &ancestors, &seenTexts, &visited) | ||
| } | ||
|
|
||
| return ancestors | ||
| } | ||
|
|
||
| // addSentinelParents appends pure-sentinel parents and their ancestors, | ||
| // guarding against repeated sentinels and malformed cycles. | ||
| func addSentinelParents( | ||
| cls errx.Classified, | ||
| ancestors *[]string, | ||
| seenTexts *map[string]bool, | ||
| visited *map[errx.Classified]bool, | ||
| ) { | ||
| parents := errx.SentinelParents(cls) | ||
| if len(parents) == 0 { | ||
| return | ||
| } | ||
|
|
||
| // SentinelParents only returns a non-empty slice for the internal, | ||
| // pointer-backed sentinel type, which is safe to use as a map key. | ||
| if *visited == nil { | ||
| *visited = make(map[errx.Classified]bool) | ||
| } | ||
| if (*visited)[cls] { | ||
| return | ||
| } | ||
| (*visited)[cls] = true | ||
|
|
||
| for _, parent := range parents { | ||
| if !isPureSentinel(parent) { | ||
| continue | ||
| } | ||
|
Comment on lines
+412
to
+415
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Avoid rewalking every parent's subtree during ancestor collection
A concrete valid DAG is built by repeatedly assigning Please avoid recursive displayability detection at every parent. Once the root has passed the chain-wide displayable check, propagate that result and use shallow attribute/trace-kind filtering during descent, or compute the relevant properties in one traversal. Add a scaling benchmark for this DAG shape to guard against repeated subtree scans. |
||
| text := parent.Error() | ||
| if !rememberSentinel(text, seenTexts) { | ||
| *ancestors = append(*ancestors, text) | ||
| } | ||
| addSentinelParents(parent, ancestors, seenTexts, visited) | ||
| } | ||
| } | ||
|
|
||
| // serializeAttributes extracts and serializes attributes from an error. | ||
| // Attribute values whose JSON encoding fails (e.g. a func() value) are | ||
| // replaced with a fmt.Sprintf("%+v", v) fallback string so a single bad | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,6 +209,48 @@ func TestMarshal_HierarchicalSentinels(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestMarshal_HierarchicalSentinelAncestors(t *testing.T) { | ||
| shared := errx.NewSentinel("shared") | ||
| left := errx.NewSentinel("left", shared) | ||
| right := errx.NewSentinel("right", shared) | ||
| child := errx.NewSentinel("child", left, right) | ||
|
Comment on lines
+213
to
+216
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking: extend coverage for the new ancestor contract This diamond tests deduplication of a shared parent object, but not deduplication by text across distinct objects. Please add two different Please also cover |
||
| testErr := errx.Classify(errors.New("hierarchical error"), child) | ||
|
|
||
| data, err := errxjson.Marshal(testErr) | ||
| if err != nil { | ||
| t.Fatalf("Marshal error: %v", err) | ||
| } | ||
| var result errxjson.SerializedError | ||
| if err := json.Unmarshal(data, &result); err != nil { | ||
| t.Fatalf("Unmarshal error: %v", err) | ||
| } | ||
| if got, want := fmt.Sprint(result.Sentinels), "[child]"; got != want { | ||
| t.Errorf("Sentinels = %s, want %s", got, want) | ||
| } | ||
| if got, want := fmt.Sprint(result.SentinelAncestors), "[left shared right]"; got != want { | ||
| t.Errorf("SentinelAncestors = %s, want %s", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestMarshal_WithSentinelsFalseOmitsAncestors(t *testing.T) { | ||
| root := errx.NewSentinel("root") | ||
| child := errx.NewSentinel("child", root) | ||
| data, err := errxjson.Marshal( | ||
| errx.Classify(errors.New("error"), child), | ||
| errxjson.WithSentinels(false), | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("Marshal error: %v", err) | ||
| } | ||
| var result errxjson.SerializedError | ||
| if err := json.Unmarshal(data, &result); err != nil { | ||
| t.Fatalf("Unmarshal error: %v", err) | ||
| } | ||
| if len(result.Sentinels) != 0 || len(result.SentinelAncestors) != 0 { | ||
| t.Errorf("sentinel fields should be omitted, got sentinels=%v ancestors=%v", result.Sentinels, result.SentinelAncestors) | ||
| } | ||
| } | ||
|
|
||
| func TestMarshal_ComplexError(t *testing.T) { | ||
| // Create a complex error with all features | ||
| baseErr := errors.New("connection failed") | ||
|
|
@@ -249,9 +291,15 @@ func TestMarshal_ComplexError(t *testing.T) { | |
| if len(result.Sentinels) != 0 { | ||
| t.Errorf("top-level Sentinels = %v, want empty (sentinel attached at inner level)", result.Sentinels) | ||
| } | ||
| if len(result.SentinelAncestors) != 0 { | ||
| t.Errorf("top-level SentinelAncestors = %v, want empty (sentinel attached at inner level)", result.SentinelAncestors) | ||
| } | ||
| if result.Cause == nil { | ||
| t.Fatal("Cause is nil; sentinel should be reported at this level") | ||
| } | ||
| if got, want := fmt.Sprint(result.Cause.SentinelAncestors), "[database retryable]"; got != want { | ||
| t.Errorf("Cause.SentinelAncestors = %s, want %s", got, want) | ||
| } | ||
| gotTimeout := false | ||
| for _, s := range result.Cause.Sentinels { | ||
| if s == "timeout" { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: document the remaining loss of hierarchy information
The additive format is appropriate for the v1 option in #59, but this limitations entry should explicitly say that it preserves a flattened set of ancestor texts, not parent-child relationships. With multiple direct sentinels, a consumer cannot determine which ancestor belongs to which direct sentinel; intermediate edges and distinct identities sharing the same text are also not represented.
Please retain that limitation here and distinguish it from the future work in #37: a registry can resolve known texts to local sentinel values for matching, but this payload alone cannot reconstruct the original graph. No schema redesign is needed for this PR.