Skip to content

Commit fcaf20b

Browse files
committed
fix: always show response status code on error
1 parent dcf4b8d commit fcaf20b

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

src/client/api/index.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,35 @@ function throwApiError(url, error, statusCode) {
3232
}
3333

3434
/**
35-
* @param {String} url
3635
* @param {Object} params
3736
* @returns {Promise.<T>}
3837
* @private
3938
*/
40-
function _request(url, params) {
41-
return fetch(url, {...defaultParams, ...params})
39+
function _request(params) {
40+
let requestUrl;
41+
let requestParams;
42+
if (typeof params === 'string') {
43+
requestUrl = params;
44+
requestParams = {};
45+
} else {
46+
const {url, ...restParams} = params;
47+
requestUrl = url;
48+
requestParams = restParams;
49+
}
50+
51+
let rawResponse;
52+
return fetch(requestUrl, {...defaultParams, ...requestParams})
4253
.then((response) => {
43-
const json = response.json();
54+
rawResponse = response;
55+
return response.json();
56+
})
57+
.then((json) => {
4458
if (json && json.error) {
45-
return throwApiError(url, json.error, response.status);
59+
return throwApiError(requestUrl, json.error, rawResponse.status);
4660
}
4761
return json;
4862
})
49-
.catch((error) => throwApiError(url, error.message));
63+
.catch((error) => throwApiError(requestUrl, error.message, rawResponse.status));
5064
}
5165

5266
// application api
@@ -56,15 +70,13 @@ function getStats() {
5670
}
5771

5872
function addServer(payload) {
59-
return _request(
60-
`${endpoint}/servers`,
61-
{
62-
method: 'POST',
63-
headers: new Headers({'content-type': 'application/json'}),
64-
body: JSON.stringify(payload),
65-
...defaultParams
66-
}
67-
);
73+
return _request({
74+
url: `${endpoint}/servers`,
75+
method: 'POST',
76+
headers: new Headers({'content-type': 'application/json'}),
77+
body: JSON.stringify(payload),
78+
...defaultParams
79+
});
6880
}
6981

7082
export default {

0 commit comments

Comments
 (0)