Skip to content

Commit

Permalink
Merge pull request #9044 from joshcooper/warn_pson
Browse files Browse the repository at this point in the history
(PUP-11787) Warn if we fail to serialize and fallback
  • Loading branch information
tvpartytonight authored Mar 27, 2023
2 parents 049dc6a + 394f263 commit 4d7ebae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/puppet/network/http/api/indirected_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def do_find(indirection, key, params, request, response)

rendered_result = result

rendered_format = first_response_formatter_for(indirection.model, request) do |format|
rendered_format = first_response_formatter_for(indirection.model, request, key) do |format|
if result.respond_to?(:render)
Puppet::Util::Profiler.profile(_("Rendered result in %{format}") % { format: format }, [:http, :v3_render, format]) do
rendered_result = result.render(format)
Expand Down Expand Up @@ -157,7 +157,7 @@ def do_search(indirection, key, params, request, response)

rendered_result = nil

rendered_format = first_response_formatter_for(indirection.model, request) do |format|
rendered_format = first_response_formatter_for(indirection.model, request, key) do |format|
rendered_result = indirection.model.render_multiple(format, result)
end

Expand Down Expand Up @@ -185,14 +185,15 @@ def do_save(indirection, key, params, request, response)

# Return the first response formatter that didn't cause the yielded
# block to raise a FormatError.
def first_response_formatter_for(model, request, &block)
def first_response_formatter_for(model, request, key, &block)
formats = accepted_response_formatters_for(model, request)
formatter = formats.find do |format|
begin
yield format
true
rescue Puppet::Network::FormatHandler::FormatError => err
Puppet.log_exception(err, err.message, level: :debug)
Puppet.warning(_("Failed to serialize %{model} for '%{key}': %{detail}") %
{model: model, key: key, detail: err})
false
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/unit/network/http/api/indirected_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
indirection.save(data, "my data")
request = a_request_that_finds(data, :accept_header => "application/json, text/pson")
allow(data).to receive(:to_json).and_raise(Puppet::Network::FormatHandler::FormatError, 'Could not render to Puppet::Network::Format[json]: source sequence is illegal/malformed utf-8')
expect(Puppet).to receive(:warning).with(/Failed to serialize Puppet::IndirectorTesting for 'my data': Could not render to Puppet::Network::Format\[json\]/)

handler.call(request, response)

Expand Down Expand Up @@ -228,6 +229,7 @@
indirection.save(data, "my data")
request = a_request_that_searches(Puppet::IndirectorTesting.new("my"), :accept_header => "application/json, text/pson")
allow(data).to receive(:to_json).and_raise(Puppet::Network::FormatHandler::FormatError, 'Could not render to Puppet::Network::Format[json]: source sequence is illegal/malformed utf-8')
expect(Puppet).to receive(:warning).with(/Failed to serialize Puppet::IndirectorTesting for 'my': Could not render_multiple to Puppet::Network::Format\[json\]/)

handler.call(request, response)

Expand Down

0 comments on commit 4d7ebae

Please sign in to comment.