2024-09-06 23:29:29 -04:00
|
|
|
package x
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-09-29 14:59:01 -04:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2024-09-06 23:29:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func Assert(cond bool, msg string) {
|
|
|
|
if cond {
|
|
|
|
panic(errors.New(msg))
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 18:19:00 -04:00
|
|
|
|
|
|
|
func Check(cond bool, err error) *invariants {
|
|
|
|
return new(invariants).Check(cond, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type invariants struct {
|
|
|
|
errs []error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *invariants) Check(cond bool, err error) *invariants {
|
|
|
|
if !cond {
|
|
|
|
i.errs = append(i.errs, err)
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *invariants) Join() error {
|
|
|
|
return errors.Join(i.errs...)
|
|
|
|
}
|
|
|
|
func (i *invariants) First() error {
|
|
|
|
if len(i.errs) > 0 {
|
|
|
|
return i.errs[0]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (i *invariants) All() []error {
|
|
|
|
return i.errs
|
|
|
|
}
|
2024-09-29 14:59:01 -04:00
|
|
|
|
|
|
|
type xError struct {
|
|
|
|
LineNo int
|
|
|
|
File string
|
|
|
|
E error
|
|
|
|
Debug bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *xError) Error() string {
|
|
|
|
if e.Debug {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%s\n\t%s:%d", e.E, e.File, e.LineNo,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return e.E.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *xError) Unwrap() error {
|
|
|
|
return e.E
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewError(unwrapped error, debug bool) error {
|
|
|
|
_, file, line, ok := runtime.Caller(1)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
file = "unknown"
|
|
|
|
line = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return &xError{
|
|
|
|
LineNo: line,
|
|
|
|
File: file,
|
|
|
|
E: unwrapped,
|
|
|
|
Debug: debug,
|
|
|
|
}
|
|
|
|
}
|