Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit df2d19b

Browse filesBrowse files
authored
Merge pull request #40 from otovo/espenra/fix-renderer-error-handling
bugfix: fix _render_node's error handling
2 parents f8ff2f2 + 058b755 commit df2d19b
Copy full SHA for df2d19b

File tree

Expand file treeCollapse file tree

5 files changed

+48
-4
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+48
-4
lines changed

‎.pre-commit-config.yaml

Copy file name to clipboardExpand all lines: .pre-commit-config.yaml
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/ambv/black
3-
rev: 21.12b0
3+
rev: 22.3.0
44
hooks:
55
- id: black
66
args: [ "--quiet" ]
@@ -19,7 +19,7 @@ repos:
1919
- id: mixed-line-ending
2020
- id: trailing-whitespace
2121
- repo: https://gitlab.com/pycqa/flake8
22-
rev: 3.9.2
22+
rev: 4.0.1
2323
hooks:
2424
- id: flake8
2525
additional_dependencies: [
@@ -34,6 +34,9 @@ repos:
3434
'flake8-printf-formatting',
3535
'flake8-type-checking',
3636
]
37+
args: [
38+
'--allow-star-arg-any'
39+
]
3740
- repo: https://github.com/asottile/pyupgrade
3841
rev: v2.31.0
3942
hooks:

‎portabletext_html/renderer.py

Copy file name to clipboardExpand all lines: portabletext_html/renderer.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _render_node(self, node: dict, context: Optional[Block] = None, list_item: b
113113
return self._custom_serializers.get(node.get('_type', ''))(node, context, list_item) # type: ignore
114114

115115
else:
116-
if hasattr(node, '_type'):
116+
if '_type' in node:
117117
raise MissingSerializerError(
118118
f'Found unhandled node type: {node["_type"]}. ' 'Most likely this requires a custom serializer.'
119119
)

‎tests/fixtures/invalid_node.json

Copy file name to clipboard
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"_key": "73405dda68e7",
3+
"children": [
4+
{
5+
"_key": "25a09c61d80a",
6+
"_type": "span",
7+
"marks": [],
8+
"text": "Otovo guarantee is good"
9+
}
10+
],
11+
"markDefs": [],
12+
"style": "normal"
13+
}

‎tests/fixtures/invalid_type.json

Copy file name to clipboard
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"_key": "73405dda68e7",
3+
"_type": "invalid_type",
4+
"children": [
5+
{
6+
"_key": "25a09c61d80a",
7+
"_type": "span",
8+
"marks": [],
9+
"text": "Otovo guarantee is good"
10+
}
11+
],
12+
"markDefs": [],
13+
"style": "normal"
14+
}

‎tests/test_rendering.py

Copy file name to clipboardExpand all lines: tests/test_rendering.py
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import json
33
from pathlib import Path
44

5-
from portabletext_html.renderer import render
5+
import pytest
6+
7+
from portabletext_html.renderer import MissingSerializerError, UnhandledNodeError, render
68

79

810
def load_fixture(fixture_name) -> dict:
@@ -45,3 +47,15 @@ def test_nested_marks():
4547
fixture = load_fixture('nested_marks.json')
4648
output = render(fixture)
4749
assert output == '<p><strong>A word of <em>warning;</em></strong> Sanity is addictive.</p>'
50+
51+
52+
def test_missing_serializer():
53+
fixture = load_fixture('invalid_type.json')
54+
with pytest.raises(MissingSerializerError):
55+
render(fixture)
56+
57+
58+
def test_invalid_node():
59+
fixture = load_fixture('invalid_node.json')
60+
with pytest.raises(UnhandledNodeError):
61+
render(fixture)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.