Skip to content

Commit

Permalink
add a test on the hint message when getDocuments uses an unknown route
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed May 25, 2023
1 parent 995b572 commit 7f927d0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
es2020: true,
'jest/globals': true,
node: true,
jasmine: true,
},
extends: [
'eslint:recommended',
Expand Down
6 changes: 5 additions & 1 deletion src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
MeiliSearchError,
MeiliSearchCommunicationError,
versionErrorHintMessage,
MeiliSearchApiError,
} from './errors'
import {
Config,
Expand Down Expand Up @@ -317,16 +318,19 @@ class Index<T extends Record<string, any> = Record<string, any>> {
parameters = removeUndefinedFromObject(parameters)

// In case `filter` is provided, use `POST /documents/fetch`
if (parameters.filter) {
if (parameters.filter !== undefined) {
try {
const url = `indexes/${this.uid}/documents/fetch`

return await this.httpRequest.post<
DocumentsQuery,
Promise<ResourceResults<D[]>>
>(url, parameters)
} catch (e) {
if (e instanceof MeiliSearchCommunicationError) {
e.message = versionErrorHintMessage(e.message, 'getDocuments')
} else if (e instanceof MeiliSearchApiError) {
e.message = versionErrorHintMessage(e.message, 'getDocuments')
}

throw e
Expand Down
6 changes: 6 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ export const enum ErrorStatusCode {
/** @see https://docs.meilisearch.com/errors/#invalid_document_offset */
INVALID_DOCUMENT_OFFSET = 'invalid_document_offset',

/** @see https://docs.meilisearch.com/errors/#invalid_document_offset */
INVALID_DOCUMENT_FILTER = 'invalid_document_filter',

/** @see https://docs.meilisearch.com/errors/#invalid_document_offset */
MISSING_DOCUMENT_FILTER = 'missing_document_filter',

/** @see https://docs.meilisearch.com/errors/#payload_too_large */
PAYLOAD_TOO_LARGE = 'payload_too_large',

Expand Down
38 changes: 38 additions & 0 deletions tests/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
getClient,
dataset,
Book,
getKey,
HOST,
} from './utils/meilisearch-test-utils'

const indexNoPk = {
Expand Down Expand Up @@ -155,6 +157,42 @@ describe('Documents tests', () => {
expect(documents.results.length).toEqual(2)
})

test(`${permission} key: Get documents should trigger error with a MeilisearchCommunicationError`, async () => {
const apiKey = await getKey(permission)
const client = new MeiliSearch({ host: `${HOST}/indexes`, apiKey })

try {
await client.index(indexPk.uid).getDocuments({ filter: '' })

fail(
'getDocuments should have raised an error when the route does not exist'
)
} catch (e: any) {
expect(e.message).toEqual(
"Not Found\nHint: It might not be working because maybe you're not up to date with the Meilisearch version that getDocuments call requires."
)
}
})

test(`${permission} key: Get documents should trigger error with a hint on a MeilisearchApiError`, async () => {
const apiKey = await getKey(permission)
const client = new MeiliSearch({ host: `${HOST}`, apiKey })

try {
await client.index(indexPk.uid).getDocuments({ filter: 'id = 1' })

fail(
'getDocuments should have raised an error when the route does not exist'
)
} catch (e: any) {
expect(e.message).toEqual(
`Attribute \`id\` is not filterable. This index does not have configured filterable attributes.
1:3 id = 1
Hint: It might not be working because maybe you're not up to date with the Meilisearch version that getDocuments call requires.`
)
}
})

test(`${permission} key: Get documents from index that has NO primary key`, async () => {
const client = await getClient(permission)
const { taskUid } = await client
Expand Down

0 comments on commit 7f927d0

Please sign in to comment.