meilisearch / meilisearch/meilisearch-java
Refining exception throws
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 245
- Forks
- 152
- PR merge metrics
- No merged PRs in 30d
Description
Description
Currently, the exceptions thrown are all MeilisearchException.
This makes it difficult for the user to specify which exception was thrown.
Basic example
The way to fix this is to throw all exceptions as-is.
Instead, the practice of creating and throwing MeilsearchException directly should be eliminated.
AS-IS
public <T> HttpResponse<T> execute(HttpRequest request) throws MeilisearchException {
try {
Request okRequest = buildRequest(request);
Response response = client.newCall(okRequest).execute();
return buildResponse(response);
} catch (MalformedURLException e) {
throw new MeilisearchException(e); // do not throw MeilisearchException directly!
} catch (SocketTimeoutException e) {
throw new MeilisearchTimeoutException(e);
} catch (IOException e) {
throw new MeilisearchCommunicationException(e);
}
}
If the user wants to get a specific exception, they have to compare if it is castable and perform the cast.
TO-BE
public <T> HttpResponse<T> execute(HttpRequest request)
throws MeilisearchURLException, MeilisearchTimeoutException, MeilisearchCommunicationException {
try {
Request okRequest = buildRequest(request);
Response response = client.newCall(okRequest).execute();
return buildResponse(response);
} catch (MalformedURLException e) {
throw new MeilisearchURLException(e); // for example
} catch (SocketTimeoutException e) {
throw new MeilisearchTimeoutException(e);
} catch (IOException e) {
throw new MeilisearchCommunicationException(e);
}
}
With this change, users will be able to receive verbose exceptions(e.g. MeilisearchURLException..) as they are,
or as a MeilisearchException as they do now.
Other
This issue has great synergy when applied with #677.
If all exceptions were RuntimeException, we wouldn't need any code to re-throw.
public <T> HttpResponse<T> execute(HttpRequest request) { // we don't have to re-throw!
try {
Request okRequest = buildRequest(request);
Response response = client.newCall(okRequest).execute();
return buildResponse(response);
} catch (MalformedURLException e) {
throw new MeilisearchURLException(e); // for example
} catch (SocketTimeoutException e) {
throw new MeilisearchTimeoutException(e);
} catch (IOException e) {
throw new MeilisearchCommunicationException(e);
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the execute entry point and the exception types shown in the issue, then review the related proposal in #677. Done means specific exceptions such as MeilisearchURLException, MeilisearchTimeoutException, and MeilisearchCommunicationException propagate directly while callers can still handle them as MeilisearchException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100