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
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,15 @@ which can be found in ``pjrpc.common.exceptions`` module so that error handling


Default error list can be easily extended. All you need to create an error class inherited from
``pjrpc.exc.TypedError`` and define an error code and a description message. ``pjrpc`` will be automatically
``pjrpc.client.exceptions.TypedError`` and define an error code and a description message. ``pjrpc`` will be automatically
deserializing custom errors for you:

.. code-block:: python

import pjrpc
from pjrpc.client.backend import requests as pjrpc_client

class UserNotFound(pjrpc.exc.TypedError):
class UserNotFound(pjrpc.client.exceptions.TypedError):
CODE = 1
MESSAGE = 'user not found'

Expand Down Expand Up @@ -395,7 +395,7 @@ On the server side everything is also pretty straightforward:
methods = pjrpc.server.MethodRegistry()


class UserNotFound(pjrpc.exc.TypedError):
class UserNotFound(pjrpc.server.exceptions.TypedError):
CODE = 1
MESSAGE = 'user not found'

Expand Down Expand Up @@ -513,7 +513,7 @@ and Swagger UI web tool with basic auth:
id: UserId


class AlreadyExistsError(pjrpc.exc.TypedError):
class AlreadyExistsError(pjrpc.server.exceptions.TypedError):
"""
User already registered error.
"""
Expand All @@ -522,7 +522,7 @@ and Swagger UI web tool with basic auth:
MESSAGE = "user already exists"


class NotFoundError(pjrpc.exc.TypedError):
class NotFoundError(pjrpc.server.exceptions.TypedError):
"""
User not found error.
"""
Expand Down
14 changes: 7 additions & 7 deletions docs/source/pjrpc/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Errors handling
-32000 to -32099 , Server error , Reserved for implementation-defined server-errors.


Errors can be found in :py:mod:`pjrpc.common.exceptions` module. Having said that error handling
Errors can be found in :py:mod:`pjrpc.client.exceptions` module. Having said that error handling
is very simple and "pythonic-way":

.. code-block:: python
Expand All @@ -34,23 +34,23 @@ is very simple and "pythonic-way":

try:
result = client.proxy.sum(1, 2)
except pjrpc.MethodNotFound as e:
except pjrpc.client.exceptions.MethodNotFound as e:
print(e)


Custom errors
-------------

Default error list can be easily extended. All you need to create an error class inherited from
:py:class:`pjrpc.common.exceptions.TypedError` and define an error code and a description message. ``pjrpc``
:py:class:`pjrpc.client.exceptions.TypedError` and define an error code and a description message. ``pjrpc``
will be automatically deserializing custom errors for you:

.. code-block:: python

import pjrpc
from pjrpc.client.backend import requests as pjrpc_client

class UserNotFound(pjrpc.exc.TypedError):
class UserNotFound(pjrpc.client.exceptions.TypedError):
CODE = 1
MESSAGE = 'user not found'

Expand Down Expand Up @@ -81,7 +81,7 @@ On the server side everything is also pretty straightforward:
methods = pjrpc.server.MethodRegistry()


class UserNotFound(pjrpc.exc.TypedError):
class UserNotFound(pjrpc.server.exceptions.TypedError):
CODE = 1
MESSAGE = 'user not found'

Expand Down Expand Up @@ -123,7 +123,7 @@ to set a base error class for a particular client:
from pjrpc.client.backend import requests as jrpc_client


class ErrorV1(pjrpc.exc.TypeError, base=True):
class ErrorV1(pjrpc.client.exceptions.TypeError, base=True):
pass


Expand All @@ -132,7 +132,7 @@ to set a base error class for a particular client:
MESSAGE = 'permission denied'


class ErrorV2(pjrpc.exc.TypeError, base=True):
class ErrorV2(pjrpc.client.exceptions.TypeError, base=True):
pass


Expand Down
12 changes: 6 additions & 6 deletions docs/source/pjrpc/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Error handling
______________

``pjrpc`` implements all the errors listed in `protocol specification <https://www.jsonrpc.org/specification#error_object>`_
which can be found in :py:mod:`pjrpc.common.exceptions` module so that error handling is very simple and "pythonic-way":
which can be found in :py:mod:`pjrpc.client.exceptions` module so that error handling is very simple and "pythonic-way":

.. code-block:: python

Expand All @@ -294,15 +294,15 @@ which can be found in :py:mod:`pjrpc.common.exceptions` module so that error han


Default error list can be easily extended. All you need to create an error class inherited from
:py:class:`pjrpc.common.exceptions.JsonRpcError` and define an error code and a description message. ``pjrpc``
:py:class:`pjrpc.client.exceptions.TypedError` and define an error code and a description message. ``pjrpc``
will be automatically deserializing custom errors for you:

.. code-block:: python

import pjrpc
from pjrpc.client.backend import requests as pjrpc_client

class UserNotFound(pjrpc.exc.TypedError):
class UserNotFound(pjrpc.client.exceptions.TypedError):
CODE = 1
MESSAGE = 'user not found'

Expand Down Expand Up @@ -330,7 +330,7 @@ On the server side everything is also pretty straightforward:
methods = pjrpc.server.MethodRegistry()


class UserNotFound(pjrpc.exc.TypedError):
class UserNotFound(pjrpc.server.exceptions.TypedError):
CODE = 1
MESSAGE = 'user not found'

Expand Down Expand Up @@ -448,7 +448,7 @@ and Swagger UI web tool with basic auth:
id: UserId


class AlreadyExistsError(pjrpc.exc.TypedError):
class AlreadyExistsError(pjrpc.server.exceptions.TypedError):
"""
User already registered error.
"""
Expand All @@ -457,7 +457,7 @@ and Swagger UI web tool with basic auth:
MESSAGE = "user already exists"


class NotFoundError(pjrpc.exc.TypedError):
class NotFoundError(pjrpc.server.exceptions.TypedError):
"""
User not found error.
"""
Expand Down
6 changes: 3 additions & 3 deletions docs/source/pjrpc/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ Look at the following test example:
assert result == 2

pjrpc_aiohttp_mocker.replace(
'http://localhost/api/v1', 'sum', error=pjrpc.exc.JsonRpcError(code=1, message='error', data='oops'),
'http://localhost/api/v1', 'sum', error=pjrpc.client.exceptions.JsonRpcError(code=1, message='error', data='oops'),
)
with pytest.raises(pjrpc.exc.JsonRpcError) as exc_info:
with pytest.raises(pjrpc.client.exceptions.JsonRpcError) as exc_info:
await client.proxy.sum(a=1, b=1)

assert exc_info.type is pjrpc.exc.JsonRpcError
assert exc_info.type is pjrpc.client.exceptions.JsonRpcError
assert exc_info.value.code == 1
assert exc_info.value.message == 'error'
assert exc_info.value.data == 'oops'
Expand Down
4 changes: 2 additions & 2 deletions docs/source/pjrpc/webui.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ using flask web framework:
id: UserId


class AlreadyExistsError(pjrpc.exc.TypedError):
class AlreadyExistsError(pjrpc.server.exceptions.TypedError):
"""
User already registered error.
"""
Expand All @@ -95,7 +95,7 @@ using flask web framework:
MESSAGE = "user already exists"


class NotFoundError(pjrpc.exc.TypedError):
class NotFoundError(pjrpc.server.exceptions.TypedError):
"""
User not found error.
"""
Expand Down
8 changes: 5 additions & 3 deletions examples/aiohttp_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ async def test_using_fixture(pjrpc_aiohttp_mocker):
assert result == 2

pjrpc_aiohttp_mocker.replace(
'http://localhost/api/v1', 'sum', error=pjrpc.exc.JsonRpcError(code=1, message='error', data='oops'),
'http://localhost/api/v1',
'sum',
error=pjrpc.client.exceptions.JsonRpcError(code=1, message='error', data='oops'),
)
with pytest.raises(pjrpc.exc.JsonRpcError) as exc_info:
with pytest.raises(pjrpc.client.exceptions.JsonRpcError) as exc_info:
await client.proxy.sum(a=1, b=1)

assert exc_info.type is pjrpc.exc.JsonRpcError
assert exc_info.type is pjrpc.client.exceptions.JsonRpcError
assert exc_info.value.code == 1
assert exc_info.value.message == 'error'
assert exc_info.value.data == 'oops'
Expand Down
4 changes: 2 additions & 2 deletions examples/multiple_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pjrpc.client.backend import requests as jrpc_client


class ErrorV1(pjrpc.exc.TypedError, base=True):
class ErrorV1(pjrpc.client.exceptions.TypedError, base=True):
pass


Expand All @@ -13,7 +13,7 @@ class PermissionDenied(ErrorV1):
MESSAGE: ClassVar[str] = 'permission denied'


class ErrorV2(pjrpc.exc.TypedError, base=True):
class ErrorV2(pjrpc.client.exceptions.TypedError, base=True):
pass


Expand Down
4 changes: 2 additions & 2 deletions examples/openapi_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class UserOut(UserIn):
id: UserId


class AlreadyExistsError(pjrpc.exc.TypedError):
class AlreadyExistsError(pjrpc.server.exceptions.TypedError):
"""
User already registered error.
"""
Expand All @@ -76,7 +76,7 @@ class AlreadyExistsError(pjrpc.exc.TypedError):
MESSAGE = "user already exists"


class NotFoundError(pjrpc.exc.TypedError):
class NotFoundError(pjrpc.server.exceptions.TypedError):
"""
User not found error.
"""
Expand Down
4 changes: 2 additions & 2 deletions examples/openapi_aiohttp_subendpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import pjrpc.server.specs.extractors.pydantic
import pjrpc.server.specs.openapi.ui
from pjrpc.common.exceptions import MethodNotFoundError
from pjrpc.server.exceptions import MethodNotFoundError
from pjrpc.server.integration import aiohttp as integration
from pjrpc.server.specs import extractors, openapi, openrpc
from pjrpc.server.validators import pydantic as validators


class AlreadyExistsError(pjrpc.exc.TypedError):
class AlreadyExistsError(pjrpc.server.exceptions.TypedError):
"""
User already registered error.
"""
Expand Down
4 changes: 2 additions & 2 deletions examples/openapi_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class UserOut(UserIn):
id: UserId


class AlreadyExistsError(pjrpc.exc.TypedError):
class AlreadyExistsError(pjrpc.server.exceptions.TypedError):
"""
User already registered error.
"""
Expand All @@ -69,7 +69,7 @@ class AlreadyExistsError(pjrpc.exc.TypedError):
MESSAGE = "user already exists"


class NotFoundError(pjrpc.exc.TypedError):
class NotFoundError(pjrpc.server.exceptions.TypedError):
"""
User not found error.
"""
Expand Down
4 changes: 2 additions & 2 deletions examples/openrpc_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class UserOut(UserIn, title="User data"):
id: UserId


class AlreadyExistsError(pjrpc.exc.TypedError):
class AlreadyExistsError(pjrpc.server.exceptions.TypedError):
"""
User already registered error.
"""
Expand All @@ -72,7 +72,7 @@ class AlreadyExistsError(pjrpc.exc.TypedError):
MESSAGE = "user already exists"


class NotFoundError(pjrpc.exc.TypedError):
class NotFoundError(pjrpc.server.exceptions.TypedError):
"""
User not found error.
"""
Expand Down
10 changes: 6 additions & 4 deletions examples/requests_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ def test_using_fixture(pjrpc_requests_mocker):
assert result == 2

pjrpc_requests_mocker.replace(
'http://localhost/api/v1', 'sum', error=pjrpc.exc.JsonRpcError(code=1, message='error', data='oops'),
'http://localhost/api/v1',
'sum',
error=pjrpc.client.exceptions.JsonRpcError(code=1, message='error', data='oops'),
)
with pytest.raises(pjrpc.exc.JsonRpcError) as exc_info:
with pytest.raises(pjrpc.client.exceptions.JsonRpcError) as exc_info:
client.proxy.sum(a=1, b=1)

assert exc_info.type is pjrpc.exc.JsonRpcError
assert exc_info.type is pjrpc.client.exceptions.JsonRpcError
assert exc_info.value.code == 1
assert exc_info.value.message == 'error'
assert exc_info.value.data == 'oops'
Expand Down Expand Up @@ -52,5 +54,5 @@ def test_using_resource_manager():
assert localhost_calls[('2.0', 'div')].mock_calls == [mock.call(4, 2)]
assert localhost_calls[('2.0', 'mult')].mock_calls == [mock.call(2, 2)]

with pytest.raises(pjrpc.exc.MethodNotFoundError):
with pytest.raises(pjrpc.client.exceptions.MethodNotFoundError):
client.proxy.sub(4, 2)
2 changes: 1 addition & 1 deletion examples/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def sum(request: web.Request, a: int, b: int) -> int:
async def sentry_middleware(request: Request, context: web.Request, handler: AsyncHandlerType) -> Response:
try:
return await handler(request, context)
except pjrpc.exceptions.JsonRpcError as e:
except pjrpc.server.exceptions.JsonRpcError as e:
sentry_sdk.capture_exception(e)
raise

Expand Down
5 changes: 4 additions & 1 deletion pjrpc/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
JSON-RPC client.
"""

from . import validators
from . import exceptions, validators
from .client import AbstractAsyncClient, AbstractClient, AsyncMiddleware, AsyncMiddlewareHandler, Batch, Middleware
from .client import MiddlewareHandler
from .exceptions import JsonRpcError

__all__ = [
'AbstractAsyncClient',
'AbstractClient',
'AsyncMiddleware',
'AsyncMiddlewareHandler',
'Batch',
'exceptions',
'JsonRpcError',
'Middleware',
'MiddlewareHandler',
'validators',
Expand Down
11 changes: 5 additions & 6 deletions pjrpc/client/backend/aio_pika.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
from aio_pika.abc import AbstractIncomingMessage
from yarl import URL

import pjrpc
from pjrpc.client import AbstractAsyncClient, AsyncMiddleware
from pjrpc.common import AbstractRequest, AbstractResponse, BatchRequest, BatchResponse, JSONEncoder, JsonRpcError
from pjrpc.common import Request, Response, generators
from pjrpc.client import AbstractAsyncClient, AsyncMiddleware, exceptions
from pjrpc.common import AbstractRequest, AbstractResponse, BatchRequest, BatchResponse, JSONEncoder, Request, Response
from pjrpc.common import generators
from pjrpc.common.typedefs import JsonRpcRequestIdT

logger = logging.getLogger(__package__)
Expand Down Expand Up @@ -77,7 +76,7 @@ def __init__(
result_queue_name: Optional[str] = None,
result_queue_args: Optional[QueueArgs] = None,
id_gen_impl: Callable[..., Generator[JsonRpcRequestIdT, None, None]] = generators.sequential,
error_cls: type[JsonRpcError] = JsonRpcError,
error_cls: type[exceptions.JsonRpcError] = exceptions.JsonRpcError,
json_loader: Callable[..., Any] = json.loads,
json_dumper: Callable[..., str] = json.dumps,
json_encoder: type[JSONEncoder] = JSONEncoder,
Expand Down Expand Up @@ -196,7 +195,7 @@ async def _on_result_message(self, message: AbstractIncomingMessage) -> None:

if message.content_type not in self._response_content_types:
future.set_exception(
pjrpc.exc.DeserializationError(f"unexpected response content type: {message.content_type}"),
exceptions.DeserializationError(f"unexpected response content type: {message.content_type}"),
)
else:
future.set_result(message.body.decode(message.content_encoding or 'utf8'))
Expand Down
Loading