Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ charset-normalizer==3.4.2
# via requests
ckanapi==4.8
# via hdx-python-api (pyproject.toml)
click==8.1.8
click==8.2.1
# via
# mkdocs
# typer
colorama==0.4.6
# via mkdocs-material
coverage==7.8.1
coverage==7.8.2
# via pytest-cov
defopt==6.4.0
# via hdx-python-api (pyproject.toml)
Expand Down Expand Up @@ -72,7 +72,7 @@ hdx-python-utilities==3.8.7
# hdx-python-country
humanize==4.12.3
# via frictionless
identify==2.6.10
identify==2.6.12
# via pre-commit
idna==3.10
# via
Expand All @@ -96,7 +96,7 @@ jsonlines==4.0.0
# via hdx-python-utilities
jsonpath-ng==1.7.0
# via libhxl
jsonschema==4.23.0
jsonschema==4.24.0
# via
# frictionless
# tableschema-to-template
Expand Down Expand Up @@ -129,7 +129,7 @@ mergedeep==1.3.4
# via
# mkdocs
# mkdocs-get-deps
mkapi==4.4.0
mkapi==4.4.1
# via hdx-python-api (pyproject.toml)
mkdocs==1.6.1
# via
Expand Down Expand Up @@ -256,9 +256,11 @@ rpds-py==0.25.1
# referencing
rsa==4.9.1
# via google-auth
ruamel-yaml==0.18.10
ruamel-yaml==0.18.11
# via hdx-python-utilities
setuptools==80.8.0
ruamel-yaml-clib==0.2.12
# via ruamel-yaml
setuptools==80.9.0
# via ckanapi
shellingham==1.5.4
# via typer
Expand Down Expand Up @@ -286,7 +288,7 @@ text-unidecode==1.3
# via python-slugify
typeguard==4.4.2
# via inflect
typer==0.15.4
typer==0.16.0
# via frictionless
typing-extensions==4.13.2
# via
Expand Down
14 changes: 11 additions & 3 deletions src/hdx/api/utilities/hdx_error_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from os import getenv
from typing import Any, Tuple

from hdx.data.dataset import Dataset
Expand All @@ -19,17 +20,24 @@ class HDXErrorHandler(ErrorHandler):

Args:
should_exit_on_error (bool): Whether to exit with a 1 code if there are errors. Default is False.
write_to_hdx (bool): Whether to write errors to HDX resources. Default is False.
write_to_hdx (Any): Whether to write errors to HDX resources. Default is None (write errors).

"""

def __init__(
self,
should_exit_on_error: bool = False,
write_to_hdx: bool = False,
write_to_hdx: Any = None,
):
super().__init__(should_exit_on_error)
self._write_to_hdx = write_to_hdx
if write_to_hdx is None:
write_to_hdx = getenv("ERR_TO_HDX", True)
if write_to_hdx in (False, 0, "false", "False", "FALSE", "N", "n", ""):
self._write_to_hdx = False
logger.info("Errors won't be written to HDX")
else:
self._write_to_hdx = True
logger.info("Errors will be written to HDX")
self.shared_errors["hdx_error"] = {}

@staticmethod
Expand Down
28 changes: 27 additions & 1 deletion tests/hdx/api/utilities/test_hdx_error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,35 @@

class TestHDXErrorHandler:
def test_hdx_error_handler(self, caplog):
error_handler = HDXErrorHandler()
assert error_handler._write_to_hdx is True
error_handler = HDXErrorHandler(write_to_hdx=None)
assert error_handler._write_to_hdx is True
error_handler = HDXErrorHandler(write_to_hdx="true")
assert error_handler._write_to_hdx is True
error_handler = HDXErrorHandler(write_to_hdx="Y")
assert error_handler._write_to_hdx is True
error_handler = HDXErrorHandler(write_to_hdx=True)
assert error_handler._write_to_hdx is True
error_handler = HDXErrorHandler(write_to_hdx=1)
assert error_handler._write_to_hdx is True
error_handler = HDXErrorHandler(write_to_hdx="")
assert error_handler._write_to_hdx is False
error_handler = HDXErrorHandler(write_to_hdx="false")
assert error_handler._write_to_hdx is False
error_handler = HDXErrorHandler(write_to_hdx="FALSE")
assert error_handler._write_to_hdx is False
error_handler = HDXErrorHandler(write_to_hdx="n")
assert error_handler._write_to_hdx is False
error_handler = HDXErrorHandler(write_to_hdx=False)
assert error_handler._write_to_hdx is False
error_handler = HDXErrorHandler(write_to_hdx=0)
assert error_handler._write_to_hdx is False
with pytest.raises(SystemExit):
with caplog.at_level(logging.ERROR):
with HDXErrorHandler(should_exit_on_error=True) as errors:
with HDXErrorHandler(
should_exit_on_error=True, write_to_hdx=False
) as errors:
errors.add_message(
"pipeline1",
"dataset1",
Expand Down
Loading