weaviate-client
Version:
JS/TS client for Weaviate
56 lines (55 loc) • 2.37 kB
JavaScript
import { isAbortError } from 'abort-controller-x';
import { ServerError, Status } from 'nice-grpc';
import { WeaviateInsufficientPermissionsError, WeaviateRequestTimeoutError } from '../errors.js';
import { ConsistencyLevel as ConsistencyLevelGRPC } from '../proto/v1/base.js';
export default class Base {
connection;
collection;
timeout;
consistencyLevel;
tenant;
metadata;
abortSignal;
constructor(connection, collection, metadata, timeout, consistencyLevel, tenant, abortSignal) {
this.connection = connection;
this.collection = collection;
this.metadata = metadata;
this.timeout = timeout;
this.consistencyLevel = this.mapConsistencyLevel(consistencyLevel);
this.tenant = tenant;
this.abortSignal = abortSignal;
}
mapConsistencyLevel(consistencyLevel) {
switch (consistencyLevel) {
case 'ALL':
return ConsistencyLevelGRPC.CONSISTENCY_LEVEL_ALL;
case 'QUORUM':
return ConsistencyLevelGRPC.CONSISTENCY_LEVEL_QUORUM;
case 'ONE':
return ConsistencyLevelGRPC.CONSISTENCY_LEVEL_ONE;
default:
return ConsistencyLevelGRPC.CONSISTENCY_LEVEL_UNSPECIFIED;
}
}
sendWithTimeout = (send, errMkr) => {
const controller = new AbortController();
const signal = this.abortSignal
? AbortSignal.any([controller.signal, this.abortSignal])
: controller.signal;
const timeoutId = setTimeout(() => controller.abort(), this.timeout * 1000);
return send(signal)
.catch((err) => {
if (err instanceof ServerError && err.code === Status.PERMISSION_DENIED) {
throw new WeaviateInsufficientPermissionsError(7, err.message);
}
if (isAbortError(err) && this.abortSignal === undefined) {
throw new WeaviateRequestTimeoutError(`timed out after ${this.timeout * 1000}ms`);
}
if (isAbortError(err) && this.abortSignal !== undefined) {
throw err; // if the error is an abort error caused by the caller's abort signal, we re-throw it so that the caller can handle it as they see fit
}
throw errMkr(err);
})
.finally(() => clearTimeout(timeoutId));
};
}