-
Notifications
You must be signed in to change notification settings - Fork 90
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too bad for the code duplication with MeiliSearchCommunicationError
, but I don't find any other way to do that! What do you think @eskombro?
Can you add tests for your exceptions? 🙂 Especially testing the Communication error (you can put localhost:8800 if I'm not wrong). I know you have to wait for the @eskombro merge 😉
Are these changes breaking for you guys? @eskombro @bidoubiwa
For me, they are.
If they are, can you rename the apikey
parameter into api_key
please? :)
meilisearch/_httprequests.py
Outdated
@@ -14,41 +16,52 @@ def __init__(self, config): | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
meilisearch/_httprequests.py
Outdated
) | ||
return self.__validate(request) | ||
except requests.exceptions.ConnectionError as err: | ||
raise MeiliSearchCommunicationError(err) from None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
raise MeiliSearchCommunicationError(err) from None | |
raise MeiliSearchCommunicationError(err) from None | |
meilisearch/meilisearch_api_error.py
Outdated
def __init__(self, error, request): | ||
self.status_code = request.status_code | ||
if request.text: | ||
self.message = f'{json.loads(request.text)["message"]}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the is no "message" in the body? (it should not happen but it would be bad if there is an exception in the exception ^^ There was a time where MeiliSearch did not return any message for some HTTP error = 4XX or 5XX)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, should check if there's a message before setting it to the internat attribute. And have a default value like "No message received" for example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you think that the user would believe that there is an error because "there is no message" with this default value? @eskombro
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a simple unknown
can solve that?
@bidoubiwa I need confirmation about usage. With the except meilisearch.meilisearch_communication_error.MeiliSearchCommunicationError as err:
print(err.message) And with except meilisearch.meilisearch_api_error.MeiliSearchApiError as err:
print(err.status_code)
print(err.message) Can you confirm that? |
Like your work about Error handler anyway! It was needed!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error handler is great, and message is way better that returning the http error. I really like it.
I think that those error classes should be internal classes to HttpRequests class, and not separate classes in the package, what do you think @curquiza and @bidoubiwa ?
Or, we can have a MeiliSearchException (or MeiliSearchError) class and those classes inherit from the Super Error class, modifying just what is specific
:)
meilisearch/meilisearch_api_error.py
Outdated
def __init__(self, error, request): | ||
self.status_code = request.status_code | ||
if request.text: | ||
self.message = f'{json.loads(request.text)["message"]}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, should check if there's a message before setting it to the internat attribute. And have a default value like "No message received" for example
Not sure doing internal class error for the HTTPRequest class would be a good practice. I found these both separated files in Algolia and stripe packages.
Why not! I tried to start a thing like that in our ruby SDK but completely useless for the moment (because of the lazy "we'll see later" implementation, not the idea 😂) |
We checked with @bidoubiwa how several 'famous' packages handle errors. Seemds like conclusions are:
We also agreed on adding tests, @bidoubiwa waits for #80 to be merged to implement the tests
I agree, but every way of implementing DRY I can think of makes me feel like we would be adding unnecessary complexity and loosing in readability, so I would prefer to accpt @bidoubiwa 's proposition
I think we should consider them as breaking. This has an impact an almost every return value |
Ah, PS: Also, removing |
7acfca9
to
5c59b0c
Compare
- Parent MeiliSearch error class, single file and heritage - Tests: MeiliSearchCommunicationError and MeiliSearchApiError - Http requests error raise refacto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Error handling in python
Custom error created
Usage
MeiliSearchApiError will output
Implementation
MeiliSearchApiError
This error will use the default HTTP error message if MeiliSearch did not return a message.
It is called in the __validate method of the
httpRequests
class:By adding from None we prevent the chaining of errors.
MeiliSearchCommunicationError
This error will use the default error thrown by the HTTP request library to display the communication problem.
Mentionned in: /meilisearch/integration-guides#19