Skip to content

Commit

Permalink
is it a lambda arg thing?
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Dec 6, 2024
1 parent 7cfd7ca commit 708a3e8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rest_tools/server/arghandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _translate_error(
captured_stderr: str,
) -> str:
"""Translate argparse-style error to a message str for HTTPError."""
LOGGER.error(f"Intercepted error to translate for requestor: {exc}")
LOGGER.error(f"Intercepted error to translate for requestor -> {exc}")

# errors not covered by 'exit_on_error=False' (in __init__)
if isinstance(exc, (SystemExit, argparse.ArgumentError)):
Expand Down
44 changes: 43 additions & 1 deletion tests/unit_server/arghandler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,49 @@ def _error_it(_: Any, exc: Exception):
"argument_source",
[QUERY_ARGUMENTS, JSON_BODY_ARGUMENTS],
)
def test_235__argparse_custom_validation__argumenttypeerror__error(
def test_240__argparse_custom_validation__argumenttypeerror__error(
argument_source: str,
) -> None:
"""Test `argument_source` arguments using argparse's advanced options."""
args: Dict[str, Any] = {
"foo": "True",
}
if argument_source == JSON_BODY_ARGUMENTS:
args = {
"foo": [1, 2, 3],
}

# set up ArgumentHandler
arghand = setup_argument_handler(
argument_source,
args,
)

def _error_it(_: Any):
raise argparse.ArgumentTypeError("it's a bad value and you *will* see this!")

for arg, _ in args.items():
print()
print(arg)
arghand.add_argument(
arg,
type=lambda x: _error_it(x),
)

with pytest.raises(tornado.web.HTTPError) as e:
arghand.parse_args()

assert (
str(e.value)
== "HTTP 400: argument foo: it's a bad value and you *will* see this!"
)


@pytest.mark.parametrize(
"argument_source",
[QUERY_ARGUMENTS, JSON_BODY_ARGUMENTS],
)
def test_241__argparse_custom_validation__argumenttypeerror__error(
argument_source: str,
) -> None:
"""Test `argument_source` arguments using argparse's advanced options."""
Expand Down

0 comments on commit 708a3e8

Please sign in to comment.