Skip to content

Commit 7f7e2a9

Browse files
authored
Improve Vector constructor error message (#1343)
1 parent 7e95dc8 commit 7f7e2a9

File tree

3 files changed

+7
-38
lines changed

3 files changed

+7
-38
lines changed

packages/core/src/vector.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,8 @@ export default class Vector<K extends Float32Array | Float64Array | Int8Array |
6161
this._type = vectorTypes.FLOAT32
6262
} else if (typedArray instanceof Float64Array) {
6363
this._type = vectorTypes.FLOAT64
64-
} else if (
65-
typedArray instanceof Uint8Array ||
66-
typedArray instanceof Uint16Array ||
67-
typedArray instanceof Uint32Array ||
68-
typedArray instanceof BigUint64Array
69-
) {
70-
throw newError('The neo4j Vector class does not support Unsigned Integer Arrays, please use a signed IntArray')
7164
} else {
72-
// @ts-expect-error
73-
throw newError(`The neo4j Vector class is a wrapper for TypedArrays. got ${(typedArray.toString() as string)}`)
65+
throw newError(`Invalid argument type passed to Vector constructor: should be signed integer or float TypedArray, got: ${(typedArray as any)?.constructor?.name as string ?? 'undefined or type without constructor name'}`)
7466
}
7567
this._typedArray = typedArray
7668
}
@@ -132,15 +124,7 @@ export function vector<K extends Float32Array | Float64Array | Int8Array | Int16
132124
try {
133125
return new Vector(typedArray)
134126
} catch {
135-
if (
136-
typedArray instanceof Uint8Array ||
137-
typedArray instanceof Uint16Array ||
138-
typedArray instanceof Uint32Array ||
139-
typedArray instanceof BigUint64Array
140-
) {
141-
throw newError('Invalid argument type passed to vector constructor function: should be signed integer or float TypedArray, got unsigned integer TypedArray')
142-
}
143-
throw newError(`Invalid argument type passed to vector constructor function: should be TypedArray, got ${typedArray.toString()}`)
127+
throw newError(`Invalid argument type passed to vector constructor function: should be signed integer or float TypedArray, got: ${typedArray?.constructor?.name ?? 'undefined or type without constructor name'}`)
144128
}
145129
}
146130

packages/core/test/vector-type.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ describe('Vector', () => {
3333
})
3434

3535
it.each([
36-
['array', [], 'should be TypedArray, got'],
37-
['Unsigned TypedArray', Uint16Array.from([]), 'should be signed integer or float TypedArray, got unsigned integer TypedArray']
36+
['array', [], 'Invalid argument type passed to vector constructor function: should be signed integer or float TypedArray, got: Array'],
37+
['Unsigned TypedArray', Uint16Array.from([]), 'Invalid argument type passed to vector constructor function: should be signed integer or float TypedArray, got: Uint16Array'],
38+
['undefined', undefined, 'Invalid argument type passed to vector constructor function: should be signed integer or float TypedArray, got: undefined or type without constructor name']
3839
])('should fail to create create vector from (%s)', (_, typedArray, expectedMessage) => {
3940
// @ts-expect-error
4041
expect(() => vector(typedArray)).toThrow(expectedMessage)

packages/neo4j-driver-deno/lib/core/vector.ts

Lines changed: 2 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)