Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add InvalidArgumentsError
* Avoid confusion with random type errors in methods.
  • Loading branch information
st31ny committed Oct 27, 2019
commit 0fc363d913d5c357dd26a2d699102825af38b4fa
7 changes: 4 additions & 3 deletions jsonrpcserver/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
)
from .errors import (
MethodNotFoundError,
InvalidArgumentsError,
ApiError,
)

Expand Down Expand Up @@ -131,9 +132,9 @@ def handle_exceptions(request: Request, debug: bool) -> Generator:
handler.response = MethodNotFoundResponse(
id=request.id, data=request.method, debug=debug
)
except (TypeError, AssertionError) as exc:
# Invalid Params - TypeError is raised by jsonschema, AssertionError raised
# inside the methods.
except (InvalidArgumentsError, AssertionError) as exc:
# Invalid Params - InvalidArgumentsError is raised by validate_args,
# AssertionError raised inside the methods.
handler.response = InvalidParamsResponse(
id=request.id, data=str(exc), debug=debug
)
Expand Down
4 changes: 4 additions & 0 deletions jsonrpcserver/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class MethodNotFoundError(KeyError):
""" Method lookup failed """
pass

class InvalidArgumentsError(TypeError):
""" Method arguments invalid """
pass

class ApiError(RuntimeError):
""" A method responds with a custom error """

Expand Down
9 changes: 6 additions & 3 deletions jsonrpcserver/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from inspect import signature

from .errors import MethodNotFoundError
from .errors import MethodNotFoundError, InvalidArgumentsError

Method = Callable[..., Any]

Expand All @@ -27,9 +27,12 @@ def validate_args(func: Method, *args: Any, **kwargs: Any) -> Method:
kwargs: Keyword arguments.

Raises:
TypeError: If the arguments cannot be passed to the function.
InvalidArgumentsError: If the arguments cannot be passed to the function.
"""
signature(func).bind(*args, **kwargs)
try:
signature(func).bind(*args, **kwargs)
except TypeError as exc:
raise InvalidArgumentsError from exc
return func


Expand Down