From e2600862c7d9074689ddf403c10ac347f6c3a90b Mon Sep 17 00:00:00 2001 From: Dogan AY Date: Fri, 6 Feb 2026 16:39:26 +0100 Subject: [PATCH] fix(apimap): log error on post apimap failure --- .../builder/agent_factory.rb | 23 +++++- .../builder/agent_factory_spec.rb | 73 +++++++++++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/packages/forest_admin_agent/lib/forest_admin_agent/builder/agent_factory.rb b/packages/forest_admin_agent/lib/forest_admin_agent/builder/agent_factory.rb index d26118251..fc3c361bf 100644 --- a/packages/forest_admin_agent/lib/forest_admin_agent/builder/agent_factory.rb +++ b/packages/forest_admin_agent/lib/forest_admin_agent/builder/agent_factory.rb @@ -161,9 +161,7 @@ def post_schema(schema, force) should_send = do_server_want_schema(api_map[:meta][:schemaFileHash]) if should_send || force - client = ForestAdminAgent::Http::ForestAdminApiRequester.new - client.post('/forest/apimaps', api_map.to_json) - ForestAdminAgent::Facades::Container.logger.log('Info', 'schema was updated, sending new version') + send_schema_to_server(api_map) else @container.resolve(:logger) ForestAdminAgent::Facades::Container.logger.log('Info', 'Schema was not updated since last run') @@ -241,6 +239,25 @@ def log_schema_skip @logger.log('Info', "[ForestAdmin] Running in #{environment} mode") end + + def send_schema_to_server(api_map) + ForestAdminAgent::Facades::Container.logger.log('Info', 'schema was updated, sending new version') + client = ForestAdminAgent::Http::ForestAdminApiRequester.new + client.post('/forest/apimaps', api_map.to_json) + rescue Faraday::Error => e + status = e.response[:status] if e.response + if status + ForestAdminAgent::Facades::Container.logger.log( + 'Error', + "Failed to send schema: invalid request (HTTP #{status})" + ) + else + ForestAdminAgent::Facades::Container.logger.log( + 'Error', + 'Failed to send schema: cannot reach ForestAdmin server' + ) + end + end end end end diff --git a/packages/forest_admin_agent/spec/lib/forest_admin_agent/builder/agent_factory_spec.rb b/packages/forest_admin_agent/spec/lib/forest_admin_agent/builder/agent_factory_spec.rb index 945cefcf1..3236830a4 100644 --- a/packages/forest_admin_agent/spec/lib/forest_admin_agent/builder/agent_factory_spec.rb +++ b/packages/forest_admin_agent/spec/lib/forest_admin_agent/builder/agent_factory_spec.rb @@ -383,6 +383,79 @@ module Builder expect(result).to eq(instance) end end + + describe 'send_schema_to_server' do + let(:instance) { described_class.instance } + let(:logger) { instance_double(Services::LoggerService) } + let(:client) { instance_double(ForestAdminAgent::Http::ForestAdminApiRequester) } + let(:api_map) { { meta: { schemaFileHash: 'abc123' }, collections: [] } } + + before do + allow(ForestAdminAgent::Facades::Container).to receive(:logger).and_return(logger) + allow(ForestAdminAgent::Http::ForestAdminApiRequester).to receive(:new).and_return(client) + allow(logger).to receive(:log) + end + + it 'logs success message and posts schema when successful' do + allow(client).to receive(:post).with('/forest/apimaps', api_map.to_json) + + instance.send(:send_schema_to_server, api_map) + + expect(logger).to have_received(:log).with('Info', 'schema was updated, sending new version') + expect(client).to have_received(:post).with('/forest/apimaps', api_map.to_json) + end + + context 'when error occurs with HTTP status' do + it 'logs error with status 400' do + error = Faraday::ClientError.new('Bad Request', { status: 400 }) + allow(client).to receive(:post).and_raise(error) + + instance.send(:send_schema_to_server, api_map) + + expect(logger).to have_received(:log).with('Info', 'schema was updated, sending new version') + expect(logger).to have_received(:log).with('Error', 'Failed to send schema: invalid request (HTTP 400)') + end + + it 'logs error with status 500' do + error = Faraday::ServerError.new('Internal Server Error', { status: 500 }) + allow(client).to receive(:post).and_raise(error) + + instance.send(:send_schema_to_server, api_map) + + expect(logger).to have_received(:log).with('Error', 'Failed to send schema: invalid request (HTTP 500)') + end + + it 'logs error with status 502' do + error = Faraday::ServerError.new('Bad Gateway', { status: 502 }) + allow(client).to receive(:post).and_raise(error) + + instance.send(:send_schema_to_server, api_map) + + expect(logger).to have_received(:log).with('Error', 'Failed to send schema: invalid request (HTTP 502)') + end + end + + context 'when error occurs without HTTP status (unreachable)' do + it 'logs connection failure message' do + error = Faraday::ConnectionFailed.new('Failed to open TCP connection') + allow(client).to receive(:post).and_raise(error) + + instance.send(:send_schema_to_server, api_map) + + expect(logger).to have_received(:log).with('Info', 'schema was updated, sending new version') + expect(logger).to have_received(:log).with('Error', 'Failed to send schema: cannot reach ForestAdmin server') + end + + it 'logs timeout error message' do + error = Faraday::TimeoutError.new('execution expired') + allow(client).to receive(:post).and_raise(error) + + instance.send(:send_schema_to_server, api_map) + + expect(logger).to have_received(:log).with('Error', 'Failed to send schema: cannot reach ForestAdmin server') + end + end + end end end end