Bug #15046

bad error message for type defined without namevar

Added by Chris Price about 1 year ago. Updated 2 months ago.

Status:Needs DecisionStart date:06/14/2012
Priority:LowDue date:
Assignee:eric sorenson% Done:

0%

Category:-
Target version:-
Affected Puppet version: Branch:
Keywords: customer

Description

If you write a type that looks like this:

Puppet::Type.newtype(:foo) do

  ensurable do
    defaultvalues
    defaultto :present
  end
end

And you then try to instantiate it using something like:

resource = Puppet::Type::Foo.new(:title => 'myfoo')

You will get a very bad error message about Array not having a method called “merge” or something to that effect. This is because in lib/puppet/type.rb around line 225 we have:

 
def self.title_patterns
    case key_attributes.length
    when 0; []
    when 1;
...

Which returns an empty array if there is no namevar, and then in lib/puppet/resource.rb around line 423:

def parse_title
    h = {}
    type = resource_type
    if type.respond_to? :title_patterns
      type.title_patterns.each { |regexp, symbols_and_lambdas|
        if captures = regexp.match(title.to_s)
          symbols_and_lambdas.zip(captures[1..-1]).each { |symbol_and_lambda,capture|
            sym, lam = symbol_and_lambda
            #self[sym] = lam.call(capture)
            h[sym] = lam.call(capture)
          }
          return h
        end
      }
    else
      return { :name => title.to_s }
    end
  end

Basically this method falls through and unintentionally returns that empty array in that case… which leads to a very bad error message. I would have submitted a fix but I’m not sure what the desired behavior is; I suspect it’s that we should give a clear error message about there being no namevar, but if so, I’m not sure whether that should happen in type.rb or resource.rb.


Related issues

Related to Puppet - Bug #15264: Custom title patterns can fail cryptically Closed 06/27/2012
Duplicates Puppet - Bug #5220: when namevar is not specified for a type, error message i... Accepted 11/08/2010

History

#1 Updated by eric sorenson 10 months ago

  • Status changed from Unreviewed to Investigating
  • Assignee set to Chris Price

Chris, does the error messages guidance from UX help you craft an error? Seems like since you went through all the work to track this down it’d be a shame not to fix it.

#2 Updated by Chris Price 10 months ago

I think what was blocking me was just the decision as to whether the error handling should go in “type.rb” or “resource.rb”. Coming up with a meaningful error message shouldn’t be hard.

If you need me to try to pick this up let me know, I’ll see if I can get some time. It should be really easy to repro though.

#3 Updated by Chris Price 10 months ago

  • Status changed from Investigating to Needs Decision
  • Assignee changed from Chris Price to eric sorenson

#4 Updated by Charlie Sharpsteen 2 months ago

  • Keywords set to customer

Also available in: Atom PDF