Skip to content

Commit 7f927d0

Browse files
committed
add a test on the hint message when getDocuments uses an unknown route
1 parent 995b572 commit 7f927d0

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
es2020: true,
66
'jest/globals': true,
77
node: true,
8+
jasmine: true,
89
},
910
extends: [
1011
'eslint:recommended',

src/indexes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
MeiliSearchError,
1212
MeiliSearchCommunicationError,
1313
versionErrorHintMessage,
14+
MeiliSearchApiError,
1415
} from './errors'
1516
import {
1617
Config,
@@ -317,16 +318,19 @@ class Index<T extends Record<string, any> = Record<string, any>> {
317318
parameters = removeUndefinedFromObject(parameters)
318319

319320
// In case `filter` is provided, use `POST /documents/fetch`
320-
if (parameters.filter) {
321+
if (parameters.filter !== undefined) {
321322
try {
322323
const url = `indexes/${this.uid}/documents/fetch`
324+
323325
return await this.httpRequest.post<
324326
DocumentsQuery,
325327
Promise<ResourceResults<D[]>>
326328
>(url, parameters)
327329
} catch (e) {
328330
if (e instanceof MeiliSearchCommunicationError) {
329331
e.message = versionErrorHintMessage(e.message, 'getDocuments')
332+
} else if (e instanceof MeiliSearchApiError) {
333+
e.message = versionErrorHintMessage(e.message, 'getDocuments')
330334
}
331335

332336
throw e

src/types/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@ export const enum ErrorStatusCode {
550550
/** @see https://docs.meilisearch.com/errors/#invalid_document_offset */
551551
INVALID_DOCUMENT_OFFSET = 'invalid_document_offset',
552552

553+
/** @see https://docs.meilisearch.com/errors/#invalid_document_offset */
554+
INVALID_DOCUMENT_FILTER = 'invalid_document_filter',
555+
556+
/** @see https://docs.meilisearch.com/errors/#invalid_document_offset */
557+
MISSING_DOCUMENT_FILTER = 'missing_document_filter',
558+
553559
/** @see https://docs.meilisearch.com/errors/#payload_too_large */
554560
PAYLOAD_TOO_LARGE = 'payload_too_large',
555561

tests/documents.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
getClient,
88
dataset,
99
Book,
10+
getKey,
11+
HOST,
1012
} from './utils/meilisearch-test-utils'
1113

1214
const indexNoPk = {
@@ -155,6 +157,42 @@ describe('Documents tests', () => {
155157
expect(documents.results.length).toEqual(2)
156158
})
157159

160+
test(`${permission} key: Get documents should trigger error with a MeilisearchCommunicationError`, async () => {
161+
const apiKey = await getKey(permission)
162+
const client = new MeiliSearch({ host: `${HOST}/indexes`, apiKey })
163+
164+
try {
165+
await client.index(indexPk.uid).getDocuments({ filter: '' })
166+
167+
fail(
168+
'getDocuments should have raised an error when the route does not exist'
169+
)
170+
} catch (e: any) {
171+
expect(e.message).toEqual(
172+
"Not Found\nHint: It might not be working because maybe you're not up to date with the Meilisearch version that getDocuments call requires."
173+
)
174+
}
175+
})
176+
177+
test(`${permission} key: Get documents should trigger error with a hint on a MeilisearchApiError`, async () => {
178+
const apiKey = await getKey(permission)
179+
const client = new MeiliSearch({ host: `${HOST}`, apiKey })
180+
181+
try {
182+
await client.index(indexPk.uid).getDocuments({ filter: 'id = 1' })
183+
184+
fail(
185+
'getDocuments should have raised an error when the route does not exist'
186+
)
187+
} catch (e: any) {
188+
expect(e.message).toEqual(
189+
`Attribute \`id\` is not filterable. This index does not have configured filterable attributes.
190+
1:3 id = 1
191+
Hint: It might not be working because maybe you're not up to date with the Meilisearch version that getDocuments call requires.`
192+
)
193+
}
194+
})
195+
158196
test(`${permission} key: Get documents from index that has NO primary key`, async () => {
159197
const client = await getClient(permission)
160198
const { taskUid } = await client

0 commit comments

Comments
 (0)