Skip to content

Commit 3e8ce2c

Browse files
bors[bot]bidoubiwa
andauthored
Merge #1077
1077: Fix unknown catch r=bidoubiwa a=bidoubiwa fixes: #1076 - Remove unecessary type complexcity in MeiliSearchApiError - Add explicit type comparison when Error are used for a specific purpose - Fix tests and all occurrences of catch by adding an explicit any type Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 081b3cd + 0208abb commit 3e8ce2c

File tree

13 files changed

+41
-41
lines changed

13 files changed

+41
-41
lines changed

src/errors/http-error-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function httpResponseErrorHandler(response: Response): Promise<Response> {
77
let err
88
try {
99
err = await response.json()
10-
} catch (e) {
10+
} catch (e: any) {
1111
throw new MeiliSearchCommunicationError(
1212
response.statusText,
1313
response,

src/errors/meilisearch-api-error.ts

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

7-
const MeiliSearchApiError: MSApiErrorConstructor = class
8-
extends Error
9-
implements MSApiError {
3+
const MeiliSearchApiError = class extends Error implements MSApiError {
104
httpStatus: number
115
response?: MeiliSearchApiErrorResponse
126
errorCode?: string
@@ -25,6 +19,8 @@ const MeiliSearchApiError: MSApiErrorConstructor = class
2519
this.errorLink = error.errorLink
2620
this.message = error.message
2721
this.httpStatus = status
22+
// Make errors comparison possible. ex: error instanceof MeiliSearchApiError.
23+
Object.setPrototypeOf(this, MeiliSearchApiError.prototype)
2824

2925
if (Error.captureStackTrace) {
3026
Error.captureStackTrace(this, MeiliSearchApiError)

src/errors/meilisearch-communication-error.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class MeiliSearchCommunicationError extends Error {
3434
'Failed to fetch',
3535
`request to ${url} failed, reason: connect ECONNREFUSED`
3636
)
37+
this.stack = this.stack?.replace('Not Found', `Not Found: ${url}`)
3738
} else {
3839
if (Error.captureStackTrace) {
3940
Error.captureStackTrace(this, MeiliSearchCommunicationError)

src/lib/http-requests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class HttpRequests {
6767
} catch (_) {
6868
return
6969
}
70-
} catch (e) {
70+
} catch (e: any) {
7171
const stack = e.stack
7272
httpErrorHandler(e, stack, constructURL.toString())
7373
}

src/lib/indexes.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
'use strict'
99

10-
import { MeiliSearchTimeOutError, MeiliSearchError } from '../errors'
10+
import {
11+
MeiliSearchTimeOutError,
12+
MeiliSearchError,
13+
MeiliSearchApiError,
14+
} from '../errors'
1115

1216
import {
1317
Config,
@@ -255,8 +259,11 @@ class Index<T = Record<string, any>> {
255259
try {
256260
await this.delete()
257261
return true
258-
} catch (e) {
259-
if (e.errorCode === 'index_not_found') {
262+
} catch (e: any) {
263+
if (
264+
e instanceof MeiliSearchApiError &&
265+
e.errorCode === 'index_not_found'
266+
) {
260267
return false
261268
}
262269
throw e

src/lib/meilisearch.ts

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

1010
import { Index } from './indexes'
11-
import { MeiliSearchApiError, MeiliSearchCommunicationError } from '../errors'
11+
import { MeiliSearchApiError } from '../errors'
1212
import {
1313
Config,
1414
IndexOptions,
@@ -90,16 +90,13 @@ class MeiliSearch {
9090
try {
9191
const index = await this.getIndex(uid)
9292
return index
93-
} catch (e) {
94-
if (e.errorCode === 'index_not_found') {
93+
} catch (e: any) {
94+
if (
95+
e instanceof MeiliSearchApiError &&
96+
e.errorCode === 'index_not_found'
97+
) {
9598
return this.createIndex(uid, options)
9699
}
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-
}
103100
throw e
104101
}
105102
}
@@ -170,7 +167,10 @@ class MeiliSearch {
170167
await this.deleteIndex(uid)
171168
return true
172169
} catch (e) {
173-
if (e.errorCode === 'index_not_found') {
170+
if (
171+
e instanceof MeiliSearchApiError &&
172+
e.errorCode === 'index_not_found'
173+
) {
174174
return false
175175
}
176176
throw e

src/types/types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,6 @@ export type Version = {
236236
** ERROR HANDLER
237237
*/
238238

239-
export type MSApiErrorConstructor = new (
240-
error: MSApiError,
241-
status: number
242-
) => void
243-
244239
export type MeiliSearchApiErrorResponse = {
245240
status?: number
246241
statusText?: string

tests/client_tests.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe.each([
9494
})
9595
const health = await client.isHealthy()
9696
expect(health).toBe(false) // Left here to trigger failed test if error is not thrown
97-
} catch (e) {
97+
} catch (e: any) {
9898
expect(e.message).toMatch(`${BAD_HOST}/api/health`)
9999
expect(e.type).toBe('MeiliSearchCommunicationError')
100100
}
@@ -109,7 +109,7 @@ describe.each([
109109
})
110110
const health = await client.isHealthy()
111111
expect(health).toBe(false) // Left here to trigger failed test if error is not thrown
112-
} catch (e) {
112+
} catch (e: any) {
113113
expect(e.message).toMatch(`${BAD_HOST}/api/health`)
114114
expect(e.type).toBe('MeiliSearchCommunicationError')
115115
}
@@ -124,7 +124,7 @@ describe.each([
124124
})
125125
const health = await client.isHealthy()
126126
expect(health).toBe(false) // Left here to trigger failed test if error is not thrown
127-
} catch (e) {
127+
} catch (e: any) {
128128
expect(e.message).toMatch(`${BAD_HOST}//health`)
129129
expect(e.type).toBe('MeiliSearchCommunicationError')
130130
}
@@ -139,7 +139,7 @@ describe.each([
139139
})
140140
const health = await client.isHealthy()
141141
expect(health).toBe(false) // Left here to trigger failed test if error is not thrown
142-
} catch (e) {
142+
} catch (e: any) {
143143
expect(e.message).toMatch(`${BAD_HOST}/health`)
144144
expect(e.type).toBe('MeiliSearchCommunicationError')
145145
}
@@ -149,7 +149,7 @@ describe.each([
149149
const client = new MeiliSearch({ host: 'http://localhost:9345' })
150150
try {
151151
await client.health()
152-
} catch (e) {
152+
} catch (e: any) {
153153
expect(e.type).toEqual('MeiliSearchCommunicationError')
154154
}
155155
})

tests/env/typescript-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"author": "Charlotte Vermandel",
1515
"license": "ISC",
1616
"devDependencies": {
17-
"typescript": "^4.1.3"
17+
"typescript": "4.4.4"
1818
},
1919
"dependencies": {}
2020
}

tests/env/typescript-node/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# yarn lockfile v1
33

44

5-
typescript@^4.1.3:
6-
version "4.1.3"
7-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
8-
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==
5+
typescript@4.4.4:
6+
version "4.4.4"
7+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.4.tgz#2cd01a1a1f160704d3101fd5a58ff0f9fcb8030c"
8+
integrity sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==

0 commit comments

Comments
 (0)