Skip to content

Commit a05f24a

Browse files
committed
only delete in case of failure
1 parent fe93da5 commit a05f24a

File tree

13 files changed

+45
-33
lines changed

13 files changed

+45
-33
lines changed

clients/algoliasearch-client-csharp/algoliasearch/Utils/SearchClientExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,11 @@ await WaitForTaskAsync(tmpIndexName, moveResponse.TaskID, requestOptions: option
528528
BatchResponses = batchResponse
529529
};
530530
}
531-
finally
531+
catch (Exception ex)
532532
{
533533
await DeleteIndexAsync(tmpIndexName, cancellationToken: cancellationToken).ConfigureAwait(false);
534+
535+
throw ex;
534536
}
535537
}
536538

clients/algoliasearch-client-kotlin/client/src/commonMain/kotlin/com/algolia/client/extensions/SearchClient.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,10 @@ public suspend fun SearchClient.replaceAllObjects(
513513
waitForTask(indexName = tmpIndexName, taskID = move.taskID)
514514

515515
return ReplaceAllObjectsResponse(copy, batchResponses, move)
516-
} finally {
516+
} catch (e: Exception) {
517517
deleteIndex(tmpIndexName)
518+
519+
throw e
518520
}
519521
}
520522

clients/algoliasearch-client-scala/src/main/scala/algoliasearch/extension/package.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,12 @@ package object extension {
411411
batchResponses = batchResponses,
412412
moveOperationResponse = move
413413
)
414-
} finally {
415-
client.deleteIndex(tmpIndexName)
414+
} catch {
415+
case e : Throwable => {
416+
client.deleteIndex(tmpIndexName)
417+
418+
throw e
419+
}
416420
}
417421
}
418422

clients/algoliasearch-client-swift/Sources/Search/Extra/SearchClientExtension.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,6 @@ public extension SearchClient {
610610

611611
throw error
612612
}
613-
614-
_ = try? await self.deleteIndex(indexName: tmpIndexName)
615613
}
616614

617615
/// Generate a secured API key

scripts/cts/testServer/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import express from 'express';
66
import { createSpinner } from '../../spinners.js';
77
import type { CTSType } from '../runCts.js';
88

9+
import { expect } from 'chai';
910
import { apiKeyServer } from './apiKey.js';
1011
import { benchmarkServer } from './benchmark.js';
1112
import { chunkWrapperServer } from './chunkWrapper.js';
@@ -60,13 +61,13 @@ export async function setupServer(name: string, port: number, addRoutes: (app: E
6061
// 404 handler
6162
app.use((req, res) => {
6263
console.error(`[PORT ${port}] endpoint not implemented for`, req.method, req.url);
63-
res.status(404).json({ message: 'not found' });
64+
expect.fail('endpoint not implemented');
6465
});
6566

6667
// catch all error handler
6768
app.use((err, _req, res, _) => {
6869
console.error(err.message);
69-
res.status(500).send({ message: err.message });
70+
expect.fail(err.message);
7071
});
7172

7273
const server = await new Promise<Server>((resolve) => {

scripts/cts/testServer/replaceAllObjects.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const raoState: Record<
1414
waitTaskCount: number;
1515
tmpIndexName: string;
1616
waitingForFinalWaitTask: boolean;
17-
tmpIndexDeleted: boolean;
1817
successful: boolean;
1918
}
2019
> = {};
@@ -23,7 +22,6 @@ export function assertValidReplaceAllObjects(expectedCount: number): void {
2322
expect(Object.keys(raoState)).to.have.length(expectedCount);
2423
for (const lang in raoState) {
2524
expect(raoState[lang].successful).to.equal(true);
26-
expect(raoState[lang].tmpIndexDeleted).to.equal(true);
2725
}
2826
}
2927

@@ -52,7 +50,6 @@ function addRoutes(app: Express): void {
5250
waitTaskCount: 0,
5351
tmpIndexName: req.body.destination,
5452
waitingForFinalWaitTask: false,
55-
tmpIndexDeleted: false,
5653
successful: false,
5754
};
5855
} else {
@@ -71,7 +68,6 @@ function addRoutes(app: Express): void {
7168
waitTaskCount: 6,
7269
tmpIndexName: req.params.indexName,
7370
waitingForFinalWaitTask: false,
74-
tmpIndexDeleted: false,
7571
successful: false,
7672
});
7773

@@ -117,16 +113,6 @@ function addRoutes(app: Express): void {
117113

118114
res.json({ status: 'published', updatedAt: '2021-01-01T00:00:00.000Z' });
119115
});
120-
121-
app.delete('/1/indexes/:indexName', (req, res) => {
122-
const lang = req.params.indexName.match(/^cts_e2e_replace_all_objects_(.*)_tmp_\d+$/)?.[1] as string;
123-
expect(raoState).to.include.keys(lang);
124-
expect(raoState[lang].tmpIndexName).to.equal(req.params.indexName);
125-
126-
raoState[lang].tmpIndexDeleted = true;
127-
128-
res.json({ taskID: 456, deletedAt: '2021-01-01T00:00:00.000Z' });
129-
});
130116
}
131117

132118
export function replaceAllObjectsServer(): Promise<Server> {

scripts/cts/testServer/replaceAllObjectsFailed.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const raoState: Record<
1717
export function assertValidReplaceAllObjectsFailed(expectedCount: number): void {
1818
const count = Object.values(raoState).filter((s) => s.successful).length;
1919
if (count !== expectedCount) {
20+
console.log(JSON.stringify(raoState, null, 2));
2021
throw new Error(`Expected ${expectedCount} call to replaceAllObjectsFailed, got ${count} instead.`);
2122
}
2223
}
@@ -44,7 +45,7 @@ function addRoutes(app: Express): void {
4445
});
4546

4647
app.delete('/1/indexes/:indexName', (req, res) => {
47-
const lang = req.params.indexName.match(/^cts_e2e_replace_all_objects_too_big_(.*)_tmp/)?.[1] as string;
48+
const lang = req.params.indexName.match(/^cts_e2e_replace_all_objects_too_big_(.*)_tmp_\d+/)?.[1] as string;
4849
expect(raoState[lang].tmpIndexName).to.equal(req.params.indexName);
4950
raoState[lang].successful = true;
5051

templates/go/search_helpers.mustache

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,39 +657,47 @@ func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any
657657
return nil, err
658658
}
659659

660-
defer func() {
661-
_, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
662-
}()
663-
664660
opts = append(opts, WithWaitForTasks(true))
665661

666662
batchResp, err := c.ChunkedBatch(tmpIndexName, objects, ACTION_ADD_OBJECT, opts...)
667663
if err != nil {
664+
_, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
665+
668666
return nil, err
669667
}
670668

671669
_, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, toIterableOptions(opts)...)
672670
if err != nil {
671+
_, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
672+
673673
return nil, err
674674
}
675675

676676
copyResp, err = c.OperationIndex(c.NewApiOperationIndexRequest(indexName, NewOperationIndexParams(OPERATION_TYPE_COPY, tmpIndexName, WithOperationIndexParamsScope([]ScopeType{SCOPE_TYPE_SETTINGS, SCOPE_TYPE_RULES, SCOPE_TYPE_SYNONYMS}))), toRequestOptions(opts)...)
677677
if err != nil {
678+
_, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
679+
678680
return nil, err
679681
}
680682

681683
_, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, toIterableOptions(opts)...)
682684
if err != nil {
685+
_, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
686+
683687
return nil, err
684688
}
685689

686690
moveResp, err := c.OperationIndex(c.NewApiOperationIndexRequest(tmpIndexName, NewOperationIndexParams(OPERATION_TYPE_MOVE, indexName)), toRequestOptions(opts)...)
687691
if err != nil {
692+
_, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
693+
688694
return nil, err
689695
}
690696

691697
_, err = c.WaitForTask(tmpIndexName, moveResp.TaskID, toIterableOptions(opts)...)
692698
if err != nil {
699+
_, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
700+
693701
return nil, err
694702
}
695703

templates/java/api_helpers.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,8 +978,10 @@ public <T> ReplaceAllObjectsResponse replaceAllObjects(
978978
.setCopyOperationResponse(copyOperationResponse)
979979
.setBatchResponses(batchResponses)
980980
.setMoveOperationResponse(moveOperationResponse);
981-
} finally {
981+
} catch (Exception e) {
982982
deleteIndex(tmpIndexName);
983+
984+
throw e;
983985
}
984986
}
985987

templates/javascript/clients/client/api/helpers.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,10 @@ async replaceAllObjects(
459459
});
460460

461461
return { copyOperationResponse, batchResponses, moveOperationResponse };
462-
} finally {
462+
} catch (error) {
463463
await this.deleteIndex({ indexName: tmpIndexName });
464+
465+
throw error;
464466
}
465467
},
466468

templates/php/api.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,10 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
481481
'batchResponses' => $batchResponses,
482482
'moveOperationResponse' => $moveOperationResponse,
483483
];
484-
} finally {
484+
} catch (\Throwable $e) {
485485
$this->deleteIndex($tmpIndexName);
486+
487+
throw $e;
486488
}
487489
}
488490

@@ -636,7 +638,7 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
636638
$this->getSettings($indexName);
637639
} catch (NotFoundException $e) {
638640
return false;
639-
} catch (Exception $e) {
641+
} catch (\Throwable $e) {
640642
throw $e;
641643
}
642644

templates/python/search_helpers.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,11 @@
417417
batch_responses=batch_responses,
418418
move_operation_response=move_operation_response,
419419
)
420-
finally:
420+
except Exception as e:
421421
{{^isSyncClient}}await {{/isSyncClient}}self.delete_index(tmp_index_name)
422422
423+
raise e
424+
423425
{{^isSyncClient}}async {{/isSyncClient}}def index_exists(self, index_name: str) -> bool:
424426
"""
425427
Helper: Checks if the given `index_name` exists.

templates/ruby/search_helpers.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,10 @@ def replace_all_objects(index_name, objects, batch_size = 1000, request_options
405405
batch_responses: batch_responses,
406406
move_operation_response: move_operation_response
407407
)
408-
ensure
408+
rescue Exception => e
409409
delete_index(tmp_index_name)
410+
411+
raise e
410412
end
411413
end
412414

0 commit comments

Comments
 (0)