Skip to content

Commit 20405f6

Browse files
better exception handling
1 parent 50fd471 commit 20405f6

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

mfr/extensions/tabular/libs/stdlib_tools.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from http import HTTPStatus
44

55
from mfr.extensions.tabular import utilities
6-
from mfr.extensions.tabular.exceptions import EmptyTableError, TabularRendererError
6+
from mfr.extensions.tabular.exceptions import (EmptyTableError,
7+
TabularRendererError)
78

89

910
def csv_stdlib(fp):
@@ -73,6 +74,14 @@ def parse_stdlib(reader):
7374
code=HTTPStatus.BAD_REQUEST,
7475
extension='csv') from e
7576

77+
# Outside other except because the `if any` line causes more errors to be raised
78+
# on certain exceptions
79+
except UnicodeDecodeError as e:
80+
raise TabularRendererError('Cannot render file as csv/tsv. '
81+
'The file may be empty or corrupt',
82+
code=HTTPStatus.BAD_REQUEST,
83+
extension='csv') from e
84+
7685
if not columns and not rows:
7786
raise EmptyTableError('Cannot render file as csv/tsv. '
7887
'The file may be empty or corrupt',
9.76 KB
Binary file not shown.

tests/extensions/tabular/test_stdlib_tools.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import pytest
55

66
from mfr.extensions.tabular.libs import stdlib_tools
7-
from mfr.extensions.tabular.exceptions import EmptyTableError
8-
7+
from mfr.extensions.tabular.exceptions import(EmptyTableError,
8+
TabularRendererError)
99

1010
BASE = os.path.dirname(os.path.abspath(__file__))
1111

@@ -49,3 +49,9 @@ def test_csv_stdlib_exception_raises(self):
4949
with pytest.raises(EmptyTableError) as e:
5050
stdlib_tools.tsv_stdlib(fp)
5151
assert e.value.code == 400
52+
53+
def test_csv_stdlib_other_exception_raises(self):
54+
with open(os.path.join(BASE, 'files', 'invalid_null.csv')) as fp:
55+
with pytest.raises(TabularRendererError) as e:
56+
stdlib_tools.tsv_stdlib(fp)
57+
assert e.value.code == 400

0 commit comments

Comments
 (0)