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)