puppetlabs/puppet · error · Puppet::Network::FormatHandler::FormatError

Could not mime to %{format}: %{err}

Error message

Could not mime to %{format}: %{err}

What it means

Instance-level FormatSupport#mime resolves the mime type for a format and wraps failures as FormatError 'Could not mime to %{format}: %{err}'. Since Format#mime is just an attribute read on the format object, the wrapped error nearly always comes from get_format: the requested format is not registered in this process. It looks like a serialization error but is a format-lookup error.

Source

Thrown at lib/puppet/network/format_support.rb:125

    Puppet::Util::Json.dump(to_data_hash, *args)
  end

  def render(format = nil)
    format ||= self.class.default_format

    self.class.get_format(format).render(self)
  rescue => err
    # TRANSLATORS "render" is a function name and should not be translated
    raise Puppet::Network::FormatHandler::FormatError, _("Could not render to %{format}: %{err}") % { format: format, err: err }, err.backtrace
  end

  def mime(format = nil)
    format ||= self.class.default_format

    self.class.get_format(format).mime
  rescue => err
    # TRANSLATORS "mime" is a function name and should not be translated
    raise Puppet::Network::FormatHandler::FormatError, _("Could not mime to %{format}: %{err}") % { format: format, err: err }, err.backtrace
  end

  def support_format?(name)
    self.class.support_format?(name)
  end

  # @comment Document to_data_hash here as it is called as a hook from to_msgpack if it exists
  # @!method to_data_hash(*args)
  # @api public
  # @abstract
  # This method may be implemented to return a hash object that is used for serializing.
  # The object returned by this method should contain all the info needed to instantiate it again.
  # If the method exists it will be called from to_msgpack and other serialization methods.
  # @return [Hash]
end

View on GitHub (pinned to e227c27540)

Solutions

  1. Call mime() with no argument — it falls back to the class default_format, which is always registered.
  2. Validate the name first: Puppet::Network::FormatHandler.formats.include?(name.to_sym).
  3. Load the plugin that registers the missing format.
  4. Catch the FormatError and fall back to the default format's mime type.

Example fix

# before
content_type = obj.mime(params[:format]) # FormatError when unregistered

# after
known = Widget.supported_formats.map(&:to_s)
fmt = known.include?(params[:format]) ? params[:format] : nil # nil → default_format
content_type = obj.mime(fmt)
Defensive patterns

Strategy: validation

Validate before calling

known = Widget.supported_formats.map(&:to_s)
fmt = known.include?(requested.to_s) ? requested : nil # nil arg → default_format
content_type = widget.mime(fmt)

Try / catch

begin
  widget.mime(fmt)
rescue Puppet::Network::FormatHandler::FormatError
  widget.mime # the default format is always registered
end

Prevention

When it happens

Trigger: obj.mime(:bogus) or obj.mime(user_supplied_string) where the name is not a registered format; also defaulting to a format whose registering plugin failed to load.

Common situations: Passing request-derived format names into mime(); typos in format names; pluginsync not delivering a custom format; version skew where a format was removed.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/2fb847862a222d68. Report an issue: GitHub.