Skip to content

Commit 671a2e5

Browse files
authored
Merge pull request #1081 from meilisearch/update_error_codes
Update fields in MeiliSearchApiError
2 parents 9d9e100 + e28f4f5 commit 671a2e5

22 files changed

+286
-238
lines changed

src/errors/meilisearch-api-error.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
import {
2-
MSApiError,
3-
MeiliSearchApiErrorResponse,
4-
MSApiErrorConstructor,
5-
} from '../types'
1+
import { MeiliSearchErrorInterface } from '../types'
62

7-
const MeiliSearchApiError: MSApiErrorConstructor = class
8-
extends Error
9-
implements MSApiError {
3+
const MeiliSearchApiError = class extends Error {
104
httpStatus: number
11-
response?: MeiliSearchApiErrorResponse
12-
errorCode?: string
13-
errorType?: string
14-
errorLink?: string
5+
code?: string
6+
link?: string
157
stack?: string
16-
type: string
8+
type?: string
179

18-
constructor(error: MSApiError, status: number) {
10+
constructor(error: MeiliSearchErrorInterface, status: number) {
1911
super(error.message)
20-
this.type = 'MeiliSearchApiError'
2112
this.name = 'MeiliSearchApiError'
2213

23-
this.errorCode = error.errorCode
24-
this.errorType = error.errorType
25-
this.errorLink = error.errorLink
14+
this.code = error.code
15+
this.type = error.type
16+
this.link = error.link
2617
this.message = error.message
2718
this.httpStatus = status
2819

src/lib/indexes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
SearchableAttributes,
3535
DisplayedAttributes,
3636
WaitForPendingUpdateOptions,
37+
ErrorStatusCode,
3738
} from '../types'
3839
import { sleep, removeUndefinedFromObject } from './utils'
3940
import { HttpRequests } from './http-requests'
@@ -256,7 +257,7 @@ class Index<T = Record<string, any>> {
256257
await this.delete()
257258
return true
258259
} catch (e) {
259-
if (e.errorCode === 'index_not_found') {
260+
if (e.code === ErrorStatusCode.INDEX_NOT_FOUND) {
260261
return false
261262
}
262263
throw e

src/lib/meilisearch.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
'use strict'
99

1010
import { Index } from './indexes'
11-
import { MeiliSearchApiError, MeiliSearchCommunicationError } from '../errors'
1211
import {
1312
Config,
1413
IndexOptions,
@@ -18,6 +17,7 @@ import {
1817
Stats,
1918
Version,
2019
EnqueuedDump,
20+
ErrorStatusCode,
2121
} from '../types'
2222
import { HttpRequests } from './http-requests'
2323
import { addProtocolIfNotPresent } from './utils'
@@ -91,15 +91,9 @@ class MeiliSearch {
9191
const index = await this.getIndex(uid)
9292
return index
9393
} catch (e) {
94-
if (e.errorCode === 'index_not_found') {
94+
if (e.code === ErrorStatusCode.INDEX_NOT_FOUND) {
9595
return this.createIndex(uid, options)
9696
}
97-
if (e.type !== 'MeiliSearchCommunicationError') {
98-
throw new MeiliSearchApiError(e, e.status)
99-
}
100-
if (e.type === 'MeiliSearchCommunicationError') {
101-
throw new MeiliSearchCommunicationError(e.message, e, e.stack)
102-
}
10397
throw e
10498
}
10599
}
@@ -170,7 +164,7 @@ class MeiliSearch {
170164
await this.deleteIndex(uid)
171165
return true
172166
} catch (e) {
173-
if (e.errorCode === 'index_not_found') {
167+
if (e.code === ErrorStatusCode.INDEX_NOT_FOUND) {
174168
return false
175169
}
176170
throw e

src/types/types.ts

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export type Update = {
174174
duration: number
175175
enqueuedAt: string
176176
processedAt: string
177+
error?: MeiliSearchErrorBody
177178
}
178179

179180
export type EnqueuedDump = {
@@ -236,22 +237,11 @@ export type Version = {
236237
** ERROR HANDLER
237238
*/
238239

239-
export type MSApiErrorConstructor = new (
240-
error: MSApiError,
241-
status: number
242-
) => void
243-
244-
export type MeiliSearchApiErrorResponse = {
245-
status?: number
246-
statusText?: string
247-
path?: string
248-
method?: string
249-
body?: object
250-
}
251-
export type MeiliSearchApiErrorRequest = {
252-
url?: string
253-
path?: string
254-
method?: string
240+
export interface MeiliSearchErrorInterface extends Error {
241+
code?: string
242+
link?: string
243+
stack?: string
244+
type?: string
255245
}
256246

257247
export interface FetchError extends Error {
@@ -260,14 +250,11 @@ export interface FetchError extends Error {
260250
code: string
261251
}
262252

263-
export interface MSApiError extends Error {
264-
name: string
253+
export type MeiliSearchErrorBody = {
254+
code: string
255+
link: string
265256
message: string
266-
stack?: string
267-
httpStatus: number
268-
errorCode?: string
269-
errorType?: string
270-
errorLink?: string
257+
type: string
271258
}
272259

273260
export const enum ErrorStatusCode {
@@ -289,26 +276,56 @@ export const enum ErrorStatusCode {
289276
/** @see https://docs.meilisearch.com/errors/#invalid_state */
290277
INVALID_STATE = 'invalid_state',
291278

292-
/** @see https://docs.meilisearch.com/errors/#missing_primary_key */
293-
MISSING_PRIMARY_KEY = 'missing_primary_key',
279+
/** @see https://docs.meilisearch.com/errors/#primary_key_inference_failed */
280+
PRIMARY_KEY_INFERENCE_FAILED = 'primary_key_inference_failed',
294281

295-
/** @see https://docs.meilisearch.com/errors/#primary_key_already_present */
296-
PRIMARY_KEY_ALREADY_PRESENT = 'primary_key_already_present',
282+
/** @see https://docs.meilisearch.com/errors/#index_primary_key_already_exists */
283+
INDEX_PRIMARY_KEY_ALREADY_EXISTS = 'index_primary_key_already_exists',
297284

298285
/** @see https://docs.meilisearch.com/errors/#max_fields_limit_exceeded */
299-
MAX_FIELDS_LIMIT_EXCEEDED = 'max_fields_limit_exceeded',
286+
DOCUMENTS_FIELDS_LIMIT_REACHED = 'document_fields_limit_reached',
300287

301288
/** @see https://docs.meilisearch.com/errors/#missing_document_id */
302289
MISSING_DOCUMENT_ID = 'missing_document_id',
303290

304-
/** @see https://docs.meilisearch.com/errors/#invalid_facet */
305-
INVALID_FACET = 'invalid_facet',
291+
/** @see https://docs.meilisearch.com/errors/#missing_document_id */
292+
INVALID_DOCUMENT_ID = 'invalid_document_id',
293+
294+
/** @see https://docs.meilisearch.com/errors/#invalid_content_type */
295+
INVALID_CONTENT_TYPE = 'invalid_content_type',
296+
297+
/** @see https://docs.meilisearch.com/errors/#missing_content_type */
298+
MISSING_CONTENT_TYPE = 'missing_content_type',
299+
300+
/** @see https://docs.meilisearch.com/errors/#payload_too_large */
301+
PAYLOAD_TOO_LARGE = 'payload_too_large',
302+
303+
/** @see https://docs.meilisearch.com/errors/#missing_payload */
304+
MISSING_PAYLOAD = 'missing_payload',
305+
306+
/** @see https://docs.meilisearch.com/errors/#malformed_payload */
307+
MALFORMED_PAYLOAD = 'malformed_payload',
308+
309+
/** @see https://docs.meilisearch.com/errors/#no_space_left_on_device */
310+
NO_SPACE_LEFT_ON_DEVICE = 'no_space_left_on_device',
311+
312+
/** @see https://docs.meilisearch.com/errors/#invalid_store_file */
313+
INVALID_STORE_FILE = 'invalid_store_file',
314+
315+
/** @see https://docs.meilisearch.com/errors/#invalid_ranking_rules */
316+
INVALID_RANKING_RULES = 'missing_document_id',
317+
318+
/** @see https://docs.meilisearch.com/errors/#invalid_request */
319+
INVALID_REQUEST = 'invalid_request',
306320

307321
/** @see https://docs.meilisearch.com/errors/#invalid_filter */
308322
INVALID_FILTER = 'invalid_filter',
309323

310-
/** @see https://docs.meilisearch.com/errors/#bad_parameter */
311-
BAD_PARAMETER = 'bad_parameter',
324+
/** @see https://docs.meilisearch.com/errors/#invalid_sort */
325+
INVALID_SORT = 'invalid_sort',
326+
327+
/** @see https://docs.meilisearch.com/errors/#invalid_geo_field */
328+
INVALID_GEO_FIELD = 'invalid_geo_field',
312329

313330
/** @see https://docs.meilisearch.com/errors/#bad_request */
314331
BAD_REQUEST = 'bad_request',
@@ -319,36 +336,27 @@ export const enum ErrorStatusCode {
319336
/** @see https://docs.meilisearch.com/errors/#internal */
320337
INTERNAL = 'internal',
321338

322-
/** @see https://docs.meilisearch.com/errors/#invalid_token */
323-
INVALID_TOKEN = 'invalid_token',
324-
325-
/** @see https://docs.meilisearch.com/errors/#maintenance */
326-
MAINTENANCE = 'maintenance',
339+
/** @see https://docs.meilisearch.com/errors/#invalid_api_key */
340+
INVALID_API_KEY = 'invalid_api_key',
327341

328342
/** @see https://docs.meilisearch.com/errors/#missing_authorization_header */
329343
MISSING_AUTHORIZATION_HEADER = 'missing_authorization_header',
330344

331-
/** @see https://docs.meilisearch.com/errors/#missing_header */
332-
MISSING_HEADER = 'missing_header',
333-
334-
/** @see https://docs.meilisearch.com/errors/#not_found */
335-
NOT_FOUND = 'not_found',
336-
337-
/** @see https://docs.meilisearch.com/errors/#payload_too_large */
338-
PAYLOAD_TOO_LARGE = 'payload_too_large',
339-
340345
/** @see https://docs.meilisearch.com/errors/#unretrievable_document */
341346
UNRETRIEVABLE_DOCUMENT = 'unretrievable_document',
342347

343-
/** @see https://docs.meilisearch.com/errors/#search_error */
344-
SEARCH_ERROR = 'search_error',
348+
/** @see https://docs.meilisearch.com/errors/#database_size_limit_reached */
349+
MAX_DATABASE_SIZE_LIMIT_REACHED = 'database_size_limit_reached',
345350

346-
/** @see https://docs.meilisearch.com/errors/#unsupported_media_type */
347-
UNSUPPORTED_MEDIA_TYPE = 'unsupported_media_type',
351+
/** @see https://docs.meilisearch.com/errors/#task_not_found */
352+
TASK_NOT_FOUND = 'task_not_found',
348353

349-
/** @see https://docs.meilisearch.com/errors/#dump_already_in_progress */
350-
DUMP_ALREADY_IN_PROGRESS = 'dump_already_in_progress',
354+
/** @see https://docs.meilisearch.com/errors/#dump_already_processing */
355+
DUMP_ALREADY_PROCESSING = 'dump_already_processing',
351356

352357
/** @see https://docs.meilisearch.com/errors/#dump_process_failed */
353358
DUMP_PROCESS_FAILED = 'dump_process_failed',
359+
360+
/** @see https://docs.meilisearch.com/errors/#dump_not_found */
361+
DUMP_NOT_FOUND = 'dump_not_found',
354362
}

0 commit comments

Comments
 (0)