Skip to content

Commit 94d5791

Browse files
committed
Handling minssing or empty TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID
1 parent 51c1e72 commit 94d5791

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

src/eruption_forecast/decorators/notify.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,8 @@ def notify(
311311
and returns a list of paths. Defaults to None.
312312
313313
Returns:
314-
Callable: Decorator function that wraps the target function.
315-
316-
Raises:
317-
ValueError: If ``bot_token`` or ``chat_id`` cannot be resolved from
318-
arguments or environment variables at decoration time.
319-
ValueError: If ``bot_token`` or ``chat_id`` contains whitespace.
314+
Callable: Decorator function that wraps the target function, or a
315+
no-op decorator if credentials are missing or invalid.
320316
321317
Examples:
322318
>>> @notify(bot_token="TOKEN", chat_id="123", name="Training")
@@ -337,22 +333,30 @@ def notify(
337333
resolved_chat: str | int = chat_id or os.environ.get("TELEGRAM_CHAT_ID", "")
338334

339335
if not resolved_token:
340-
raise ValueError(
341-
"notify: bot_token not provided and TELEGRAM_BOT_TOKEN not set in environment."
336+
logger.warning(
337+
"notify: bot_token not provided and TELEGRAM_BOT_TOKEN not set in environment. "
338+
"Notifications disabled."
342339
)
340+
return lambda func: func
343341
if any(c.isspace() for c in resolved_token):
344-
raise ValueError(
342+
logger.warning(
345343
f"notify: bot_token contains whitespace — got {resolved_token!r}. "
346-
"Ensure TELEGRAM_BOT_TOKEN is set to the raw token from BotFather."
344+
"Ensure TELEGRAM_BOT_TOKEN is set to the raw token from BotFather. "
345+
"Notifications disabled."
347346
)
347+
return lambda func: func
348348
if not resolved_chat:
349-
raise ValueError(
350-
"notify: chat_id not provided and TELEGRAM_CHAT_ID not set in environment."
349+
logger.warning(
350+
"notify: chat_id not provided and TELEGRAM_CHAT_ID not set in environment. "
351+
"Notifications disabled."
351352
)
353+
return lambda func: func
352354
if isinstance(resolved_chat, str) and any(c.isspace() for c in resolved_chat):
353-
raise ValueError(
354-
f"notify: chat_id contains whitespace — got {resolved_chat!r}."
355+
logger.warning(
356+
f"notify: chat_id contains whitespace — got {resolved_chat!r}. "
357+
"Notifications disabled."
355358
)
359+
return lambda func: func
356360

357361
hostname = socket.gethostname()
358362

0 commit comments

Comments
 (0)