package template import ( "fmt" "strings" ) type Error struct { err error start, end int } func (e Error) Error() string { content := e.err.Error() if strings.Contains(content, " @pos ") { if e.start == e.end { return strings.ReplaceAll(content, " @pos ", " ") } return strings.ReplaceAll(content, " @pos ", fmt.Sprintf(" position=%d:%d ", e.start, e.end)) } return content } func NewErrorf(msg string, args ...any) Error { return Error{ err: fmt.Errorf(msg, args...), } } func (e Error) WithPosition(start, end int) Error { e.start = start e.end = end return e }