Skip to content

Commit b0cda7e

Browse files
[sonic-platform-common] catch exception while creating api object (sonic-net#569)
1 parent 6738bcd commit b0cda7e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

sonic_platform_base/sonic_xcvr/xcvr_api_factory.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,9 @@ def create_xcvr_api(self):
128128
if id in id_mapping:
129129
func, args = id_mapping[id]
130130
if isinstance(args, tuple):
131-
return func(*args)
131+
try:
132+
return func(*args)
133+
except Exception as e:
134+
print(f"Error creating API: {e}")
135+
return None
132136
return None

tests/sonic_xcvr/test_xcvr_api_factory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ def test_create_xcvr_api_8436_8636(self, reader, expected_api):
7373
self.api.reader = reader
7474
api = self.api.create_xcvr_api()
7575
assert isinstance(api, expected_api)
76+
77+
@patch('sonic_platform_base.sonic_xcvr.xcvr_api_factory.XcvrApiFactory._create_api', MagicMock(side_effect=Exception('')))
78+
def test_create_xcvr_api_with_exception(self):
79+
self.api.reader = self.mock_reader
80+
CmisCodes = MagicMock()
81+
CmisMemMap = MagicMock()
82+
XcvrEeprom = MagicMock()
83+
CmisFr800gApi = MagicMock()
84+
assert self.api.create_xcvr_api() is None
7685

7786
class TestAmphBackplaneImpl:
7887
@pytest.fixture

0 commit comments

Comments
 (0)