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

Update generic exception MeilisearchException to throw more accurate Exception #719

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 10 additions & 10 deletions src/main/java/com/meilisearch/sdk/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ <T> T get(String api, Class<T> targetClass, Class<?>... parameters)
* @param api Path to document
* @param param Parameter to be passed
* @return document that was requested
* @throws MeilisearchException if the response is an error
* @throws MeilisearchApiException if the response is an error
*/
<T> T get(String api, String param, Class<T> targetClass, Class<?>... parameters)
throws MeilisearchException {
throws MeilisearchApiException {
HttpRequest requestConfig = request.create(HttpMethod.GET, api + param, this.headers, null);
HttpResponse<T> httpRequest = this.client.get(requestConfig);
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass, parameters);
Expand All @@ -88,10 +88,10 @@ <T> T get(String api, String param, Class<T> targetClass, Class<?>... parameters
* @param api Path to server
* @param body Query for search
* @return results of the search
* @throws MeilisearchException if the response is an error
* @throws MeilisearchApiException if the response is an error
*/
<S, T> T post(String api, S body, Class<T> targetClass, Class<?>... parameters)
throws MeilisearchException {
throws MeilisearchApiException {
HttpRequest requestConfig = request.create(HttpMethod.POST, api, this.headers, body);
HttpResponse<T> httpRequest = this.client.post(requestConfig);
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass, parameters);
Expand All @@ -109,9 +109,9 @@ <S, T> T post(String api, S body, Class<T> targetClass, Class<?>... parameters)
* @param api Path to the requested resource
* @param body Replacement data for the requested resource
* @return updated resource
* @throws MeilisearchException if the response is an error
* @throws MeilisearchApiException if the response is an error
*/
<S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchException {
<S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchApiException {
HttpRequest requestConfig = request.create(HttpMethod.PUT, api, this.headers, body);
HttpResponse<T> httpRequest = this.client.put(requestConfig);
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
Expand All @@ -129,9 +129,9 @@ <S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchExcepti
* @param api Path to server
* @param body Query for search
* @return results of the search
* @throws MeilisearchException if the response is an error
* @throws MeilisearchApiException if the response is an error
*/
<S, T> T patch(String api, S body, Class<T> targetClass) throws MeilisearchException {
<S, T> T patch(String api, S body, Class<T> targetClass) throws MeilisearchApiException {
HttpRequest requestConfig = request.create(HttpMethod.PATCH, api, this.headers, body);
HttpResponse<T> httpRequest = this.client.patch(requestConfig);
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
Expand All @@ -148,9 +148,9 @@ <S, T> T patch(String api, S body, Class<T> targetClass) throws MeilisearchExcep
*
* @param api Path to the requested resource
* @return deleted resource
* @throws MeilisearchException if the response is an error
* @throws MeilisearchApiException if the response is an error
*/
<T> T delete(String api, Class<T> targetClass) throws MeilisearchException {
<T> T delete(String api, Class<T> targetClass) throws MeilisearchApiException {
HttpRequest requestConfig = request.create(HttpMethod.DELETE, api, this.headers, null);
HttpResponse<T> httpRequest = this.client.delete(requestConfig);
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/meilisearch/sdk/TasksHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ void waitForTask(int taskUid) throws MeilisearchException {
* @param taskUid Identifier of the Task
* @param timeoutInMs number of milliseconds before throwing an Exception
* @param intervalInMs number of milliseconds before requesting the status again
* @throws MeilisearchException if timeout is reached
* @throws MeilisearchTimeoutException if timeout is reached
*/
void waitForTask(int taskUid, int timeoutInMs, int intervalInMs) throws MeilisearchException {
void waitForTask(int taskUid, int timeoutInMs, int intervalInMs)
throws MeilisearchTimeoutException {
Task task;
TaskStatus status = null;
long startTime = new Date().getTime();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.meilisearch.sdk.exceptions;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class MeilisearchUrlException extends MeilisearchException {

String error;

public MeilisearchUrlException(Exception e) {
super(e);
this.error = e.toString();
}

@Override
public String toString() {
return "Meilisearch UrlException: {" + "Error=" + this.error + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.meilisearch.sdk.exceptions.MeilisearchCommunicationException;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.exceptions.MeilisearchTimeoutException;
import com.meilisearch.sdk.exceptions.MeilisearchUrlException;
import com.meilisearch.sdk.http.request.HttpRequest;
import com.meilisearch.sdk.http.response.HttpResponse;
import java.io.IOException;
Expand Down Expand Up @@ -31,14 +32,16 @@ public CustomOkHttpClient(Config config) {
this.client = new OkHttpClient();
}

public <T> HttpResponse<T> execute(HttpRequest request) throws MeilisearchException {
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 MeilisearchException(e);
throw new MeilisearchUrlException(e);
} catch (SocketTimeoutException e) {
throw new MeilisearchTimeoutException(e);
} catch (IOException e) {
Expand Down
Loading