Show graphql error message (#1852)

Co-authored-by: StephaneBischoffSSENSE <stephane.bischoff@ssense.com>
Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
This commit is contained in:
Stephane
2021-10-03 22:31:47 -04:00
committed by GitHub
parent 2ab1d3dbfa
commit 06161bc963
4 changed files with 38 additions and 11 deletions

View File

@@ -447,12 +447,15 @@ const runQuery = async () => {
icon: "done", icon: "done",
}) })
} catch (e: any) { } catch (e: any) {
response.value = `${e}. ${t("error.check_console_details")}` response.value = `${e}`
nuxt.value.$loading.finish() nuxt.value.$loading.finish()
$toast.error(`${e} ${t("error.f12_details")}`, { $toast.error(
icon: "error_outline", `${t("error.something_went_wrong")}. ${t("error.check_console_details")}`,
}) {
icon: "error_outline",
}
)
console.error(e) console.error(e)
} }

View File

@@ -1,6 +1,7 @@
import axios from "axios" import axios from "axios"
import { decodeB64StringToArrayBuffer } from "../utils/b64" import { decodeB64StringToArrayBuffer } from "../utils/b64"
import { settingsStore } from "~/newstore/settings" import { settingsStore } from "~/newstore/settings"
import { JsonFormattedError } from "~/helpers/utils/JsonFormattedError"
let cancelSource = axios.CancelToken.source() let cancelSource = axios.CancelToken.source()
@@ -39,7 +40,6 @@ const axiosWithProxy = async (req) => {
// eslint-disable-next-line no-throw-literal // eslint-disable-next-line no-throw-literal
throw "cancellation" throw "cancellation"
} else { } else {
console.error(e)
throw e throw e
} }
} }
@@ -52,14 +52,16 @@ const axiosWithoutProxy = async (req, _store) => {
cancelToken: (cancelSource && cancelSource.token) || "", cancelToken: (cancelSource && cancelSource.token) || "",
responseType: "arraybuffer", responseType: "arraybuffer",
}) })
return res return res
} catch (e) { } catch (e) {
if (axios.isCancel(e)) { if (axios.isCancel(e)) {
// eslint-disable-next-line no-throw-literal // eslint-disable-next-line no-throw-literal
throw "cancellation" throw "cancellation"
} else if (e.response?.data) {
throw new JsonFormattedError(
JSON.parse(Buffer.from(e.response.data, "base64").toString("utf8"))
)
} else { } else {
console.error(e)
throw e throw e
} }
} }

View File

@@ -1,5 +1,6 @@
import axios from "axios" import axios from "axios"
import axiosStrategy from "../AxiosStrategy" import axiosStrategy from "../AxiosStrategy"
import { JsonFormattedError } from "~/helpers/utils/JsonFormattedError"
jest.mock("axios") jest.mock("axios")
jest.mock("~/newstore/settings", () => { jest.mock("~/newstore/settings", () => {
@@ -42,18 +43,34 @@ describe("axiosStrategy", () => {
await expect(axiosStrategy({})).resolves.toBeDefined() await expect(axiosStrategy({})).resolves.toBeDefined()
}) })
test("rejects cancel errors with text 'cancellation'", () => { test("rejects cancel errors with text 'cancellation'", async () => {
axios.isCancel.mockReturnValueOnce(true) axios.isCancel.mockReturnValueOnce(true)
axios.mockRejectedValue("err") axios.mockRejectedValue("err")
expect(axiosStrategy({})).rejects.toBe("cancellation") await expect(axiosStrategy({})).rejects.toBe("cancellation")
}) })
test("rejects non-cancellation errors as-is", () => { test("rejects non-cancellation errors as-is", async () => {
axios.isCancel.mockReturnValueOnce(false) axios.isCancel.mockReturnValueOnce(false)
axios.mockRejectedValue("err") axios.mockRejectedValue("err")
expect(axiosStrategy({})).rejects.toBe("err") await expect(axiosStrategy({})).rejects.toBe("err")
})
test("non-cancellation errors that have response data are thrown", async () => {
const errorResponse = { error: "errr" }
axios.isCancel.mockReturnValueOnce(false)
axios.mockRejectedValue({
response: {
data: Buffer.from(JSON.stringify(errorResponse), "utf8").toString(
"base64"
),
},
})
await expect(axiosStrategy({})).rejects.toMatchObject(
new JsonFormattedError(errorResponse)
)
}) })
}) })
}) })

View File

@@ -0,0 +1,5 @@
export class JsonFormattedError extends Error {
constructor(jsonObject: any) {
super(JSON.stringify(jsonObject, null, 2))
}
}