fix: handle 404 in tn_eligible_for_port_out_pin?

Phillip Davis created

Change summary

lib/bandwidth_tn_options.rb       |  8 +++++++-
test/test_bandwidth_tn_options.rb | 19 +++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletion(-)

Detailed changes

lib/bandwidth_tn_options.rb 🔗

@@ -14,7 +14,13 @@ class BandwidthTNOptions
 			password: secret
 		)
 		EMPromise.resolve(nil).then do
-			tn = BandwidthIris::Tn.get(client, tel_local)
+			begin
+				tn = BandwidthIris::Tn.get(client, tel_local)
+			rescue BandwidthIris::Errors::GenericError => e
+				raise e unless e.http_status == 404
+
+				next false
+			end
 			details = tn.get_details()
 			details[:tier] == 0.0 && details[:on_net_vendor] == true
 		end

test/test_bandwidth_tn_options.rb 🔗

@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+require "test_helper"
+
+class BandwidthTNOptionsTest < Minitest::Test
+	CREDS = ['account_id', 'token', 'secret', '+15551234567'].freeze
+
+	def test_tn_eligible_for_port_out_pin_returns_false_on_404
+		error = BandwidthIris::Errors::GenericError.new("404", "Not Found", 404)
+
+		with_stubs([
+			[BandwidthIris::Tn, :get, ->(_client, _tel) { raise error }]
+		]) do
+			result = BandwidthTNOptions.tn_eligible_for_port_out_pin?(CREDS).sync
+			refute result, "Expected false when TN returns 404"
+		end
+	end
+	em :test_tn_eligible_for_port_out_pin_returns_false_on_404
+end