From 174ae059cce33116c04c61dc885e6e5b250eb19f Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Tue, 3 Mar 2015 17:37:03 -0800 Subject: [PATCH] Report variant errors instead of panicing Variant errors can be introduced by Blueprints files, so print a module error instead of panicing. --- context.go | 25 +++++++++++++++++++------ module_ctx.go | 5 ++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/context.go b/context.go index 9fea1db..9ab359a 100644 --- a/context.go +++ b/context.go @@ -655,12 +655,14 @@ func (c *Context) processSubdirs( } func (c *Context) createVariants(origModule *moduleInfo, mutatorName string, - variantNames []string) []*moduleInfo { + variantNames []string) ([]*moduleInfo, []error) { newModules := []*moduleInfo{} origVariantName := origModule.name group := origModule.group + var errs []error + for i, variantName := range variantNames { typeName := group.typeName factory, ok := c.moduleFactories[typeName] @@ -713,7 +715,10 @@ func (c *Context) createVariants(origModule *moduleInfo, mutatorName string, newModules = append(newModules, newModule) c.moduleInfo[newModule.logicModule] = newModule - c.convertDepsToVariant(newModule, newSubName) + newErrs := c.convertDepsToVariant(newModule, newSubName) + if len(newErrs) > 0 { + errs = append(errs, newErrs...) + } } // Mark original variant as invalid. Modules that depend on this module will still @@ -721,10 +726,11 @@ func (c *Context) createVariants(origModule *moduleInfo, mutatorName string, origModule.logicModule = nil origModule.splitModules = newModules - return newModules + return newModules, errs } -func (c *Context) convertDepsToVariant(module *moduleInfo, newSubName subName) { +func (c *Context) convertDepsToVariant(module *moduleInfo, newSubName subName) (errs []error) { + for i, dep := range module.directDeps { if dep.logicModule == nil { var newDep *moduleInfo @@ -735,12 +741,19 @@ func (c *Context) convertDepsToVariant(module *moduleInfo, newSubName subName) { } } if newDep == nil { - panic(fmt.Sprintf("failed to find variant %s for module %s needed by %s", - newSubName.variantName, dep.group.properties.Name, module.group.properties.Name)) + errs = append(errs, &Error{ + Err: fmt.Errorf("failed to find variant %q for module %q needed by %q", + newSubName.variantName, dep.group.properties.Name, + module.group.properties.Name), + Pos: module.group.pos, + }) + continue } module.directDeps[i] = newDep } } + + return errs } func (c *Context) processModuleDef(moduleDef *parser.Module, diff --git a/module_ctx.go b/module_ctx.go index 4931b6d..4f65a8d 100644 --- a/module_ctx.go +++ b/module_ctx.go @@ -367,7 +367,10 @@ type BottomUpMutator func(mctx BottomUpMutatorContext) // automatically be updated to point to the first variant. func (mctx *mutatorContext) CreateVariants(variantNames ...string) []Module { ret := []Module{} - modules := mctx.context.createVariants(mctx.module, mctx.name, variantNames) + modules, errs := mctx.context.createVariants(mctx.module, mctx.name, variantNames) + if len(errs) > 0 { + mctx.errs = append(mctx.errs, errs...) + } for _, module := range modules { ret = append(ret, module.logicModule)