From 5a25fcb192ccb6f19ebc0b7bc7894adde2233a43 Mon Sep 17 00:00:00 2001 From: Jan Kessler Date: Fri, 28 Jun 2024 17:24:22 +0200 Subject: [PATCH] raise a specific error when a required server tag is not available (#1091) Co-authored-by: Ahmad Farhat --- app/controllers/concerns/bbb_errors.rb | 6 ++++++ app/models/server.rb | 2 +- spec/models/server_spec.rb | 2 +- spec/requests/bigbluebutton_api_controller_spec.rb | 2 +- test/controllers/bigbluebutton_api_controller_test.rb | 2 +- test/models/server_test.rb | 2 +- 6 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/controllers/concerns/bbb_errors.rb b/app/controllers/concerns/bbb_errors.rb index 04fbeb5e..9a4f84b6 100644 --- a/app/controllers/concerns/bbb_errors.rb +++ b/app/controllers/concerns/bbb_errors.rb @@ -57,4 +57,10 @@ def initialize super('serverUnavailable', 'The server for this meeting is disabled/offline.') end end + + class ServerTagUnavailableError < BBBError + def initialize(tag) + super('serverTagUnavailable', "There is no available server with the required tag=#{tag}.") + end + end end diff --git a/app/models/server.rb b/app/models/server.rb index 75ba0ab2..d92a0b5c 100644 --- a/app/models/server.rb +++ b/app/models/server.rb @@ -300,7 +300,7 @@ def self.find_available(tag_arg = nil) ids_loads = redis.zrange('server_load', 0, -1, with_scores: true) raise RecordNotFound.new("Could not find any available servers.", name, nil) if ids_loads.blank? if !tag.nil? && ids_loads.none? { |myid, _| redis.hget(key(myid), 'tag') == tag } - raise RecordNotFound.new("Could not find any available servers with tag=#{tag}.", name, nil) if tag_required + raise BBBErrors::ServerTagUnavailableError, tag if tag_required tag = nil # fall back to servers without tag end ids_loads = ids_loads.select { |myid, _| redis.hget(key(myid), 'tag') == tag } diff --git a/spec/models/server_spec.rb b/spec/models/server_spec.rb index 1e3f3d23..54ca5b95 100644 --- a/spec/models/server_spec.rb +++ b/spec/models/server_spec.rb @@ -431,7 +431,7 @@ it 'raises error with specific message' do expect { described_class.find_available('test-tag!') - }.to raise_error(ApplicationRedisRecord::RecordNotFound, "Could not find any available servers with tag=test-tag.") + }.to raise_error(BBBErrors::ServerTagUnavailableError) end end end diff --git a/spec/requests/bigbluebutton_api_controller_spec.rb b/spec/requests/bigbluebutton_api_controller_spec.rb index 36c2e79f..169c7f61 100644 --- a/spec/requests/bigbluebutton_api_controller_spec.rb +++ b/spec/requests/bigbluebutton_api_controller_spec.rb @@ -511,7 +511,7 @@ get bigbluebutton_api_create_url, params: { meetingID: "test-meeting-1", "meta_server-tag" => "test-tag!" } response_xml = Nokogiri.XML(response.body) - expected_error = BBBErrors::InternalError.new("Could not find any available servers with tag=test-tag.") + expected_error = BBBErrors::ServerTagUnavailableError.new("test-tag") expect(response_xml.at_xpath("/response/returncode").text).to(eq("FAILED")) expect(response_xml.at_xpath("/response/messageKey").text).to(eq(expected_error.message_key)) expect(response_xml.at_xpath("/response/message").text).to(eq(expected_error.message)) diff --git a/test/controllers/bigbluebutton_api_controller_test.rb b/test/controllers/bigbluebutton_api_controller_test.rb index 89f29c24..445f6bf1 100644 --- a/test/controllers/bigbluebutton_api_controller_test.rb +++ b/test/controllers/bigbluebutton_api_controller_test.rb @@ -450,7 +450,7 @@ class BigBlueButtonApiControllerTest < ActionDispatch::IntegrationTest response_xml = Nokogiri::XML(@response.body) - expected_error = InternalError.new('Could not find any available servers with tag=test-tag.') + expected_error = ServerTagUnavailableError.new('test-tag') assert_equal 'FAILED', response_xml.at_xpath('/response/returncode').text assert_equal expected_error.message_key, response_xml.at_xpath('/response/messageKey').text diff --git a/test/models/server_test.rb b/test/models/server_test.rb index c20be75a..57ef0070 100644 --- a/test/models/server_test.rb +++ b/test/models/server_test.rb @@ -359,7 +359,7 @@ class ServerTest < ActiveSupport::TestCase redis.zadd('server_load', 1, 'test-2') end - assert_raises(ApplicationRedisRecord::RecordNotFound, "Could not find any available servers with tag=test-tag.") do + assert_raises(BBBErrors::ServerTagUnavailableError) do Server.find_available('test-tag!') end end