Skip to content

Commit

Permalink
Expose an option in createIndex to suppress the 409 conflict error (#125
Browse files Browse the repository at this point in the history
)
  • Loading branch information
glody007 authored Sep 23, 2023
1 parent 699b5dd commit 4f93203
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/control/__tests__/createIndex.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createIndex } from '../createIndex';
import {
PineconeBadRequestError,
PineconeConflictError,
PineconeInternalServerError,
} from '../../errors';
import { IndexOperationsApi } from '../../pinecone-generated-ts-fetch';
Expand Down Expand Up @@ -166,5 +167,20 @@ describe('createIndex', () => {
await expect(toThrow).rejects.toThrow(PineconeBadRequestError);
await expect(toThrow).rejects.toThrow(serverError);
});

test('when 409 occurs', async () => {
const IOA = setupCreateIndexResponse(
{ status: 409, text: () => 'conflict error message' },
undefined,
false
);

const toThrow = async () => {
const createIndexFn = createIndex(IOA);
await createIndexFn({ name: 'index-name', dimension: 10 });
};

await expect(toThrow).rejects.toThrow(PineconeConflictError);
});
});
});
5 changes: 5 additions & 0 deletions src/control/createIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type CreateIndexOptions = {
metadataConfig?: { indexed: Array<string> };
sourceCollection?: string;
waitUntilReady?: boolean;
suppressConflicts?: boolean;
};

const CreateIndexOptionsSchema = Type.Object(
Expand All @@ -39,6 +40,7 @@ const CreateIndexOptionsSchema = Type.Object(
metadataConfig: Type.Optional(MetadataConfigSchema),
sourceCollection: Type.Optional(CollectionNameSchema),
waitUntilReady: Type.Optional(Type.Boolean()),
suppressConflicts: Type.Optional(Type.Boolean()),
},
{ additionalProperties: false }
);
Expand All @@ -60,6 +62,9 @@ export const createIndex = (api: IndexOperationsApi) => {
return;
} catch (e) {
const err = await handleIndexRequestError(e, api, options.name);
if (options.suppressConflicts && err.name === 'PineconeConflictError') {
return;
}
throw err;
}
};
Expand Down
19 changes: 19 additions & 0 deletions src/errors/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ export class PineconeNotFoundError extends BasePineconeError {
}
}

export class PineconeConflictError extends BasePineconeError {
constructor(failedRequest: FailedRequestInfo) {
const { url, message } = failedRequest;
if (url) {
super(
`A call to ${url} returned HTTP status 409. ${message ? message : ''}`
);
} else if (message) {
super(message);
} else {
super();
}

this.name = 'PineconeConflictError';
}
}

export class PineconeInternalServerError extends BasePineconeError {
constructor(failedRequest: FailedRequestInfo) {
const { url, body } = failedRequest;
Expand Down Expand Up @@ -101,6 +118,8 @@ export const mapHttpStatusError = (failedRequestInfo: FailedRequestInfo) => {
return new PineconeAuthorizationError(failedRequestInfo);
case 404:
return new PineconeNotFoundError(failedRequestInfo);
case 409:
return new PineconeConflictError(failedRequestInfo);
case 500:
return new PineconeInternalServerError(failedRequestInfo);
case 501:
Expand Down

0 comments on commit 4f93203

Please sign in to comment.