Skip to content

Commit

Permalink
fix(api.js): fix http.fetch throwing error if response body is empty,
Browse files Browse the repository at this point in the history
closes #2831 (#3008)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog committed Dec 9, 2021
1 parent c664e9d commit 50c6390
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/api-fetch-empty-reponse.md
@@ -0,0 +1,5 @@
---
"api": patch
---

Fix `http.fetch` throwing error if the response is successful but the body is empty.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion tooling/api/src/http.ts
Expand Up @@ -203,7 +203,10 @@ class Client {
// @ts-expect-error
response.data = JSON.parse(response.data as string)
} catch (e) {
if (response.ok) {
if (response.ok && (response.data as unknown as string) === '') {
// @ts-expect-error
response.data = {}
} else if (response.ok) {
throw Error(
`Failed to parse response \`${response.data}\` as JSON: ${e};
try setting the \`responseType\` option to \`ResponseType.Text\` or \`ResponseType.Binary\` if the API does not return a JSON response.`
Expand Down

0 comments on commit 50c6390

Please sign in to comment.