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
7 changes: 6 additions & 1 deletion redcap/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ def execute(

bad_request = False

if self.fmt == "json":
# empty json content is hardcoded, so check the real response instead
if self.config.return_empty_json:
bad_request = not response.ok or "<error>" in response.text.lower()
if bad_request:
content = response.text
elif self.fmt == "json":
try:
bad_request = "error" in content.keys() # type: ignore
except AttributeError:
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/callback_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ def handle_simple_project_file_request(**kwargs) -> Any:
data = kwargs["data"]
headers = kwargs["headers"]
resp = {}
if "bad_record" in str(data):
resp = (
'<?xml version="1.0" encoding="UTF-8" ?>'
"<hash><error>The record 'bad_record' does not exist</error></hash>"
)
return (400, headers, resp)
# file export
if " filename" not in str(data):
# name of the data file that was imported
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_simple_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ def test_file_import(simple_project):
simple_project.import_file("1", "file", upload_fname, fobj)


def test_file_import_raises_on_error(simple_project):
this_dir, _ = os.path.split(__file__)
upload_fname = os.path.join(this_dir, "data.txt")
with open(upload_fname, "r", encoding="UTF-8") as fobj:
with pytest.raises(RedcapError):
simple_project.import_file("bad_record", "file", upload_fname, fobj)


def test_file_delete_raises_on_error(simple_project):
with pytest.raises(RedcapError):
simple_project.delete_file("bad_record", "file")


def test_file_export(simple_project):
record, field = "1", "file"
content, headers = simple_project.export_file(record, field)
Expand Down
Loading