diff --git a/frontend/src/metabase/dashboard/dashboard.js b/frontend/src/metabase/dashboard/dashboard.js index 19de4d3317cb4e3e4353e1c417fb4593068be86a..d3d0003ee1ba87364d2f7146ade85406fc0015fc 100644 --- a/frontend/src/metabase/dashboard/dashboard.js +++ b/frontend/src/metabase/dashboard/dashboard.js @@ -113,6 +113,15 @@ const updateDashcardId = createAction(UPDATE_DASHCARD_ID, (oldDashcardId, newDas const CLEAR_CARD_DATA = "CLEAR_CARD_DATA"; export const clearCardData = createAction(CLEAR_CARD_DATA, (cardId, dashcardId) => ({ cardId, dashcardId })); +export async function fetchDataOrError(dataPromise) { + try { + return await dataPromise; + } + catch (error) { + return { error }; + } +} + export const fetchCardData = createThunkAction(FETCH_CARD_DATA, function(card, dashcard, clearExisting = false) { return async function(dispatch, getState) { if (clearExisting) { @@ -152,13 +161,7 @@ export const fetchCardData = createThunkAction(FETCH_CARD_DATA, function(card, d } }, DATASET_SLOW_TIMEOUT); - - try { - result = await MetabaseApi.dataset(datasetQuery); - } - catch (error) { - result = { error }; - } + result = await fetchDataOrError(Promise.reject({data: {message: 'test'}})); clearTimeout(slowCardTimer); return { dashcard_id: dashcard.id, card_id: card.id, result }; diff --git a/frontend/test/unit/dashboard/dashboard.spec.js b/frontend/test/unit/dashboard/dashboard.spec.js new file mode 100644 index 0000000000000000000000000000000000000000..9290c95206994a319f13d6066fc1af3963f0e84d --- /dev/null +++ b/frontend/test/unit/dashboard/dashboard.spec.js @@ -0,0 +1,32 @@ +import { fetchDataOrError } from 'metabase/dashboard/dashboard'; + +describe("Dashboard", () => { + describe("fetchDataOrError()", () => { + it("should return data on successful fetch", async () => { + const data = { + series: [1, 2, 3] + }; + + const successfulFetch = Promise.resolve(data); + + const result = await fetchDataOrError(successfulFetch); + expect(result.error).toBeUndefined(); + expect(result).toEqual(data); + }); + + it("should return map with error key on failed fetch", async () => { + const error = { + status: 504, + statusText: "GATEWAY_TIMEOUT", + data: { + message: "Failed to load resource: the server responded with a status of 504 (GATEWAY_TIMEOUT)" + } + }; + + const failedFetch = Promise.reject(error); + + const result = await fetchDataOrError(failedFetch); + expect(result.error).toEqual(error); + }); + }); +});