-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from ral-facilities/DSEGOG-271-Handling-Faile…
…d-Ingestion Dsegog 271 handling failed ingestion
- Loading branch information
Showing
9 changed files
with
558 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from functools import wraps | ||
from inspect import iscoroutinefunction | ||
import logging | ||
|
||
from operationsgateway_api.src.exceptions import DatabaseError | ||
|
||
log = logging.getLogger() | ||
|
||
|
||
def mongodb_error_handling(operation: str): | ||
""" | ||
Decorator for consistent error handling in MongoDB operations, supporting both | ||
sync and async functions. | ||
""" | ||
|
||
def decorator(func): | ||
if iscoroutinefunction(func): # Check if the function is async | ||
|
||
@wraps(func) | ||
async def async_wrapper(*args, **kwargs): | ||
try: | ||
return await func(*args, **kwargs) | ||
except DatabaseError: | ||
raise # If it's already a DatabaseError, propagate it as-is | ||
except Exception as exc: | ||
log.error("Database operation: %s failed", operation) | ||
raise DatabaseError( | ||
f"Database operation failed during {operation}", | ||
) from exc | ||
|
||
return async_wrapper | ||
else: # Handle synchronous functions | ||
|
||
@wraps(func) | ||
def sync_wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except DatabaseError: | ||
raise # If it's already a DatabaseError, propagate it as-is | ||
except Exception as exc: | ||
log.error("Database operation: %s failed", operation) | ||
raise DatabaseError( | ||
f"Database operation failed during {operation}", | ||
) from exc | ||
|
||
return sync_wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.