Skip to content

Commit a97ae2c

Browse files
committed
add defensive logic for MessageAIO callback handling
1 parent f96ed32 commit a97ae2c

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

crystal_toolkit/components/messageAIO.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@
3333

3434
from __future__ import annotations
3535

36-
from dash import MATCH, Input, Output, State, callback, dcc, html, no_update
36+
import logging
37+
38+
from dash import MATCH, Input, Output, State, callback, ctx, dcc, html, no_update
3739

3840
from crystal_toolkit.core.mpcomponent import MPComponent
3941

42+
logger = logging.getLogger(__name__)
43+
4044
# Bulma-inspired color scheme for notification types
4145
_TYPE_COLORS = {
4246
"info": {
@@ -294,24 +298,39 @@ def layout(self) -> html.Div:
294298
Output(ids.message(MATCH), "children"),
295299
Output(ids.wrapper(MATCH), "style", allow_duplicate=True),
296300
Output(ids.div(MATCH), "style"),
297-
Output(ids.close_button(MATCH), "n_clicks"),
298301
Input(ids.data(MATCH), "data"),
299302
Input(ids.close_button(MATCH), "n_clicks"),
300303
State(ids.div(MATCH), "style"),
301304
prevent_initial_call=True,
302305
)
303306
def update_messages(input_data, close_clicks, cur_style):
304-
if close_clicks or not input_data:
305-
return no_update, {"display": "none"}, cur_style, 0
307+
if not isinstance(input_data, dict):
308+
raise ValueError("`input_data` must be a dictionary for MessageAIO")
309+
310+
if (
311+
isinstance(ctx.triggered_id, dict)
312+
and ctx.triggered_id.get("subcomponents") == "close_button"
313+
):
314+
return no_update, {"display": "none"}, cur_style
315+
316+
if not input_data:
317+
return no_update, {"display": "none"}, cur_style
306318

307319
message = input_data.get("message", None)
308-
msg_type = input_data.get("msg_type", "info")
320+
msg_type = input_data.get("msg_type", None)
321+
322+
if not message:
323+
raise ValueError("`message` field is required for MessageAIO")
324+
325+
if not msg_type:
326+
logger.warning("No `msg_type`. Falling back to 'info'")
327+
msg_type = "info"
309328

310329
# Resolve type colors
311-
type_style = _TYPE_COLORS.get(msg_type, _TYPE_COLORS["info"])
330+
type_style = _TYPE_COLORS.get(msg_type, {})
312331
cur_style.update(type_style)
313332

314-
return message, {"display": "block"}, cur_style, 1
333+
return message, {"display": "block"}, cur_style
315334

316335
"""
317336
@callback(

0 commit comments

Comments
 (0)