Skip to content

Custom Exceptions: MeiliSearchException => MeiliSearchApiException + MeiliSearchConnectionException #46

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 4 commits into from
Nov 4, 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
11 changes: 5 additions & 6 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.google.gson.Gson;

import com.meilisearch.sdk.exceptions.MeiliSearchApiException;

/**
* Meilisearch client
*/
Expand Down Expand Up @@ -32,10 +34,8 @@ public Client(Config config) {
* @return Meilisearch API response
* @throws Exception If an error occurs
*/
public Index createIndex(String uid) throws Exception {
Index index = gson.fromJson(this.indexesHandler.create(uid), Index.class);
index.setConfig(this.config);
return index;
public Index createIndex(String uid) throws Exception, MeiliSearchApiException {
return this.createIndex(uid, null);
}

/**
Expand All @@ -47,8 +47,7 @@ public Index createIndex(String uid) throws Exception {
* @return Meilisearch API response
* @throws Exception If an error occurs
*/
public Index createIndex(String uid, String primaryKey) throws Exception {
// return this.indexesHandler.create(uid, primaryKey);
public Index createIndex(String uid, String primaryKey) throws Exception, MeiliSearchApiException {
Index index = gson.fromJson(this.indexesHandler.create(uid, primaryKey), Index.class);
index.setConfig(this.config);
return index;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.meilisearch.sdk.exceptions;

import com.meilisearch.sdk.json.GsonJsonHandler;

public class MeiliSearchApiException extends MeiliSearchException {

/**
* This is class wraps errors sent by MeiliSearch API
*/

private GsonJsonHandler gson = new GsonJsonHandler();

private class ApiError {
public String message;
public String errorCode;
public String errorType;
public String errorLink;
}

public MeiliSearchApiException (String errorMessage) throws Exception {
super(errorMessage);
ApiError error = this.gson.decode(
errorMessage,
ApiError.class
);
this.setErrorMessage(error.message);
this.setErrorCode(error.errorCode);
this.setErrorType(error.errorType);
this.setErrorLink(error.errorLink);
}

public String toString() {
return this.getClass().getName()
+ ". Error message: " + this.errorMessage
+ ". Error code: " + this.errorCode
+ ". Error documentation: " + this.errorLink
+ ". Error type: " + this.errorType
+ ".";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.meilisearch.sdk.exceptions;

public class MeiliSearchCommunicationException extends MeiliSearchException {

public MeiliSearchCommunicationException (String errorMessage) {
super(errorMessage);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.meilisearch.sdk.exceptions;

public class MeiliSearchException extends Exception {

/**
* This is a generic class for MeiliSearch Exception handling
*/

String errorMessage;
String errorType;
String errorCode;
String errorLink;

public MeiliSearchException (String errorMessage) {
super(errorMessage);
this.setErrorMessage(errorMessage);
}

public String getMessage () {
return this.errorMessage;
}

public String getErrorType () {
return this.errorType;
}

public String getErrorCode () {
return this.errorCode;
}

public String getErrorLink () {
return this.errorLink;
}

public void setErrorMessage (String errorMessage) {
this.errorMessage = errorMessage;
}

public void setErrorType (String errorType) {
this.errorType = errorType;
}

public void setErrorCode (String errorCode) {
this.errorCode = errorCode;
}

public void setErrorLink (String errorLink) {
this.errorLink = errorLink;
}

public String toString() {
return this.getClass().getName()
+ ". Error message: " + this.errorMessage
+ ".";
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/meilisearch/sdk/IndexesHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.gson.JsonObject;

import com.meilisearch.sdk.exceptions.MeiliSearchApiException;

/**
* Wrapper around the MeilisearchHttpRequest class to ease usage for Meilisearch indexes
*/
Expand All @@ -24,7 +26,7 @@ class IndexesHandler {
* @return Meilisearch API response
* @throws Exception If something goes wrong
*/
String create(String uid) throws Exception {
String create(String uid) throws Exception, MeiliSearchApiException {
return this.create(uid, null);
}

Expand All @@ -36,13 +38,12 @@ String create(String uid) throws Exception {
* @return Meilisearch API response
* @throws Exception If something goes wrong
*/
String create(String uid, String primaryKey) throws Exception {
String create(String uid, String primaryKey) throws Exception, MeiliSearchApiException {
JsonObject params = new JsonObject();
params.addProperty("uid", uid);
if (primaryKey != null) {
params.addProperty("primaryKey", primaryKey);
}

return meilisearchHttpRequest.post("/indexes", params.toString());
}

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.meilisearch.sdk.http.factory.RequestFactory;
import com.meilisearch.sdk.http.request.HttpMethod;
import com.meilisearch.sdk.http.response.HttpResponse;
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;

import java.util.Collections;

Expand All @@ -24,30 +25,42 @@ public MeiliSearchHttpRequest(AbstractHttpClient client, RequestFactory factory)
}


public String get(String api) throws Exception {
public String get(String api) throws Exception, MeiliSearchApiException {
return this.get(api, "");
}

String get(String api, String param) throws Exception {
String get(String api, String param) throws Exception, MeiliSearchApiException {
HttpResponse<?> httpResponse = this.client.get(factory.create(HttpMethod.GET, api + param, Collections.emptyMap(), null));
if (httpResponse.getStatusCode() >= 400) {
throw new MeiliSearchApiException(httpResponse.getContent().toString());
}
return new String(httpResponse.getContentAsBytes());
}


String post(String api, String body) throws Exception {
String post(String api, String body) throws Exception, MeiliSearchApiException {
HttpResponse<?> httpResponse = this.client.post(factory.create(HttpMethod.POST, api, Collections.emptyMap(), body));
if (httpResponse.getStatusCode() >= 400) {
throw new MeiliSearchApiException(httpResponse.getContent().toString());
}
return new String(httpResponse.getContentAsBytes());
}


String put(String api, String body) throws Exception {
String put(String api, String body) throws Exception, MeiliSearchApiException {
HttpResponse<?> httpResponse = this.client.put(factory.create(HttpMethod.PUT, api, Collections.emptyMap(), body));
if (httpResponse.getStatusCode() >= 400) {
throw new MeiliSearchApiException(httpResponse.getContent().toString());
}
return new String(httpResponse.getContentAsBytes());
}


String delete(String api) throws Exception {
String delete(String api) throws Exception, MeiliSearchApiException {
HttpResponse<?> httpResponse = this.client.put(factory.create(HttpMethod.DELETE, api, Collections.emptyMap(), null));
if (httpResponse.getStatusCode() >= 400) {
throw new MeiliSearchApiException(httpResponse.getContent().toString());
}
return new String(httpResponse.getContentAsBytes());
}
}
11 changes: 8 additions & 3 deletions src/main/java/com/meilisearch/sdk/http/DefaultHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ private HttpResponse<?> execute(HttpRequest<?> request) throws IOException {
connection.getOutputStream().write(request.getContentAsBytes());
}

InputStream errorStream = connection.getErrorStream();
InputStream contentStream = (errorStream != null ? errorStream : connection.getInputStream());
if (connection.getResponseCode() >= 400) {
return new BasicHttpResponse(
Collections.emptyMap(),
connection.getResponseCode(),
new BufferedReader(new InputStreamReader(connection.getErrorStream())).lines().collect(Collectors.joining("\n"))
);
}

return new BasicHttpResponse(
Collections.emptyMap(),
connection.getResponseCode(),
new BufferedReader(new InputStreamReader(contentStream)).lines().collect(Collectors.joining("\n"))
new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining("\n"))
);
}

Expand Down
20 changes: 19 additions & 1 deletion src/test/java/com/meilisearch/integration/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import static org.junit.jupiter.api.Assertions.*;

import com.meilisearch.sdk.exceptions.MeiliSearchApiException;

@Tag("integration")
public class ClientTest extends AbstractIT {

Expand Down Expand Up @@ -53,6 +55,22 @@ public void testCreateIndexWithPrimaryKey() throws Exception {
client.deleteIndex(index.getUid());
}

/**
* Test Index creation error: already exists
*/
@Test
public void testCreateIndexAlreadyExists() throws Exception {
String indexUid = "CreateIndexAlreadyExists";
Index index = client.createIndex(indexUid, this.primaryKey);
assertEquals(index.getUid(), indexUid);
assertEquals(index.getPrimaryKey(), this.primaryKey);
assertThrows(
MeiliSearchApiException.class,
() -> client.createIndex(indexUid, this.primaryKey)
);
client.deleteIndex(index.getUid());
}

/**
* Test update Index PrimaryKey
*/
Expand Down Expand Up @@ -107,7 +125,7 @@ public void testDeleteIndex() throws Exception {
Index index = client.createIndex(indexUid);
client.deleteIndex(index.getUid());
assertThrows(
Exception.class,
MeiliSearchApiException.class,
() -> client.getIndex(indexUid)
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/meilisearch/integration/DocumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import static org.junit.jupiter.api.Assertions.*;

import com.meilisearch.sdk.exceptions.MeiliSearchApiException;

@Tag("integration")
public class DocumentsTest extends AbstractIT {

Expand Down Expand Up @@ -180,7 +182,7 @@ public void testDeleteDocument() throws Exception {
index.waitForPendingUpdate(updateInfo.getUpdateId());

assertThrows(
Exception.class,
MeiliSearchApiException.class,
() -> index.getDocument(toDelete.getId())
);
}
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/com/meilisearch/integration/ExceptionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.meilisearch.integration;

import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.exceptions.MeiliSearchException;
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

@Tag("integration")
public class ExceptionsTest extends AbstractIT {


@BeforeEach
public void initialize() {
this.setUp();
}

@AfterAll
static void cleanMeiliSearch() {
cleanup();
}

/**
* Test MeiliSearchApiException serialization and getters
*/
@Test
public void testErrorSerializeAndGetters() throws Exception {
String errorMessage = "You must have an authorization token";
String errorCode = "missing_authorization_header";
String errorType = "authentication_error";
String errorLink = "https://docs.meilisearch.com/errors#missing_authorization_header";
try {
throw new MeiliSearchApiException(
"{"
+ "\"message\":\"" + errorMessage + "\","
+ "\"errorCode\":\"" + errorCode + "\","
+ "\"errorType\":\"" + errorType + "\","
+ "\"errorLink\":\"" + errorLink + "\""
+ "}");
} catch (MeiliSearchApiException e) {
assertEquals(errorMessage, e.getMessage());
assertEquals(errorCode, e.getErrorCode());
assertEquals(errorType, e.getErrorType());
assertEquals(errorLink, e.getErrorLink());
}
}

/**
* Test MeiliSearchApiException is thrown on MeiliSearch bad request
*/
@Test
public void testMeiliSearchApiExceptionBadRequest () throws Exception {
String indexUid = "MeiliSearchApiExceptionBadRequest";
Index index = client.createIndex(indexUid);
assertThrows(
MeiliSearchException.class,
() -> client.createIndex(indexUid)
);
try {
client.createIndex(indexUid);
} catch (MeiliSearchApiException e) {
assertEquals("index_already_exists", e.getErrorCode());
}
}
}