some error work

This commit is contained in:
Joel Wetzell
2025-11-22 11:21:37 -06:00
parent 4b07c85508
commit 4ae5261d25
4 changed files with 60 additions and 11 deletions

View File

@@ -29,12 +29,15 @@ func main() {
if err != nil { if err != nil {
return err return err
} }
router, err := showbridge.NewRouter(ctx, config) router, moduleErrors, routeErrors := showbridge.NewRouter(ctx, config)
if err != nil { for _, moduleError := range moduleErrors {
return err slog.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
}
for _, routeError := range routeErrors {
slog.Error("problem initializing module", "index", routeError.Index, "error", routeError.Error)
} }
router.Run() router.Run()
return nil return nil
}, },
} }

View File

@@ -6,6 +6,12 @@ import (
"sync" "sync"
) )
type ModuleError struct {
Index int
Config ModuleConfig
Error error
}
type Module interface { type Module interface {
Id() string Id() string
Type() string Type() string

View File

@@ -7,6 +7,12 @@ import (
"github.com/jwetzell/showbridge-go/internal/processing" "github.com/jwetzell/showbridge-go/internal/processing"
) )
type RouteError struct {
Index int
Config RouteConfig
Error error
}
type Route struct { type Route struct {
index int index int
Input string Input string

View File

@@ -13,7 +13,7 @@ type Router struct {
RouteInstances []*Route RouteInstances []*Route
} }
func NewRouter(ctx context.Context, config Config) (*Router, error) { func NewRouter(ctx context.Context, config Config) (*Router, []ModuleError, []RouteError) {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug, Level: slog.LevelDebug,
@@ -29,18 +29,35 @@ func NewRouter(ctx context.Context, config Config) (*Router, error) {
RouteInstances: []*Route{}, RouteInstances: []*Route{},
} }
for _, moduleDecl := range config.Modules { var moduleErrors []ModuleError
for moduleIndex, moduleDecl := range config.Modules {
moduleInfo, ok := moduleRegistry[moduleDecl.Type] moduleInfo, ok := moduleRegistry[moduleDecl.Type]
if !ok { if !ok {
return nil, fmt.Errorf("problem loading module registration for module type: %s", moduleDecl.Type) if moduleErrors == nil {
moduleErrors = []ModuleError{}
}
moduleErrors = append(moduleErrors, ModuleError{
Index: moduleIndex,
Config: moduleDecl,
Error: fmt.Errorf("module type not defined"),
})
} }
moduleInstanceExists := false moduleInstanceExists := false
for _, moduleInstance := range router.ModuleInstances { for _, moduleInstance := range router.ModuleInstances {
if moduleInstance.Id() == moduleDecl.Id { if moduleInstance.Id() == moduleDecl.Id {
moduleInstanceExists = true moduleInstanceExists = true
slog.Warn("module id conflict", "id", moduleDecl.Id, "type", moduleDecl.Type) if moduleErrors == nil {
moduleErrors = []ModuleError{}
}
moduleErrors = append(moduleErrors, ModuleError{
Index: moduleIndex,
Config: moduleDecl,
Error: fmt.Errorf("duplicate module id"),
})
break break
} }
} }
@@ -48,7 +65,15 @@ func NewRouter(ctx context.Context, config Config) (*Router, error) {
if !moduleInstanceExists { if !moduleInstanceExists {
moduleInstance, err := moduleInfo.New(moduleDecl) moduleInstance, err := moduleInfo.New(moduleDecl)
if err != nil { if err != nil {
return nil, err if moduleErrors == nil {
moduleErrors = []ModuleError{}
}
moduleErrors = append(moduleErrors, ModuleError{
Index: moduleIndex,
Config: moduleDecl,
Error: err,
})
continue
} }
router.ModuleInstances = append(router.ModuleInstances, moduleInstance) router.ModuleInstances = append(router.ModuleInstances, moduleInstance)
@@ -56,20 +81,29 @@ func NewRouter(ctx context.Context, config Config) (*Router, error) {
} }
var routeErrors []RouteError
for routeIndex, routeDecl := range config.Routes { for routeIndex, routeDecl := range config.Routes {
route, err := NewRoute(routeIndex, routeDecl, &router) route, err := NewRoute(routeIndex, routeDecl, &router)
if err != nil { if err != nil {
slog.Error("problem creating route", "index", routeIndex, "error", err.Error()) if routeErrors == nil {
routeErrors = []RouteError{}
}
routeErrors = append(routeErrors, RouteError{
Index: routeIndex,
Config: routeDecl,
Error: err,
})
continue continue
} }
router.RouteInstances = append(router.RouteInstances, route) router.RouteInstances = append(router.RouteInstances, route)
} }
for _, moduleInstance := range router.ModuleInstances { for _, moduleInstance := range router.ModuleInstances {
slog.Debug("registering router with module", "id", moduleInstance.Id())
moduleInstance.RegisterRouter(&router) moduleInstance.RegisterRouter(&router)
} }
return &router, nil return &router, moduleErrors, routeErrors
} }
func (r *Router) Run() { func (r *Router) Run() {