{"record":{"id":"5565548457735f74","repo":"puppetlabs/puppet","slug":"subject-what","errorCode":null,"errorMessage":"#{subject} #{what},","messagePattern":"#\\{subject\\} #\\{what\\},","errorType":"validation","errorClass":"Puppet::Pops::Types::TypeAssertionError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pops/types/type_asserter.rb","lineNumber":43,"sourceCode":"  # @param subject [String,Array] String to be prepended to the exception message or Array where the first element is\n  #                               a format string and the rest are arguments to that format string\n  # @param expected_type [PAnyType] Expected type for the value\n  # @param value [Object] Value to check\n  # @param nil_ok [Boolean] Can be true to allow nil value. Optional and defaults to false\n  # @return The value argument\n  #\n  # @api public\n  def self.assert_instance_of(subject, expected_type, value, nil_ok = false, &block)\n    unless value.nil? && nil_ok\n      report_type_mismatch(subject, expected_type, TypeCalculator.singleton.infer_set(value), &block) unless expected_type.instance?(value)\n    end\n    value\n  end\n\n  def self.report_type_mismatch(subject, expected_type, actual_type, what = 'has wrong type')\n    subject = yield(subject) if block_given?\n    subject = subject[0] % subject[1..] if subject.is_a?(Array)\n    raise TypeAssertionError.new(\n      TypeMismatchDescriber.singleton.describe_mismatch(\"#{subject} #{what},\", expected_type, actual_type), expected_type, actual_type\n    )\n  end\n  private_class_method :report_type_mismatch\nend\nend\n","sourceCodeStart":25,"sourceCodeEnd":50,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pops/types/type_asserter.rb#L25-L50","documentation":"TypeAsserter.report_type_mismatch (lib/puppet/pops/types/type_asserter.rb:43) builds the prefix \"#{subject} #{what},\" (default what = 'has wrong type') and passes it to TypeMismatchDescriber.describe_mismatch, which appends a detailed expected-vs-actual description (including nested structure mismatches), then raises TypeAssertionError carrying the expected and actual types. It is fired by TypeAsserter.assert_instance_of — the standard Ruby-API gate used by Puppet functions and providers to enforce parameter types — whenever a passed value fails expected_type.instance?(value).","triggerScenarios":"Calling Puppet::Pops::Types::TypeAsserter.assert_instance_of('The parameter', PIntegerType::DEFAULT, '42') — a String where Integer is required; dispatchers of Puppet::Functions::Function with typed parameters receiving mismatched arguments; assert_keywords_or_instance style calls in the Ruby API receiving wrong-typed keyword args.","commonSituations":"Writing custom Ruby functions for Puppet and forgetting that DSL scalars arrive as Ruby types with Puppet semantics (e.g. default/undef); API callers passing strings from JSON where the signature declares Integer/Array; version upgrades that tightened signatures with assert_instance_of where previously untyped args were accepted.","solutions":["Pass a value matching the asserted type — coerce first: '42'.to_i, or use the type's cast helpers","If nil is legitimate, pass the nil_ok = true flag: assert_instance_of(subject, type, value, true)","Use a block to customize the subject: assert_instance_of(['parameter %s', name], type, value)","For optional parameters, branch on nil before asserting"],"exampleFix":"# before\nPuppet::Pops::Types::TypeAsserter.assert_instance_of('The value', Puppet::Pops::Types::PIntegerType::DEFAULT, '42')\n# after\nPuppet::Pops::Types::TypeAsserter.assert_instance_of('The value', Puppet::Pops::Types::PIntegerType::DEFAULT, 42)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"# Ruby: narrow the value before asserting, mirroring the asserter's own check\nPINT = Puppet::Pops::Types::PIntegerType::DEFAULT\ndef int_or_nil!(value, subject)\n  return nil if value.nil?                       # decide nil policy yourself\n  return value if PINT.instance?(value)          # same predicate the asserter uses\n  raise TypeError, \"#{subject} expected Integer, got #{value.class}\"\nend","tryCatchPattern":"begin\n  Puppet::Pops::Types::TypeAsserter.assert_instance_of(subject, expected_type, value)\nrescue Puppet::Pops::Types::TypeAssertionError => e\n  # e.expected_type / e.actual_type are exposed for structured handling\n  raise ArgumentError, \"bad input from caller: #{e.message}\"\nend","preventionTips":["Call expected_type.instance?(value) yourself when you want a branch instead of an exception","Pass nil_ok = true only for genuinely optional parameters","Coerce at the boundary (JSON string -> Integer) before typed APIs see the value","Use the subject-array form ['parameter %s of function %s', name, fn] for actionable messages"],"tags":["puppet","pops","type-assertion","ruby-api","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}