Skip to content

Commit c0ca1ef

Browse files
committed
fix: allow expected code only for instances of WeaviateUnexpectedStatusCodeError
1 parent 53049e3 commit c0ca1ef

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/users/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WeaviateUnexpectedStatusCodeError } from '../errors.js';
12
import { ConnectionREST } from '../index.js';
23
import {
34
WeaviateUserTypeInternal as UserTypeInternal,
@@ -102,8 +103,14 @@ const users = (connection: ConnectionREST): Users => {
102103
const db = (connection: ConnectionREST): DBUsers => {
103104
const ns = namespacedUsers(connection);
104105

105-
const allowCode = (code: number): ((reason: any) => boolean) => {
106-
return (reason) => reason.code !== undefined && reason.code === code;
106+
/** expectCode returns true if the error contained an expected status code. */
107+
const expectCode = (code: number): ((_: any) => boolean) => {
108+
return (error) => {
109+
if (error instanceof WeaviateUnexpectedStatusCodeError) {
110+
return error.code === code;
111+
}
112+
throw error;
113+
}
107114
};
108115

109116
type APIKeyResponse = { apikey: string };
@@ -128,12 +135,12 @@ const db = (connection: ConnectionREST): DBUsers => {
128135
connection
129136
.postEmpty<null>(`/users/db/${userId}/activate`, null)
130137
.then(() => true)
131-
.catch(allowCode(409)),
138+
.catch(expectCode(409)),
132139
deactivate: (userId: string) =>
133140
connection
134141
.postEmpty<null>(`/users/db/${userId}/deactivate`, null)
135142
.then(() => true)
136-
.catch(allowCode(409)),
143+
.catch(expectCode(409)),
137144
byName: (userId: string) => connection.get<WeaviateDBUser>(`/users/db/${userId}`, true).then(Map.dbUser),
138145
listAll: () => connection.get<WeaviateDBUser[]>('/users/db', true).then(Map.dbUsers),
139146
};

0 commit comments

Comments
 (0)