-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Update error handling to log with warnings.warn()
#442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,31 @@ | ||
| import asyncio | ||
| import functools | ||
| import logging | ||
| import warnings | ||
| from logging import Logger | ||
| from typing import Any, Callable | ||
|
|
||
|
|
||
| def warn_catch_exception(logger: Logger = logging.getLogger(__name__), exception: type[Exception] = Exception) -> Any: | ||
| """ | ||
| A function wrapper that catches exceptions and logs them to the logger. | ||
| A function wrapper that catches exceptions, logs them, and emits visible warnings. | ||
|
|
||
| This decorator ensures that observability code doesn't crash the user's application | ||
| while still providing visible feedback when errors occur. | ||
|
|
||
| Errors are: | ||
| 1. Logged via standard Python logging (integrates with logging/tracing platforms) | ||
| 2. Emitted as warnings (visible by default without any logging configuration) | ||
|
|
||
| Parameters | ||
| ---------- | ||
| exception | ||
| logger | ||
| exception : type[Exception] | ||
| The base exception type to catch. Defaults to Exception. | ||
|
|
||
| Returns | ||
| ------- | ||
| Callable | ||
| The decorated function. | ||
| """ | ||
|
|
||
| def wrapper(f: Callable) -> Callable: | ||
|
|
@@ -25,7 +34,10 @@ def inner(*args: Any, **kwargs: Any) -> Any: | |
| try: | ||
| result = f(*args, **kwargs) | ||
| except exception as err: | ||
| # Standard logging for integration with logging/tracing platforms | ||
| logger.warning(f"Error occurred during execution: {f.__name__}: {err}") | ||
| # Warning for immediate visibility without config | ||
| warnings.warn(f"Galileo: {f.__name__} failed: {err}", RuntimeWarning, stacklevel=2) | ||
| else: | ||
| return result | ||
|
|
||
|
|
@@ -38,16 +50,26 @@ def async_warn_catch_exception( | |
| logger: Logger = logging.getLogger(__name__), exception: type[Exception] = Exception | ||
| ) -> Any: | ||
| """ | ||
| A function wrapper that catches exceptions and logs them to the logger. | ||
| An async function wrapper that catches exceptions, logs them, and emits visible warnings. | ||
|
|
||
| This decorator ensures that observability code doesn't crash the user's application | ||
| while still providing visible feedback when errors occur. | ||
|
|
||
| Errors are: | ||
| 1. Logged via standard Python logging (integrates with logging/tracing platforms) | ||
| 2. Emitted as warnings (visible by default without any logging configuration) | ||
|
|
||
| Parameters | ||
| ---------- | ||
| exception | ||
| logger | ||
| logger : Logger | ||
| The logger to use for logging errors. Defaults to the module logger. | ||
| exception : type[Exception] | ||
| The base exception type to catch. Defaults to Exception. | ||
|
|
||
| Returns | ||
| ------- | ||
| Callable | ||
| The decorated function. | ||
| """ | ||
|
|
||
| def wrapper(f: Callable) -> Callable: | ||
|
|
@@ -56,7 +78,10 @@ async def inner(*args: Any, **kwargs: Any) -> Any: | |
| try: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ Failed check: pre-commit / pre-commit Finding type: |
||
| result = await f(*args, **kwargs) | ||
| except exception as err: | ||
| # Standard logging for integration with logging/tracing platforms | ||
| logger.warning(f"Error occurred during execution: {f.__name__}: {err}") | ||
| # Warning for immediate visibility without config | ||
| warnings.warn(f"Galileo: {f.__name__} failed: {err}", RuntimeWarning, stacklevel=2) | ||
| else: | ||
| return result | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing support for the standard
loggerinterface is not the correct approach.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I added this back in addition to the
warnings.warn()but sounds like we'll want to think about this a bit more deeply