Skip to content

Commit 1330198

Browse files
MahmoudMahmoud
Mahmoud
authored and
Mahmoud
committed
Throw error in the event of a network error
1 parent fe0a0c7 commit 1330198

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

spotlight-client/src/metricsApi/fetchMetrics.test.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
// =============================================================================
1717

1818
import fetchMock from "jest-fetch-mock";
19-
import { fetchAndTransformMetric, fetchMetrics } from "./fetchMetrics";
20-
import { waitForTestServer } from "../testUtils";
2119
import { ERROR_MESSAGES } from "../constants";
20+
import { waitForTestServer } from "../testUtils";
21+
import { fetchAndTransformMetric, fetchMetrics } from "./fetchMetrics";
2222

2323
describe("fetchMetrics", () => {
2424
test("returns fetched metrics", async () => {
@@ -138,4 +138,22 @@ describe("fetchAndTransformMetric", () => {
138138
fetchMock.resetMocks();
139139
fetchMock.dontMock();
140140
});
141+
142+
test("handles network errors", async () => {
143+
expect.hasAssertions();
144+
fetchMock.mockReject(new Error("Network error"));
145+
146+
try {
147+
await fetchMetrics({
148+
metricNames: ["any_metric"],
149+
tenantId: "US_ND",
150+
});
151+
} catch (e) {
152+
expect(e.message).toBe(
153+
`There was a network error attempting to fetch metrics: \nError: Network error`
154+
);
155+
}
156+
157+
fetchMock.resetMocks();
158+
});
141159
});

spotlight-client/src/metricsApi/fetchMetrics.ts

+29-22
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,38 @@ export async function fetchMetrics({
4545
// we need some way to get the auth token from the userStore, so we pass a reference to the method in this way.
4646
// ideally fetching metrics should be handled in its own mobx store rather than in the content models. See issue #560
4747
const token = await rootStore?.userStore.getToken();
48-
const response = await fetch(
49-
`${process.env.REACT_APP_API_URL}/api/${tenantId}/public`,
50-
{
51-
body: JSON.stringify({
52-
metrics: metricNames,
53-
}),
54-
headers: {
55-
Authorization: `Bearer ${token}`,
56-
"Content-Type": "application/json",
57-
},
58-
method: "POST",
48+
49+
try {
50+
const response = await fetch(
51+
`${process.env.REACT_APP_API_URL}/api/${tenantId}/public`,
52+
{
53+
body: JSON.stringify({
54+
metrics: metricNames,
55+
}),
56+
headers: {
57+
Authorization: `Bearer ${token}`,
58+
"Content-Type": "application/json",
59+
},
60+
method: "POST",
61+
}
62+
);
63+
64+
if (response.ok) {
65+
const responseData: MetricsApiResponse = await response.json();
66+
return responseData;
5967
}
60-
);
6168

62-
if (response.ok) {
63-
const responseData: MetricsApiResponse = await response.json();
64-
return responseData;
69+
const errorResponse: ErrorAPIResponse = await response.json();
70+
throw new Error(
71+
`Metrics API responded with status ${response.status}. Error message: ${
72+
errorResponse.error || "none"
73+
}`
74+
);
75+
} catch (error) {
76+
throw new Error(
77+
`There was a network error attempting to fetch metrics: \n${error}`
78+
);
6579
}
66-
67-
const errorResponse: ErrorAPIResponse = await response.json();
68-
throw new Error(
69-
`Metrics API responded with status ${response.status}. Error message: ${
70-
errorResponse.error || "none"
71-
}`
72-
);
7380
}
7481

7582
/**

0 commit comments

Comments
 (0)