Skip to content

Commit 8ccdf70

Browse files
committed
only send error info in debug mode
also incorperates error messages into the VDOM schema itself
1 parent 15e2794 commit 8ccdf70

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

src/client/packages/idom-client-react/src/component.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@ export function Layout({ saveUpdateHook, sendEvent, loadImportSource }) {
1717

1818
React.useEffect(() => saveUpdateHook(patchModel), [patchModel]);
1919

20-
if (model.tagName) {
21-
return html`
22-
<${LayoutConfigContext.Provider} value=${{ sendEvent, loadImportSource }}>
23-
<${Element} model=${model} />
24-
<//>
25-
`;
26-
} else {
27-
return html`<div />`;
28-
}
20+
return html`
21+
<${LayoutConfigContext.Provider} value=${{ sendEvent, loadImportSource }}>
22+
<${Element} model=${model} />
23+
<//>
24+
`;
2925
}
3026

3127
export function Element({ model }) {
32-
if (model.importSource) {
28+
if (!model.tagName) {
29+
if (model.error) {
30+
return html`<pre>${model.error}</pre>`
31+
} else {
32+
return null
33+
}
34+
} else if (model.importSource) {
3335
return html`<${ImportedElement} model=${model} />`;
3436
} else {
3537
return html`<${StandardElement} model=${model} />`;

src/idom/core/layout.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,14 @@ def _render_component(
201201
self._render_model(old_state, new_state, raw_model)
202202
except Exception as error:
203203
logger.exception(f"Failed to render {component}")
204-
new_state.model.current = {"tagName": "__error__", "children": [str(error)]}
205-
204+
new_state.model.current = {
205+
"tagName": "",
206+
"error": (
207+
f"{type(error).__name__}: {error}"
208+
if IDOM_DEBUG_MODE.current
209+
else ""
210+
),
211+
}
206212
try:
207213
parent = new_state.parent
208214
except AttributeError:

src/idom/core/vdom.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,19 @@
4545
"properties": {
4646
"tagName": {"type": "string"},
4747
"key": {"type": "string"},
48+
"error": {"type": "string"},
4849
"children": {"$ref": "#/definitions/elementChildren"},
4950
"attributes": {"type": "object"},
5051
"eventHandlers": {"$ref": "#/definitions/elementEventHandlers"},
5152
"importSource": {"$ref": "#/definitions/importSource"},
5253
},
54+
# The 'tagName' is required because its presence is a useful indicator of
55+
# whether a dictionary describes a VDOM model or not.
5356
"required": ["tagName"],
57+
"dependentSchemas": {
58+
# When 'error' is given, the 'tagName' should be empty.
59+
"error": {"properties": {"tagName": {"maxLength": 0}}}
60+
},
5461
},
5562
"elementChildren": {
5663
"type": "array",
@@ -315,9 +322,9 @@ def _is_single_child(value: Any) -> bool:
315322

316323

317324
class _VdomDictOptional(TypedDict, total=False):
318-
key: str # noqa
319-
children: Sequence[Any] # noqa
320-
attributes: Dict[str, Any] # noqa
325+
key: str
326+
children: Sequence[Any]
327+
attributes: Dict[str, Any]
321328
eventHandlers: EventHandlerDict # noqa
322329
importSource: ImportSourceDict # noqa
323330

@@ -338,9 +345,10 @@ class ImportSourceDict(TypedDict):
338345

339346

340347
class _OptionalVdomJson(TypedDict, total=False):
341-
key: str # noqa
342-
children: List[Any] # noqa
343-
attributes: Dict[str, Any] # noqa
348+
key: str
349+
error: str
350+
children: List[Any]
351+
attributes: Dict[str, Any]
344352
eventHandlers: Dict[str, _JsonEventTarget] # noqa
345353
importSource: _JsonImportSource # noqa
346354

test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import idom
2+
3+
4+
@idom.component
5+
def Test():
6+
raise ValueError("test message")
7+
8+
9+
idom.run(Test, port=8000)

tests/test_core/test_layout.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ def BadChild():
134134
"path": "/children",
135135
"value": [
136136
{"tagName": "div", "children": ["hello"]},
137-
{
138-
"tagName": "__error__",
139-
"children": ["Something went wrong :("],
140-
},
137+
{"tagName": "", "error": "ValueError: Something went wrong :("},
141138
{"tagName": "div", "children": ["hello"]},
142139
],
143140
},

0 commit comments

Comments
 (0)