Skip to content

Simple error handler #82

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

Merged
merged 1 commit into from
Jun 2, 2020
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
48 changes: 16 additions & 32 deletions meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from meilisearch.errors import MeiliSearchApiError, MeiliSearchCommunicationError

class HttpRequests:

Expand All @@ -12,43 +13,28 @@ def __init__(self, config):
'Content-Type': 'application/json'
}

def send_request(self, http_method, path, body=None):
try:
request_path = self.config.url + '/' + path
if body is None:
request = http_method(request_path, headers=self.headers)
else:
request = http_method(request_path, headers=self.headers, json=body)
return self.__validate(request)
except requests.exceptions.ConnectionError as err:
raise MeiliSearchCommunicationError(err)

def get(self, path):
request = requests.get(
self.config.url + '/' + path,
headers=self.headers,
)
return self.__validate(request)
return self.send_request(requests.get, path)

def post(self, path, body=None):
if body is None:
body = {}
request = requests.post(
self.config.url + '/' + path,
headers=self.headers,
json=body
)
return self.__validate(request)
return self.send_request(requests.post, path, body)

def put(self, path, body=None):
if body is None:
body = {}
request = requests.put(
self.config.url + '/' + path,
headers=self.headers,
json=body
)
return self.__validate(request)
return self.send_request(requests.put, path, body)

def delete(self, path, body=None):
if body is None:
body = {}
request = requests.delete(
self.config.url + '/' + path,
headers=self.headers,
json=body
)
return self.__validate(request)
return self.send_request(requests.delete, path, body)

@staticmethod
def __to_json(request):
Expand All @@ -62,6 +48,4 @@ def __validate(request):
request.raise_for_status()
return HttpRequests.__to_json(request)
except requests.exceptions.HTTPError as err:
raise Exception(err)
except requests.exceptions.ConnectionError as err:
raise Exception(err)
raise MeiliSearchApiError(err, request)
31 changes: 31 additions & 0 deletions meilisearch/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json

class MeiliSearchError(Exception):
"""Generic class for MeiliSearch error handling"""

def __init__(self, message):
self.message = message
super().__init__(self.message)

def __str__(self):
return f'MeiliSearchError, {self.message}'

class MeiliSearchApiError(MeiliSearchError):
"""Error sent by MeiliSearch API"""

def __init__(self, error, request):
self.status_code = request.status_code
if request.text:
self.message = f'{json.loads(request.text)["message"]}'
else:
self.message = error
super().__init__(self.message)

def __str__(self):
return f'MeiliSearchApiError, HTTP status: {self.status_code} -> {self.message}'

class MeiliSearchCommunicationError(MeiliSearchError):
"""Error connecting to MeiliSearch"""

def __str__(self):
return f'MeiliSearchCommunicationError, {self.message}'
Empty file.
20 changes: 20 additions & 0 deletions meilisearch/tests/errors/test_api_error_meilisearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
import meilisearch
from meilisearch.errors import MeiliSearchApiError
from meilisearch.tests import BASE_URL, MASTER_KEY

class TestMeiliSearchApiError:

""" TESTS: MeiliSearchApiError class """

@staticmethod
def test_meilisearch_api_error_no_master_key():
client = meilisearch.Client(BASE_URL)
with pytest.raises(MeiliSearchApiError):
client.create_index("some_index")

@staticmethod
def test_meilisearch_api_error_wrong_master_key():
client = meilisearch.Client(BASE_URL, MASTER_KEY + '123')
with pytest.raises(MeiliSearchApiError):
client.create_index("some_index")
14 changes: 14 additions & 0 deletions meilisearch/tests/errors/test_communication_error_meilisearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
import meilisearch
from meilisearch.errors import MeiliSearchCommunicationError
from meilisearch.tests import MASTER_KEY

class TestMeiliSearchCommunicationError:

""" TESTS: MeiliSearchCommunicationError class """

@staticmethod
def test_meilisearch_communication_error_host():
client = meilisearch.Client("http://wrongurl:1234", MASTER_KEY)
with pytest.raises(MeiliSearchCommunicationError):
client.create_index("some_index")