Skip to content
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

Expose an option in createIndex to suppress the 409 conflict error #125

Merged
merged 7 commits into from
Sep 23, 2023
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
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
Loading