From 2eb4b14b8a8ad58a30ae564e3fe6b5d61dcf9b1e Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 27 May 2026 14:19:17 -0400 Subject: [PATCH] Fixes #39372 - Extract displayMessage from validation errors When the Foreman/Katello API returns validation errors (422 status), it includes detailed error messages in the 'displayMessage' field. The exception handler was only checking 'message' and 'full_messages', causing detailed error information to be lost. This commit updates handle_unprocessable_entity to check for 'displayMessage' first, matching the behavior of handle_foreman_error which already handles this field correctly. Before: Could not update the host After: Could not update the host: Validation failed: Host example.com: Cannot add content view environment to content facet. The host's content source does not sync lifecycle environment 'Library'. --- lib/hammer_cli_foreman/exception_handler.rb | 2 +- test/unit/exception_handler_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/hammer_cli_foreman/exception_handler.rb b/lib/hammer_cli_foreman/exception_handler.rb index a8529b232..359af9e01 100644 --- a/lib/hammer_cli_foreman/exception_handler.rb +++ b/lib/hammer_cli_foreman/exception_handler.rb @@ -35,7 +35,7 @@ def handle_foreman_unauthorized(e) def handle_unprocessable_entity(e) response = JSON.parse(e.response) response = HammerCLIForeman.record_to_common_format(response) unless response.has_key?('message') - print_error response['message'] || response['full_messages'] + print_error response['message'] || response['full_messages'] || response['displayMessage'] HammerCLI::EX_DATAERR end diff --git a/test/unit/exception_handler_test.rb b/test/unit/exception_handler_test.rb index a8dcad290..43b5d2cdb 100644 --- a/test/unit/exception_handler_test.rb +++ b/test/unit/exception_handler_test.rb @@ -29,6 +29,17 @@ _(err_code).must_equal HammerCLI::EX_DATAERR end + it "should print displayMessage on unprocessable entity exception" do + response = <<-RESPONSE + {"displayMessage":"Validation failed: Cannot add content view environment to content facet.","errors":{"base":["Cannot add content view environment to content facet."]}} + RESPONSE + + ex = RestClient::UnprocessableEntity.new(response) + output.expects(:print_error).with(heading, "Validation failed: Cannot add content view environment to content facet.") + err_code = handler.handle_exception(ex, :heading => heading) + _(err_code).must_equal HammerCLI::EX_DATAERR + end + it "should handle argument error" do ex = ArgumentError.new output.expects(:print_error).with(heading, ex.message)