浏览代码

fix: json parsing error when using extension (HOPP-110)

Andrew Bastin 4 年之前
父节点
当前提交
68aa54bdb7
共有 1 个文件被更改,包括 19 次插入6 次删除
  1. 19 6
      packages/hoppscotch-app/helpers/RequestRunner.ts

+ 19 - 6
packages/hoppscotch-app/helpers/RequestRunner.ts

@@ -1,9 +1,10 @@
 import { Observable } from "rxjs"
 import { Observable } from "rxjs"
 import { filter } from "rxjs/operators"
 import { filter } from "rxjs/operators"
 import { chain, right, TaskEither } from "fp-ts/lib/TaskEither"
 import { chain, right, TaskEither } from "fp-ts/lib/TaskEither"
-import { pipe } from "fp-ts/lib/function"
+import { pipe } from "fp-ts/function"
+import * as O from "fp-ts/Option"
 import { runTestScript, TestDescriptor } from "@hoppscotch/js-sandbox"
 import { runTestScript, TestDescriptor } from "@hoppscotch/js-sandbox"
-import { isRight } from "fp-ts/lib/Either"
+import { isRight } from "fp-ts/Either"
 import {
 import {
   getCombinedEnvVariables,
   getCombinedEnvVariables,
   getFinalEnvsFromPreRequest,
   getFinalEnvsFromPreRequest,
@@ -20,12 +21,24 @@ const getTestableBody = (res: HoppRESTResponse & { type: "success" }) => {
     (h) => h.key.toLowerCase() === "content-type"
     (h) => h.key.toLowerCase() === "content-type"
   )
   )
 
 
-  const rawBody = new TextDecoder("utf-8").decode(res.body)
+  const rawBody = new TextDecoder("utf-8")
+    .decode(res.body)
+    .replaceAll("\x00", "")
 
 
-  if (!contentTypeHeader || !isJSONContentType(contentTypeHeader.value))
-    return rawBody
+  const x = pipe(
+    // This pipeline just decides whether JSON parses or not
+    contentTypeHeader && isJSONContentType(contentTypeHeader.value)
+      ? O.of(rawBody)
+      : O.none,
 
 
-  return JSON.parse(rawBody)
+    // Try parsing, if failed, go to the fail option
+    O.chain((body) => O.tryCatch(() => JSON.parse(body))),
+
+    // If JSON, return that (get), else return just the body string (else)
+    O.getOrElse<any | string>(() => rawBody)
+  )
+
+  return x
 }
 }
 
 
 export const runRESTRequest$ = (): TaskEither<
 export const runRESTRequest$ = (): TaskEither<