Skip to content

Commit f0a227f

Browse files
committed
Throw MeiliSearchApiException when status code > 400
1 parent 2a318e7 commit f0a227f

File tree

5 files changed

+86
-10
lines changed

5 files changed

+86
-10
lines changed

src/main/java/com/meilisearch/sdk/Client.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import com.google.gson.Gson;
77

8+
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;
9+
810
/**
911
* Meilisearch client
1012
*/
@@ -32,10 +34,8 @@ public Client(Config config) {
3234
* @return Meilisearch API response
3335
* @throws Exception If an error occurs
3436
*/
35-
public Index createIndex(String uid) throws Exception {
36-
Index index = gson.fromJson(this.indexesHandler.create(uid), Index.class);
37-
index.setConfig(this.config);
38-
return index;
37+
public Index createIndex(String uid) throws Exception, MeiliSearchApiException {
38+
return this.createIndex(uid, null);
3939
}
4040

4141
/**
@@ -47,8 +47,7 @@ public Index createIndex(String uid) throws Exception {
4747
* @return Meilisearch API response
4848
* @throws Exception If an error occurs
4949
*/
50-
public Index createIndex(String uid, String primaryKey) throws Exception {
51-
// return this.indexesHandler.create(uid, primaryKey);
50+
public Index createIndex(String uid, String primaryKey) throws Exception, MeiliSearchApiException {
5251
Index index = gson.fromJson(this.indexesHandler.create(uid, primaryKey), Index.class);
5352
index.setConfig(this.config);
5453
return index;

src/main/java/com/meilisearch/sdk/IndexesHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.gson.JsonObject;
44

5+
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;
6+
57
/**
68
* Wrapper around the MeilisearchHttpRequest class to ease usage for Meilisearch indexes
79
*/
@@ -24,7 +26,7 @@ class IndexesHandler {
2426
* @return Meilisearch API response
2527
* @throws Exception If something goes wrong
2628
*/
27-
String create(String uid) throws Exception {
29+
String create(String uid) throws Exception, MeiliSearchApiException {
2830
return this.create(uid, null);
2931
}
3032

@@ -36,13 +38,12 @@ String create(String uid) throws Exception {
3638
* @return Meilisearch API response
3739
* @throws Exception If something goes wrong
3840
*/
39-
String create(String uid, String primaryKey) throws Exception {
41+
String create(String uid, String primaryKey) throws Exception, MeiliSearchApiException {
4042
JsonObject params = new JsonObject();
4143
params.addProperty("uid", uid);
4244
if (primaryKey != null) {
4345
params.addProperty("primaryKey", primaryKey);
4446
}
45-
4647
return meilisearchHttpRequest.post("/indexes", params.toString());
4748
}
4849

src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.meilisearch.sdk.http.factory.RequestFactory;
77
import com.meilisearch.sdk.http.request.HttpMethod;
88
import com.meilisearch.sdk.http.response.HttpResponse;
9+
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;
910

1011
import java.util.Collections;
1112

@@ -34,14 +35,20 @@ String get(String api, String param) throws Exception {
3435
}
3536

3637

37-
String post(String api, String body) throws Exception {
38+
String post(String api, String body) throws Exception, MeiliSearchApiException {
3839
HttpResponse<?> httpResponse = this.client.post(factory.create(HttpMethod.POST, api, Collections.emptyMap(), body));
40+
if (httpResponse.getStatusCode() >= 400) {
41+
throw new MeiliSearchApiException(httpResponse.getContent().toString());
42+
}
3943
return new String(httpResponse.getContentAsBytes());
4044
}
4145

4246

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

src/test/java/com/meilisearch/integration/ClientTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

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

14+
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;
15+
1416
@Tag("integration")
1517
public class ClientTest extends AbstractIT {
1618

@@ -53,6 +55,22 @@ public void testCreateIndexWithPrimaryKey() throws Exception {
5355
client.deleteIndex(index.getUid());
5456
}
5557

58+
/**
59+
* Test Index creation error: already exists
60+
*/
61+
@Test
62+
public void testCreateIndexAlreadyExists() throws Exception {
63+
String indexUid = "CreateIndexAlreadyExists";
64+
Index index = client.createIndex(indexUid, this.primaryKey);
65+
assertEquals(index.getUid(), indexUid);
66+
assertEquals(index.getPrimaryKey(), this.primaryKey);
67+
assertThrows(
68+
MeiliSearchApiException.class,
69+
() -> client.createIndex(indexUid, this.primaryKey)
70+
);
71+
client.deleteIndex(index.getUid());
72+
}
73+
5674
/**
5775
* Test update Index PrimaryKey
5876
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.meilisearch.integration;
2+
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Tag;
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.meilisearch.sdk.exceptions.MeiliSearchException;
9+
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
@Tag("integration")
14+
public class ExceptionsTest extends AbstractIT {
15+
16+
17+
@BeforeEach
18+
public void initialize() {
19+
this.setUp();
20+
}
21+
22+
@AfterAll
23+
static void cleanMeiliSearch() {
24+
cleanup();
25+
}
26+
27+
/**
28+
* Test MeiliSearchApiError serialization and getters
29+
*/
30+
@Test
31+
public void testErrorSerializeAndGetters() throws Exception {
32+
String errorMessage = "You must have an authorization token";
33+
String errorCode = "missing_authorization_header";
34+
String errorType = "authentication_error";
35+
String errorLink = "https://docs.meilisearch.com/errors#missing_authorization_header";
36+
try {
37+
throw new MeiliSearchApiException(
38+
"{"
39+
+ "\"message\":\"" + errorMessage + "\","
40+
+ "\"errorCode\":\"" + errorCode + "\","
41+
+ "\"errorType\":\"" + errorType + "\","
42+
+ "\"errorLink\":\"" + errorLink + "\""
43+
+ "}");
44+
} catch (MeiliSearchApiException e) {
45+
assertEquals(errorMessage, e.getMessage());
46+
assertEquals(errorCode, e.getErrorCode());
47+
assertEquals(errorType, e.getErrorType());
48+
assertEquals(errorLink, e.getErrorLink());
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)