Skip to content

Commit 8c522a8

Browse files
authored
[api] Add error handling for missing connector in notebook setup install_example API (#4120)
Wraps connector fetching logic in try-except block to gracefully handle cases where the connector doesn't exist or when queries fail. This prevents the API from sending 500 response when setting up notebook examples with invalid connector IDs. Adds corresponding test cases to verify both scenarios: - When connector lookup returns None - When connector lookup raises an exception
1 parent 1001654 commit 8c522a8

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

desktop/core/src/desktop/api2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,11 @@ def _setup_oozie_examples(request):
14891489

14901490

14911491
def _setup_notebook_examples(request):
1492-
connector = Connector.objects.get(id=request.POST.get('connector_id'))
1492+
try:
1493+
connector = Connector.objects.get(id=request.POST.get('connector_id'))
1494+
except Exception as e:
1495+
LOG.error(f'Error getting connector: {e}')
1496+
connector = None
14931497

14941498
if connector:
14951499
dialect = connector.dialect

desktop/core/src/desktop/api2_tests.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,6 @@ def test_get_unfurl(self):
737737

738738

739739
class TestAvailableAppExamplesAPI:
740-
741740
# Using custom MockApp instead of Mock to avoid conflicts with Mock's built in 'name' attribute.
742741
@dataclass
743742
class MockApp:
@@ -895,6 +894,18 @@ def test_setup_notebook_examples_connector_none(self):
895894
assert not mock_beeswax_install_command.called
896895
mock_notebook_setup_command.assert_called_once_with(dialect='spark', user=request.user)
897896

897+
def test_setup_notebook_examples_connector_exception(self):
898+
with patch('desktop.api2.Connector.objects.get') as mock_get_connector:
899+
with patch('desktop.api2.beeswax_install_examples.Command.handle') as mock_beeswax_install_command:
900+
with patch('desktop.api2.notebook_setup.Command.handle') as mock_notebook_setup_command:
901+
request = Mock(method='POST', POST={'app_name': 'notebook', 'dialect': 'spark'}, user=Mock())
902+
mock_get_connector.side_effect = Exception('Connector matching query does not exist.')
903+
904+
_setup_notebook_examples(request)
905+
906+
assert not mock_beeswax_install_command.called
907+
mock_notebook_setup_command.assert_called_once_with(dialect='spark', user=request.user)
908+
898909
def test_setup_search_examples_with_log_analytics_demo(self):
899910
with patch('desktop.api2.search_setup.Command.handle') as mock_search_setup_command:
900911
with patch('desktop.api2.indexer_setup.Command.handle') as mock_indexer_setup_command:

0 commit comments

Comments
 (0)