Skip to content

Throw a specific Exception instead of Exception in handlers #389

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

Closed
alallema opened this issue May 18, 2022 · 1 comment · Fixed by #438
Closed

Throw a specific Exception instead of Exception in handlers #389

alallema opened this issue May 18, 2022 · 1 comment · Fixed by #438
Labels
enhancement New feature or request

Comments

@alallema
Copy link
Contributor

alallema commented May 18, 2022

Following this discussion, it could be better to throw specifics Exceptions for all the handlers:

I mean, is good to only throw errors that we can control, Exception is Ok, but in the end is easier for the consumer just to expect a MeiliSearchApiException instead of the Exception because they will have more control over the exception catch flow like this:

// when we just throw a Meilisearch error, the user can catch all the Meilisearch errors first:
// of course, they need to respect an inheritance (we can talk about this later).

try {
  client.index("movies").anyMethodThatThrowsSomething();
} catch(MeiliSearchOtherException e) {
  // do something with a meilisearch error.
} catch(MeiliSearchActionException e) {
  // do something with a meilisearch error.
} catch(MeiliSearchApiException e) {
  // do something with a meilisearch error.
}

Now if we just throw an Exception:

// in this new case, we can see the code is different and harder to read because they had to handle "multiple" 
// different exception possibilities or even check if this exception isn't coming from the okhttp for example.

try {
  client.index("movies").anyMethodThatThrowsSomething();
} catch(Exception e) {
   if (e.message.startsWith("message")) // do something
   else if (e.message.startsWith("other message")) // do other thing 
}

Originally posted by @brunoocasali in #371 (comment)

@alallema alallema changed the title Throw a specific Exception instead of Exception Throw a specific Exception instead of Exception in handlers May 18, 2022
@alallema alallema added the enhancement New feature or request label May 18, 2022
@alallema alallema linked a pull request Sep 26, 2022 that will close this issue
2 tasks
@alallema
Copy link
Contributor Author

Closed by #438

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant