Report variant errors instead of panicing

Variant errors can be introduced by Blueprints files, so print
a module error instead of panicing.
This commit is contained in:
Colin Cross 2015-03-03 17:37:03 -08:00
parent cb7b9ad6ca
commit 174ae059cc
2 changed files with 23 additions and 7 deletions

View file

@ -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,

View file

@ -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)