feat: some experiments
This commit is contained in:
36
template/error.go
Normal file
36
template/error.go
Normal file
@@ -0,0 +1,36 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user