File: error_context.go

package info (click to toggle)
golang-github-oschwald-maxminddb-golang-v2 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: perl: 557; makefile: 3
file content (49 lines) | stat: -rw-r--r-- 1,590 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package decoder

import (
	"github.com/oschwald/maxminddb-golang/v2/internal/mmdberrors"
)

// errorContext provides zero-allocation error context tracking for Decoder.
// This is only used when an error occurs, ensuring no performance impact
// on the happy path.
type errorContext struct {
	path *mmdberrors.PathBuilder // Only allocated when needed
}

// BuildPath implements mmdberrors.ErrorContextTracker.
// This is only called when an error occurs, so allocation is acceptable.
func (e *errorContext) BuildPath() string {
	if e.path == nil {
		return "" // No path tracking enabled
	}
	return e.path.Build()
}

// wrapError wraps an error with context information when an error occurs.
// Zero allocation on happy path - only allocates when error != nil.
func (d *Decoder) wrapError(err error) error {
	if err == nil {
		return nil
	}
	// Only wrap with context when an error actually occurs
	return mmdberrors.WrapWithContext(err, d.offset, nil)
}

// wrapErrorAtOffset wraps an error with context at a specific offset.
// Used when the error occurs at a different offset than the decoder's current position.
func (*Decoder) wrapErrorAtOffset(err error, offset uint) error {
	if err == nil {
		return nil
	}
	return mmdberrors.WrapWithContext(err, offset, nil)
}

// Example of how to integrate into existing decoder methods:
// Instead of:
//   return mmdberrors.NewInvalidDatabaseError("message")
// Use:
//   return d.wrapError(mmdberrors.NewInvalidDatabaseError("message"))
//
// This adds zero overhead when no error occurs, but provides rich context
// when errors do happen.