Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log warning if errors are occured during the execute method #127

Merged
merged 1 commit into from
Jun 5, 2022
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
1 change: 1 addition & 0 deletions src/vk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class VkAPIError(VkException):
def __init__(self, error_data):
super(VkAPIError, self).__init__()
self.error_data = error_data
self.method = error_data.get('method')
self.code = error_data.get('error_code')
self.message = error_data.get('error_msg')
self.request_params = self.get_pretty_request_params(error_data)
Expand Down
11 changes: 6 additions & 5 deletions src/vk/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ def send(self, request):
request.response = response_or_error

if 'response' in response_or_error:
# todo Can we have error and response simultaneously
# for error in errors:
# logger.warning(str(error))

for error_data in response_or_error.get('execute_errors', ()):
api_error = VkAPIError(error_data)
logger.warning('Execute "%s" error: %s', api_error.method, api_error)

return response_or_error['response']

elif 'error' in response_or_error:
api_error = VkAPIError(request.response['error'])
request.api_error = api_error
request.api_error = VkAPIError(request.response['error'])
return self.handle_api_error(request)

def prepare_request(self, request): # noqa: U100
Expand Down
19 changes: 15 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import pytest

from vk import API
Expand All @@ -9,8 +11,17 @@ def api(access_token, v):


def test_durov(api):
"""
Get users
"""
users = api.users.get(user_id=1)
users = api.users.get(user_ids=1)
assert users[0]['last_name'] == 'Durov'


def test_execute(caplog, api):
with caplog.at_level(logging.WARNING, logger='vk'):
api.execute(code='var x = API.storage.get(); '
'return API.users.get({user_ids: 1});')

assert len(caplog.records) == 1

log = caplog.records[0]
assert log.levelno == logging.WARNING
assert 'Execute "storage.get" error' in log.message