Skip to content

Commit bed729f

Browse files
Added checkErrorV5 to submissions services
1 parent 1c8f9f2 commit bed729f

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/services/submissions.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,32 @@
55
*/
66
import _ from 'lodash';
77
import qs from 'qs';
8+
import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
89
import { getApi } from './api';
910

11+
/**
12+
* Helper method that checks for HTTP error response v5 and throws Error in this case.
13+
* @param {Object} res HTTP response object
14+
* @return {Object} API JSON response object
15+
* @private
16+
*/
17+
async function checkErrorV5(res) {
18+
if (!res.ok) {
19+
if (res.status >= 500) {
20+
setErrorIcon(ERROR_ICON_TYPES.API, '/challenges', res.statusText);
21+
}
22+
throw new Error(res.statusText);
23+
}
24+
const jsonRes = (await res.json());
25+
if (jsonRes.message) {
26+
throw new Error(res.message);
27+
}
28+
return {
29+
result: jsonRes,
30+
headers: res.headers,
31+
};
32+
}
33+
1034
/**
1135
* Submission service.
1236
*/
@@ -36,8 +60,8 @@ class SubmissionsService {
3660

3761
const url = `/submissions?${qs.stringify(query, { encode: false })}`;
3862
return this.private.apiV5.get(url)
39-
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
40-
.then(res => res);
63+
.then(checkErrorV5)
64+
.then(res => res.result);
4165
}
4266

4367
/**
@@ -47,14 +71,14 @@ class SubmissionsService {
4771
async getScanReviewIds() {
4872
const reviews = await Promise.all([
4973
this.private.apiV5.get('/reviewTypes?name=AV Scan')
50-
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
51-
.then(res => res),
74+
.then(checkErrorV5)
75+
.then(res => res.result),
5276
this.private.apiV5.get('/reviewTypes?name=SonarQube Review')
53-
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
54-
.then(res => res),
77+
.then(checkErrorV5)
78+
.then(res => res.result),
5579
this.private.apiV5.get('/reviewTypes?name=Virus Scan')
56-
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
57-
.then(res => res),
80+
.then(checkErrorV5)
81+
.then(res => res.result),
5882
]).then(([av, sonar, virus]) => (_.concat(av, sonar, virus)));
5983

6084
return reviews.map(r => r.id);

0 commit comments

Comments
 (0)