diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 8734357..b933bac 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -29,12 +29,15 @@ func main() { if err != nil { return err } - router, err := showbridge.NewRouter(ctx, config) - if err != nil { - return err + router, moduleErrors, routeErrors := showbridge.NewRouter(ctx, config) + for _, moduleError := range moduleErrors { + 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() - return nil }, } diff --git a/module.go b/module.go index 3c90ac3..c8d4d52 100644 --- a/module.go +++ b/module.go @@ -6,6 +6,12 @@ import ( "sync" ) +type ModuleError struct { + Index int + Config ModuleConfig + Error error +} + type Module interface { Id() string Type() string diff --git a/route.go b/route.go index 5a6a626..7e429ff 100644 --- a/route.go +++ b/route.go @@ -7,6 +7,12 @@ import ( "github.com/jwetzell/showbridge-go/internal/processing" ) +type RouteError struct { + Index int + Config RouteConfig + Error error +} + type Route struct { index int Input string diff --git a/router.go b/router.go index 59d35aa..f82ad66 100644 --- a/router.go +++ b/router.go @@ -13,7 +13,7 @@ type Router struct { 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{ Level: slog.LevelDebug, @@ -29,18 +29,35 @@ func NewRouter(ctx context.Context, config Config) (*Router, error) { RouteInstances: []*Route{}, } - for _, moduleDecl := range config.Modules { + var moduleErrors []ModuleError + + for moduleIndex, moduleDecl := range config.Modules { moduleInfo, ok := moduleRegistry[moduleDecl.Type] 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 for _, moduleInstance := range router.ModuleInstances { if moduleInstance.Id() == moduleDecl.Id { 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 } } @@ -48,7 +65,15 @@ func NewRouter(ctx context.Context, config Config) (*Router, error) { if !moduleInstanceExists { moduleInstance, err := moduleInfo.New(moduleDecl) 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) @@ -56,20 +81,29 @@ func NewRouter(ctx context.Context, config Config) (*Router, error) { } + var routeErrors []RouteError for routeIndex, routeDecl := range config.Routes { route, err := NewRoute(routeIndex, routeDecl, &router) 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 } router.RouteInstances = append(router.RouteInstances, route) } for _, moduleInstance := range router.ModuleInstances { + slog.Debug("registering router with module", "id", moduleInstance.Id()) moduleInstance.RegisterRouter(&router) } - return &router, nil + return &router, moduleErrors, routeErrors } func (r *Router) Run() {