Skip to content

Commit c05a8d2

Browse files
Merge #1245
1245: Rework the custom Meilisearch Errors r=bidoubiwa a=bidoubiwa `MeiliSearchApiError` were labelled as `MeilisearchCommunicationError` this PR fixes this issue. It also removes the `type` field from other custom errors as it is not a standard field in Error in javascript. It also conflicts with the `type` field returned by an error in Meilisearch. - `MeilisearchCommunicationError` - `MeiliSearchTimeOutError` - `MeiliSearchError` `MeiliSearchErrorBody` is renamed `MeiliSearchErrorInfo` for clarity. It also remove the unnecessary interface `MeiliSearchErrorInterface` as interfaces are used to create new classes corresponding following an interface rule, but no other custom error is planned with this interface. The interface used for a parameter of the custom error is replaced with the type `MeiliSearchErrorInfo` used across the different types. Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 3ede368 + b479ccd commit c05a8d2

15 files changed

+487
-668
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ updates:
1717
- dependency-name: "*prettier*"
1818
- dependency-name: "lint-staged"
1919
- dependency-name: "nodemon"
20+
- dependency-name: "*jest*"

jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const config = {
1010
},
1111
},
1212
},
13-
setupFiles: [],
1413
watchPlugins: [
1514
'jest-watch-typeahead/filename',
1615
'jest-watch-typeahead/testname',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
"eslint-plugin-standard": "5",
9696
"gzip-size": "^6.0.0",
9797
"jest": "^26.6.3",
98+
"jest-fetch-mock": "^3.0.3",
9899
"jest-watch-typeahead": "^0.6.3",
99100
"kleur": "^4.1.4",
100101
"lint-staged": "11.1.2",

src/errors/http-error-handler.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ import { FetchError } from '../types'
44

55
async function httpResponseErrorHandler(response: Response): Promise<Response> {
66
if (!response.ok) {
7-
let err
7+
let responseBody
88
try {
9-
err = await response.json()
9+
// If it is not possible to parse the return body it means there is none
10+
// In which case it is a communication error with the Meilisearch instance
11+
responseBody = await response.json()
1012
} catch (e: any) {
13+
// Not sure on how to test this part of the code.
1114
throw new MeiliSearchCommunicationError(
1215
response.statusText,
1316
response,
1417
response.url
1518
)
1619
}
17-
throw new MeiliSearchApiError(err, response.status)
20+
// If the body is parsable, then it means Meilisearch returned a body with
21+
// information on the error.
22+
throw new MeiliSearchApiError(responseBody, response.status)
1823
}
24+
1925
return response
2026
}
2127

@@ -24,7 +30,7 @@ function httpErrorHandler(
2430
stack?: string,
2531
url?: string
2632
): Promise<void> {
27-
if (response.type !== 'MeiliSearchApiError') {
33+
if (response.name !== 'MeiliSearchApiError') {
2834
throw new MeiliSearchCommunicationError(
2935
response.message,
3036
response,

src/errors/meilisearch-api-error.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { MeiliSearchErrorInterface } from '../types'
1+
import { MeiliSearchErrorInfo } from '../types'
22

33
const MeiliSearchApiError = class extends Error {
44
httpStatus: number
5-
code?: string
6-
link?: string
5+
code: string
6+
link: string
7+
type: string
78
stack?: string
8-
type?: string
99

10-
constructor(error: MeiliSearchErrorInterface, status: number) {
10+
constructor(error: MeiliSearchErrorInfo, status: number) {
1111
super(error.message)
1212
this.name = 'MeiliSearchApiError'
1313

src/errors/meilisearch-communication-error.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'cross-fetch/polyfill'
22
import { FetchError } from '../types'
33

44
class MeiliSearchCommunicationError extends Error {
5-
type: string
65
statusCode?: number
76
errno?: string
87
code?: string
@@ -17,7 +16,6 @@ class MeiliSearchCommunicationError extends Error {
1716
super(message)
1817

1918
this.name = 'MeiliSearchCommunicationError'
20-
this.type = 'MeiliSearchCommunicationError'
2119

2220
if (body instanceof Response) {
2321
this.message = body.statusText

src/errors/meilisearch-error.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class MeiliSearchError extends Error {
2-
type: string
32
constructor(message: string) {
43
super(message)
54
this.name = 'MeiliSearchError'
6-
this.type = 'MeiliSearchError'
75

86
if (Error.captureStackTrace) {
97
Error.captureStackTrace(this, MeiliSearchError)

src/errors/meilisearch-timeout-error.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class MeiliSearchTimeOutError extends Error {
2-
type: string
32
constructor(message: string) {
43
super(message)
54
this.name = 'MeiliSearchTimeOutError'
6-
this.type = this.constructor.name
75

86
if (Error.captureStackTrace) {
97
Error.captureStackTrace(this, MeiliSearchTimeOutError)

src/lib/http-requests.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,9 @@ class HttpRequests {
6767
body: JSON.stringify(body),
6868
headers: this.headers,
6969
}).then((res) => httpResponseErrorHandler(res))
70-
const parsedBody: string = await response.text()
71-
72-
try {
73-
const parsedJson = JSON.parse(parsedBody)
74-
return parsedJson
75-
} catch (_) {
76-
return
77-
}
70+
const parsedBody = await response.json().catch(() => undefined)
71+
72+
return parsedBody
7873
} catch (e: any) {
7974
const stack = e.stack
8075
httpErrorHandler(e, stack, constructURL.toString())

src/types/types.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export type Task = {
244244
duration: string
245245
enqueuedAt: string
246246
processedAt: string
247-
error?: MeiliSearchErrorBody
247+
error?: MeiliSearchErrorInfo
248248
}
249249

250250
export type EnqueuedDump = {
@@ -319,20 +319,13 @@ export type Version = {
319319
** ERROR HANDLER
320320
*/
321321

322-
export interface MeiliSearchErrorInterface extends Error {
323-
code?: string
324-
link?: string
325-
stack?: string
326-
type?: string
327-
}
328-
329322
export interface FetchError extends Error {
330323
type: string
331324
errno: string
332325
code: string
333326
}
334327

335-
export type MeiliSearchErrorBody = {
328+
export type MeiliSearchErrorInfo = {
336329
code: string
337330
link: string
338331
message: string

0 commit comments

Comments
 (0)