add additional logging to Module.RequireLoaded

This commit is contained in:
Elijah Duffy
2025-06-03 15:10:35 -07:00
parent 08ef7a5c51
commit b290bc0f7e
2 changed files with 16 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log/slog"
"strings"
)
// ErrModuleNotFound is returned when a module is not found in the lifecycle.
@@ -62,13 +63,19 @@ func (s *Module) Loaded() bool {
return s.loaded
}
// RequireLoaded panics if the module is not loaded.
func (s *Module) RequireLoaded() {
// RequireLoaded panics if the module is not loaded. Any arguments are included
// in the panic message for additional context.
func (s *Module) RequireLoaded(msg ...string) {
ctx := ""
if len(msg) > 0 {
ctx = ": " + strings.Join(msg, ", ")
}
if s == nil {
panic("module is nil")
panic("module is nil" + ctx)
}
if !s.loaded {
panic(fmt.Sprintf("module %s not loaded", s.name))
panic(fmt.Sprintf("module %s not loaded", s.name) + ctx)
}
}