diff --git a/CHANGELOG.md b/CHANGELOG.md index e463c6d..86e4f72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## [Unreleased] +## 0.3.1 / 2026-03-05 +### Fixed +* Strip whitespace from IDs in FHIR Base.find to prevent bad URI errors +* Handle ApiError/InvalidURIError gracefully in Organisation.find + ## 0.3.0 / 2026-02-26 ### Added * Added specific error classes (InvalidURIError, UnauthorizedError) for granular error handling diff --git a/lib/ndr_lookup/fhir/base.rb b/lib/ndr_lookup/fhir/base.rb index f51d2ba..2bd4d41 100644 --- a/lib/ndr_lookup/fhir/base.rb +++ b/lib/ndr_lookup/fhir/base.rb @@ -27,8 +27,9 @@ def sync # Finds a specific FHIR resource by type and ID # @return [Hash] Parsed FHIR resource def find(resource_type, id) - with_error_handling("#{resource_type} with ID '#{id}' not found") do - response = connection.get("#{endpoint}/#{resource_type}/#{id}", headers) + sanitized_id = id.to_s.strip + with_error_handling("#{resource_type} with ID '#{sanitized_id}' not found") do + response = connection.get("#{endpoint}/#{resource_type}/#{sanitized_id}", headers) JSON.parse(response.body) end end diff --git a/lib/ndr_lookup/fhir/odt/organisation.rb b/lib/ndr_lookup/fhir/odt/organisation.rb index 15eb3ab..f4325e6 100644 --- a/lib/ndr_lookup/fhir/odt/organisation.rb +++ b/lib/ndr_lookup/fhir/odt/organisation.rb @@ -21,6 +21,9 @@ def find(id) rescue Client::ResourceNotFound => e logger.info("Organization not found: #{e.message}") nil + rescue Client::ApiError, Client::InvalidURIError => e + logger.warn("Organization lookup failed for '#{id}': #{e.message}") + nil end # ActiveRecord subs .all in to /all which then becomes like finding the id 'all' diff --git a/lib/ndr_lookup/version.rb b/lib/ndr_lookup/version.rb index 8493785..194f786 100644 --- a/lib/ndr_lookup/version.rb +++ b/lib/ndr_lookup/version.rb @@ -1,3 +1,3 @@ module NdrLookup - VERSION = '0.3.0'.freeze + VERSION = '0.3.1'.freeze end diff --git a/test/fhir/base_test.rb b/test/fhir/base_test.rb index f9c1df3..67d5f3e 100644 --- a/test/fhir/base_test.rb +++ b/test/fhir/base_test.rb @@ -48,6 +48,18 @@ def test_should_raise_api_error_on_timeout TestClient.find('Organization', 'X26') end end + + def test_find_strips_whitespace_from_id + url = "#{ODT_ENDPOINT}/Organization/7A3C4" + file = File.new("#{RESPONSES_DIR}/fhir/organisation_find_success_response.txt") + stub_request(:get, url).to_return(file) + + response = TestClient.find('Organization', ' 7A3C4 ') + + # WebMock would raise an error if the id is not sanitized + assert_kind_of(Hash, response) + assert_requested(:get, url) + end end class TestClient < Fhir::Base diff --git a/test/fhir/odt/organisation_test.rb b/test/fhir/odt/organisation_test.rb index fcc705b..0021c6e 100644 --- a/test/fhir/odt/organisation_test.rb +++ b/test/fhir/odt/organisation_test.rb @@ -74,6 +74,26 @@ def test_sync_handles_api_error_gracefully assert_equal [], orgs end + + def test_find_handles_api_error_gracefully + url = "#{ODT_ENDPOINT}/Organization/X26" + stub_request(:get, url).to_timeout + + org = Organisation.find('X26') + + assert_nil org + end + + def test_find_strips_whitespace_from_id + url = "#{ODT_ENDPOINT}/Organization/7A3C4" + file = File.new("#{RESPONSES_DIR}/fhir/organisation_find_success_response.txt") + stub_request(:get, url).to_return(file) + + Organisation.find('7A3C4 ') + + # WebMock would raise an error if the id is not sanitized + assert_requested(:get, url) + end end end end