Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

Commit c6ef76d

Browse files
committed
Improve error responses handling
1 parent 1e604c5 commit c6ef76d

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

Diff for: packages/theme-predictive-search/__tests__/request.test.js

+44-12
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ describe("request()", () => {
6363

6464
jest.runAllTimers();
6565

66-
expect(spyOnError).toHaveBeenCalledWith(
67-
new Error("An error ocurred whilst sending the request.")
68-
);
66+
expect(spyOnError).toHaveBeenCalledWith(new Error("Request Error"));
6967
});
7068

7169
it("fails with an invalid Content-Type header", () => {
@@ -81,9 +79,7 @@ describe("request()", () => {
8179

8280
jest.runAllTimers();
8381

84-
expect(spyOnError).toHaveBeenCalledWith(
85-
new Error("An error ocurred whilst sending the request.")
86-
);
82+
expect(spyOnError).toHaveBeenCalledWith(new Error("Request Error"));
8783
});
8884

8985
it("fails in the absence of Content-Type header", () => {
@@ -96,9 +92,7 @@ describe("request()", () => {
9692

9793
jest.runAllTimers();
9894

99-
expect(spyOnError).toHaveBeenCalledWith(
100-
new Error("An error ocurred whilst sending the request.")
101-
);
95+
expect(spyOnError).toHaveBeenCalledWith(new Error("Request Error"));
10296
});
10397
});
10498

@@ -116,9 +110,7 @@ describe("request()", () => {
116110

117111
jest.runAllTimers();
118112

119-
expect(spyOnError).toHaveBeenCalledWith(
120-
new Error("An error ocurred whilst sending the request.")
121-
);
113+
expect(spyOnError).toHaveBeenCalledWith(new Error("Request Error"));
122114
});
123115

124116
describe("422", () => {
@@ -212,5 +204,45 @@ describe("request()", () => {
212204
expect(spyOnError).toHaveBeenCalledTimes(1);
213205
});
214206
});
207+
208+
describe("417", () => {
209+
it("head request", () => {
210+
const spyOnError = jest.fn();
211+
xhrMock.get(/^\/search\/suggest\.json/g, (req, res) =>
212+
res
213+
.status(417)
214+
.header("Content-Type", "application/json; charset=utf-8")
215+
.body(
216+
JSON.stringify({
217+
status: 417,
218+
message: "Expectation Failed",
219+
description: "Unsupported shop primary locale"
220+
})
221+
)
222+
);
223+
224+
request("config=foo", "foo-417", null, spyOnError);
225+
226+
jest.runAllTimers();
227+
228+
const error = new Error();
229+
error.name = "Expectation Failed";
230+
error.message = "Unsupported shop primary locale";
231+
expect(spyOnError).toHaveBeenCalledWith(error);
232+
});
233+
});
234+
});
235+
236+
describe("500", () => {
237+
it("head request", () => {
238+
const spyOnError = jest.fn();
239+
xhrMock.get(/^\/search\/suggest\.json/g, (req, res) => res.status(500));
240+
241+
request("config=foo", "foo-500", null, spyOnError);
242+
243+
jest.runAllTimers();
244+
245+
expect(spyOnError).toHaveBeenCalledWith(new Error("Server Error"));
246+
});
215247
});
216248
});

Diff for: packages/theme-predictive-search/src/request.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ export default function request(configParams, query, onSuccess, onError) {
55
if (xhr.readyState === XMLHttpRequest.DONE) {
66
var contentType = xhr.getResponseHeader("Content-Type");
77

8+
if (xhr.status >= 500) {
9+
onError(new Error("Server Error"));
10+
11+
return;
12+
}
13+
814
if (
915
typeof contentType !== "string" ||
1016
contentType.toLowerCase().match("application/json") === null
1117
) {
12-
onError(new Error("An error ocurred whilst sending the request."));
18+
onError(new Error("Request Error"));
1319

1420
return;
1521
}
1622

17-
if (xhr.status >= 200 && xhr.status < 300) {
23+
if (xhr.status === 200) {
1824
try {
1925
var res = JSON.parse(xhr.responseText);
2026
res.query = query;
@@ -59,7 +65,7 @@ export default function request(configParams, query, onSuccess, onError) {
5965
return;
6066
}
6167

62-
onError(new Error("An error ocurred whilst sending the request."));
68+
onError(new Error("Request Error"));
6369

6470
return;
6571
}

0 commit comments

Comments
 (0)