SIENTIAPDE-1248: Refactor MinIO activity to raise specific exception types for better error handling

This commit is contained in:
Bruno Domingues
2025-10-06 10:58:48 -03:00
parent ec5b9df125
commit 520975e7aa
2 changed files with 11 additions and 11 deletions

View File

@@ -71,7 +71,7 @@ class MinIO(BaseActivity):
notification_handler: Notification handler for alerts and monitoring notification_handler: Notification handler for alerts and monitoring
Raises: Raises:
Exception: If boto3 client initialization fails ConnectionError: If boto3 client initialization fails
""" """
BaseActivity.__init__(self, logger, notification_handler, set_error_counter=True) BaseActivity.__init__(self, logger, notification_handler, set_error_counter=True)
self.endpoint_url = endpoint_url self.endpoint_url = endpoint_url
@@ -109,7 +109,7 @@ class MinIO(BaseActivity):
except Exception as e: except Exception as e:
error_msg = f'Failed to initialize MinIO client: {str(e)}' error_msg = f'Failed to initialize MinIO client: {str(e)}'
self.error(error_msg) self.error(error_msg)
raise Exception(error_msg) from e raise ConnectionError(error_msg) from e
@activity.defn(name='fetch_file_from_minio') @activity.defn(name='fetch_file_from_minio')
async def fetch_file_from_minio(self, input_data: dict[str, Any]) -> BytesIO: async def fetch_file_from_minio(self, input_data: dict[str, Any]) -> BytesIO:
@@ -137,7 +137,7 @@ class MinIO(BaseActivity):
BytesIO: File content as a file-like object BytesIO: File content as a file-like object
Raises: Raises:
Exception: If file fetch fails due to network, permission, or other errors OSError: If file fetch fails due to network, permission, or other errors
""" """
metadata = input_data.get('metadata', {}) metadata = input_data.get('metadata', {})
bucket_name = input_data['bucket_name'] bucket_name = input_data['bucket_name']
@@ -173,7 +173,7 @@ class MinIO(BaseActivity):
attachment_content=trace, attachment_content=trace,
) )
self.error(trace, metadata=metadata) self.error(trace, metadata=metadata)
raise Exception(error_msg) from e raise OSError(error_msg) from e
@activity.defn(name='delete_file_from_minio') @activity.defn(name='delete_file_from_minio')
async def delete_file_from_minio(self, input_data: dict[str, Any]) -> None: async def delete_file_from_minio(self, input_data: dict[str, Any]) -> None:
@@ -199,7 +199,7 @@ class MinIO(BaseActivity):
None None
Raises: Raises:
Exception: If file deletion fails due to permission or other errors OSError: If file deletion fails due to permission or other errors
""" """
metadata = input_data.get('metadata', {}) metadata = input_data.get('metadata', {})
bucket_name = input_data['bucket_name'] bucket_name = input_data['bucket_name']
@@ -226,4 +226,4 @@ class MinIO(BaseActivity):
attachment_content=trace, attachment_content=trace,
) )
self.error(trace, metadata=metadata) self.error(trace, metadata=metadata)
raise Exception(error_msg) from e raise OSError(error_msg) from e

View File

@@ -56,7 +56,7 @@ def test___init___failure(mock_boto3_client):
logger = MagicMock() logger = MagicMock()
notification_handler = MagicMock() notification_handler = MagicMock()
with raises(Exception, match='Failed to initialize MinIO client'): with raises(ConnectionError, match='Failed to initialize MinIO client'):
MinIO( MinIO(
endpoint_url='http://localhost:9000', endpoint_url='http://localhost:9000',
access_key='minioadmin', access_key='minioadmin',
@@ -156,7 +156,7 @@ async def test_fetch_file_from_minio_file_not_found(minio):
} }
# Act & Assert # Act & Assert
with raises(Exception, match='Error fetching file from MinIO'): with raises(OSError, match='Error fetching file from MinIO'):
await minio.fetch_file_from_minio(input_data) await minio.fetch_file_from_minio(input_data)
# Verify notification was sent # Verify notification was sent
@@ -179,7 +179,7 @@ async def test_fetch_file_from_minio_network_error(minio):
} }
# Act & Assert # Act & Assert
with raises(Exception, match='Error fetching file from MinIO'): with raises(OSError, match='Error fetching file from MinIO'):
await minio.fetch_file_from_minio(input_data) await minio.fetch_file_from_minio(input_data)
minio.send_notification.assert_called_once() minio.send_notification.assert_called_once()
@@ -242,7 +242,7 @@ async def test_delete_file_from_minio_access_denied(minio):
} }
# Act & Assert # Act & Assert
with raises(Exception, match='Error deleting file from MinIO'): with raises(OSError, match='Error deleting file from MinIO'):
await minio.delete_file_from_minio(input_data) await minio.delete_file_from_minio(input_data)
# Verify notification was sent # Verify notification was sent
@@ -265,7 +265,7 @@ async def test_delete_file_from_minio_network_error(minio):
} }
# Act & Assert # Act & Assert
with raises(Exception, match='Error deleting file from MinIO'): with raises(OSError, match='Error deleting file from MinIO'):
await minio.delete_file_from_minio(input_data) await minio.delete_file_from_minio(input_data)
minio.send_notification.assert_called_once() minio.send_notification.assert_called_once()