Skip to content

Commit fd0a603

Browse files
authored
HDX-10885 HDXErrorHandler in HDX Python API should check ERR_TO_HDX env var and accept various types (#85)
1 parent ea53e9d commit fd0a603

3 files changed

Lines changed: 48 additions & 12 deletions

File tree

requirements.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ charset-normalizer==3.4.2
2626
# via requests
2727
ckanapi==4.8
2828
# via hdx-python-api (pyproject.toml)
29-
click==8.1.8
29+
click==8.2.1
3030
# via
3131
# mkdocs
3232
# typer
3333
colorama==0.4.6
3434
# via mkdocs-material
35-
coverage==7.8.1
35+
coverage==7.8.2
3636
# via pytest-cov
3737
defopt==6.4.0
3838
# via hdx-python-api (pyproject.toml)
@@ -72,7 +72,7 @@ hdx-python-utilities==3.8.7
7272
# hdx-python-country
7373
humanize==4.12.3
7474
# via frictionless
75-
identify==2.6.10
75+
identify==2.6.12
7676
# via pre-commit
7777
idna==3.10
7878
# via
@@ -96,7 +96,7 @@ jsonlines==4.0.0
9696
# via hdx-python-utilities
9797
jsonpath-ng==1.7.0
9898
# via libhxl
99-
jsonschema==4.23.0
99+
jsonschema==4.24.0
100100
# via
101101
# frictionless
102102
# tableschema-to-template
@@ -129,7 +129,7 @@ mergedeep==1.3.4
129129
# via
130130
# mkdocs
131131
# mkdocs-get-deps
132-
mkapi==4.4.0
132+
mkapi==4.4.1
133133
# via hdx-python-api (pyproject.toml)
134134
mkdocs==1.6.1
135135
# via
@@ -256,9 +256,11 @@ rpds-py==0.25.1
256256
# referencing
257257
rsa==4.9.1
258258
# via google-auth
259-
ruamel-yaml==0.18.10
259+
ruamel-yaml==0.18.11
260260
# via hdx-python-utilities
261-
setuptools==80.8.0
261+
ruamel-yaml-clib==0.2.12
262+
# via ruamel-yaml
263+
setuptools==80.9.0
262264
# via ckanapi
263265
shellingham==1.5.4
264266
# via typer
@@ -286,7 +288,7 @@ text-unidecode==1.3
286288
# via python-slugify
287289
typeguard==4.4.2
288290
# via inflect
289-
typer==0.15.4
291+
typer==0.16.0
290292
# via frictionless
291293
typing-extensions==4.13.2
292294
# via

src/hdx/api/utilities/hdx_error_handler.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from os import getenv
23
from typing import Any, Tuple
34

45
from hdx.data.dataset import Dataset
@@ -19,17 +20,24 @@ class HDXErrorHandler(ErrorHandler):
1920
2021
Args:
2122
should_exit_on_error (bool): Whether to exit with a 1 code if there are errors. Default is False.
22-
write_to_hdx (bool): Whether to write errors to HDX resources. Default is False.
23+
write_to_hdx (Any): Whether to write errors to HDX resources. Default is None (write errors).
2324
2425
"""
2526

2627
def __init__(
2728
self,
2829
should_exit_on_error: bool = False,
29-
write_to_hdx: bool = False,
30+
write_to_hdx: Any = None,
3031
):
3132
super().__init__(should_exit_on_error)
32-
self._write_to_hdx = write_to_hdx
33+
if write_to_hdx is None:
34+
write_to_hdx = getenv("ERR_TO_HDX", True)
35+
if write_to_hdx in (False, 0, "false", "False", "FALSE", "N", "n", ""):
36+
self._write_to_hdx = False
37+
logger.info("Errors won't be written to HDX")
38+
else:
39+
self._write_to_hdx = True
40+
logger.info("Errors will be written to HDX")
3341
self.shared_errors["hdx_error"] = {}
3442

3543
@staticmethod

tests/hdx/api/utilities/test_hdx_error_handler.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,35 @@
1212

1313
class TestHDXErrorHandler:
1414
def test_hdx_error_handler(self, caplog):
15+
error_handler = HDXErrorHandler()
16+
assert error_handler._write_to_hdx is True
17+
error_handler = HDXErrorHandler(write_to_hdx=None)
18+
assert error_handler._write_to_hdx is True
19+
error_handler = HDXErrorHandler(write_to_hdx="true")
20+
assert error_handler._write_to_hdx is True
21+
error_handler = HDXErrorHandler(write_to_hdx="Y")
22+
assert error_handler._write_to_hdx is True
23+
error_handler = HDXErrorHandler(write_to_hdx=True)
24+
assert error_handler._write_to_hdx is True
25+
error_handler = HDXErrorHandler(write_to_hdx=1)
26+
assert error_handler._write_to_hdx is True
27+
error_handler = HDXErrorHandler(write_to_hdx="")
28+
assert error_handler._write_to_hdx is False
29+
error_handler = HDXErrorHandler(write_to_hdx="false")
30+
assert error_handler._write_to_hdx is False
31+
error_handler = HDXErrorHandler(write_to_hdx="FALSE")
32+
assert error_handler._write_to_hdx is False
33+
error_handler = HDXErrorHandler(write_to_hdx="n")
34+
assert error_handler._write_to_hdx is False
35+
error_handler = HDXErrorHandler(write_to_hdx=False)
36+
assert error_handler._write_to_hdx is False
37+
error_handler = HDXErrorHandler(write_to_hdx=0)
38+
assert error_handler._write_to_hdx is False
1539
with pytest.raises(SystemExit):
1640
with caplog.at_level(logging.ERROR):
17-
with HDXErrorHandler(should_exit_on_error=True) as errors:
41+
with HDXErrorHandler(
42+
should_exit_on_error=True, write_to_hdx=False
43+
) as errors:
1844
errors.add_message(
1945
"pipeline1",
2046
"dataset1",

0 commit comments

Comments
 (0)