Skip to content

Commit e1f9c8f

Browse files
committed
Simplify error handler code
1 parent b28c6bf commit e1f9c8f

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/errors/http-error-handler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ 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 {
99
// If it is not possible to parse the return body it means there is none
1010
// In which case it is a communication error with the Meilisearch instance
11-
err = await response.json()
11+
responseBody = await response.json()
1212
} catch (e: any) {
1313
// Not sure on how to test this part of the code.
1414
throw new MeiliSearchCommunicationError(
@@ -19,8 +19,9 @@ async function httpResponseErrorHandler(response: Response): Promise<Response> {
1919
}
2020
// If the body is parsable, then it means Meilisearch returned a body with
2121
// information on the error.
22-
throw new MeiliSearchApiError(err, response.status)
22+
throw new MeiliSearchApiError(responseBody, response.status)
2323
}
24+
2425
return response
2526
}
2627

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(() => null)
71+
72+
return parsedBody
7873
} catch (e: any) {
7974
const stack = e.stack
8075
httpErrorHandler(e, stack, constructURL.toString())

0 commit comments

Comments
 (0)