refactor(errors): rename ContextAwareErr to WrappedErr

This commit is contained in:
Mark Bailey 2025-09-06 15:20:47 -04:00
parent 50f3748ed2
commit 4c6e0c9ffb
2 changed files with 20 additions and 16 deletions

View File

@ -9,15 +9,15 @@ import (
var ErrCacheUninitialized = errors.New("the route cache is uninitialized")
type ErrRouteNotFound struct {
n string
route string
}
func NewErrRouteNotFound(route string) error {
return &ErrRouteNotFound{route: route}
}
func (e ErrRouteNotFound) Error() string {
return "route not found: " + string(e.n)
}
func NewErrRouteNotFound(n string) error {
return &ErrRouteNotFound{n: n}
return "route not found: " + string(e.route)
}
type ErrBadgerInit struct {
@ -25,6 +25,10 @@ type ErrBadgerInit struct {
subDir string
}
func NewErrBadgerInit(baseDir, subDir string) error {
return &ErrBadgerInit{baseDir: baseDir, subDir: subDir}
}
func (e ErrBadgerInit) Error() string {
if e.subDir == "" {
return fmt.Sprintf("failed to initialize badger store at base directory: %s", e.baseDir)
@ -33,19 +37,19 @@ func (e ErrBadgerInit) Error() string {
return fmt.Sprintf("failed to initialize badger store at directory: %s", filepath.Join(e.baseDir, e.subDir))
}
func NewErrBadgerInit(baseDir, subDir string) error {
return &ErrBadgerInit{baseDir: baseDir, subDir: subDir}
type WrappedErr struct {
e error
with error
}
type ContextAwareErr struct {
base error
current error
func Wrap(e, with error) WrappedErr {
return WrappedErr{e: e, with: with}
}
func NewContextAwareErr(base, e error) ContextAwareErr {
return ContextAwareErr{base: base, current: e}
func (e WrappedErr) Error() string {
return fmt.Sprintf(`%s: %s`, e.e, e.with)
}
func (e ContextAwareErr) Error() string {
return fmt.Sprintf(`%s: %s`, e.base, e.current)
func (e WrappedErr) Unwrap() error {
return e.e
}

View File

@ -42,7 +42,7 @@ func WithStore(store Storer) RouteCacheOpt {
func NewRouteCache(opts ...RouteCacheOpt) (*routeCache, error) {
s, err := badger.NewBadgerStore(badger.WithSubDir("route_cache"))
if err != nil {
return nil, jerr.NewContextAwareErr(jerr.ErrCacheUninitialized, err)
return nil, jerr.Wrap(jerr.ErrCacheUninitialized, err)
}
c = &routeCache{