Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit 9b237fe

Browse files
author
Clément Le Provost
committed
refact(offline): Better error reporting when building offline indices
NOTE: This requires upgrading to version 1.2.0 of the Offline Core.
1 parent 182eb5b commit 9b237fe

File tree

4 files changed

+23
-49
lines changed

4 files changed

+23
-49
lines changed

algoliasearch/build-offline.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ android {
4040
}
4141

4242
dependencies {
43-
offlineCompile "com.algolia:algoliasearch-offline-core-android:1.1.0"
43+
offlineCompile "com.algolia:algoliasearch-offline-core-android:1.2.0"
4444
}
4545

4646

algoliasearch/src/offline/java/com/algolia/search/saas/MirroredIndex.java

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -719,12 +719,12 @@ public void run() {
719719
for (int i = 0; i < objectFiles.length; ++i) {
720720
objectFilePaths[i] = objectFiles[i].getAbsolutePath();
721721
}
722-
final int status = getLocalIndex().build(settingsFile.getAbsolutePath(), objectFilePaths, true /* clearIndex */, null /* deletedObjectIDs */);
723-
if (status != 200) {
724-
error = new AlgoliaException(String.format("Failed to build local mirror \"%s\"", MirroredIndex.this.getIndexName()), status);
725-
throw error;
726-
}
727-
return new JSONObject();
722+
final Response result = getLocalIndex().build(settingsFile.getAbsolutePath(), objectFilePaths, true /* clearIndex */, null /* deletedObjectIDs */);
723+
return OfflineClient.parseSearchResults(result);
724+
}
725+
catch (AlgoliaException e) {
726+
error = e;
727+
throw e;
728728
}
729729
finally {
730730
// Notify listeners.
@@ -1042,21 +1042,8 @@ protected JSONObject run() throws AlgoliaException {
10421042

10431043
private JSONObject _searchOffline(@NonNull Query query) throws AlgoliaException
10441044
{
1045-
try {
1046-
Response searchResults = getLocalIndex().search(query.build());
1047-
if (searchResults.getStatusCode() == 200) {
1048-
String jsonString = new String(searchResults.getData(), "UTF-8");
1049-
JSONObject json = new JSONObject(jsonString);
1050-
// NOTE: Origin tagging performed by the SDK.
1051-
return json;
1052-
}
1053-
else {
1054-
throw new AlgoliaException(searchResults.getErrorMessage(), searchResults.getStatusCode());
1055-
}
1056-
}
1057-
catch (JSONException | UnsupportedEncodingException e) {
1058-
throw new AlgoliaException("Search failed", e);
1059-
}
1045+
Response searchResults = getLocalIndex().search(query.build());
1046+
return OfflineClient.parseSearchResults(searchResults);
10601047
}
10611048

10621049
// ----------------------------------------------------------------------
@@ -1233,21 +1220,8 @@ protected JSONObject run() throws AlgoliaException {
12331220

12341221
private JSONObject _browseMirror(@NonNull Query query) throws AlgoliaException
12351222
{
1236-
try {
1237-
Response searchResults = getLocalIndex().browse(query.build());
1238-
if (searchResults.getStatusCode() == 200) {
1239-
String jsonString = new String(searchResults.getData(), "UTF-8");
1240-
JSONObject json = new JSONObject(jsonString);
1241-
// NOTE: Origin tagging performed by the SDK.
1242-
return json;
1243-
}
1244-
else {
1245-
throw new AlgoliaException(searchResults.getErrorMessage(), searchResults.getStatusCode());
1246-
}
1247-
}
1248-
catch (JSONException | UnsupportedEncodingException e) {
1249-
throw new AlgoliaException("Search failed", e);
1250-
}
1223+
Response searchResults = getLocalIndex().browse(query.build());
1224+
return OfflineClient.parseSearchResults(searchResults);
12511225
}
12521226

12531227
// ----------------------------------------------------------------------

algoliasearch/src/offline/java/com/algolia/search/saas/OfflineClient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,19 @@ private JSONObject moveIndexOfflineSync(final @NonNull String srcIndexName, fina
419419
static protected JSONObject parseSearchResults(Response searchResults) throws AlgoliaException {
420420
try {
421421
if (searchResults.getStatusCode() == 200) {
422-
String jsonString = new String(searchResults.getData(), "UTF-8");
423-
return new JSONObject(jsonString);
422+
if (searchResults.getData() != null) {
423+
String jsonString = new String(searchResults.getData(), "UTF-8");
424+
return new JSONObject(jsonString);
425+
} else { // may happen when building: no output
426+
return new JSONObject();
427+
}
424428
}
425429
else {
426430
throw new AlgoliaException(searchResults.getErrorMessage(), searchResults.getStatusCode());
427431
}
428432
}
429433
catch (JSONException | UnsupportedEncodingException e) {
430-
throw new AlgoliaException("Invalid JSON", e);
434+
throw new AlgoliaException("Offline Core returned invalid JSON", e);
431435
}
432436
}
433437
}

algoliasearch/src/offline/java/com/algolia/search/saas/OfflineIndex.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.util.Log;
3131

3232
import com.algolia.search.offline.core.LocalIndex;
33+
import com.algolia.search.offline.core.Response;
3334
import com.algolia.search.saas.helpers.DisjunctiveFaceting;
3435

3536
import org.json.JSONArray;
@@ -786,15 +787,13 @@ private void doCommit() throws AlgoliaException {
786787
if (finished) throw new IllegalStateException();
787788
try {
788789
flushObjectsToDisk(true);
789-
int statusCode = OfflineIndex.this.localIndex.build(
790+
Response result = OfflineIndex.this.localIndex.build(
790791
settingsFile != null ? settingsFile.getAbsolutePath() : null,
791792
objectFilePaths.toArray(new String[objectFilePaths.size()]),
792793
shouldClearIndex,
793794
deletedObjectIDs.toArray(new String[deletedObjectIDs.size()])
794795
);
795-
if (statusCode != 200) {
796-
throw new AlgoliaException("Error building index", statusCode);
797-
}
796+
OfflineClient.parseSearchResults(result);
798797
} finally {
799798
finished = true;
800799
}
@@ -955,11 +954,8 @@ private JSONObject _build(@NonNull File settingsFile, @NonNull File... objectFil
955954
for (int i = 0; i < objectFiles.length; ++i) {
956955
objectFilePaths[i] = objectFiles[i].getAbsolutePath();
957956
}
958-
final int status = localIndex.build(settingsFile.getAbsolutePath(), objectFilePaths, true /* clearIndex */, null /* deletedObjectIDs */);
959-
if (status != 200) {
960-
throw new AlgoliaException(String.format("Failed to build local index \"%s\"", OfflineIndex.this.getName()), status);
961-
}
962-
return new JSONObject();
957+
final Response result = localIndex.build(settingsFile.getAbsolutePath(), objectFilePaths, true /* clearIndex */, null /* deletedObjectIDs */);
958+
return OfflineClient.parseSearchResults(result);
963959
}
964960

965961
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)