5
5
*/
6
6
import _ from 'lodash' ;
7
7
import qs from 'qs' ;
8
+ import { setErrorIcon , ERROR_ICON_TYPES } from '../utils/errors' ;
8
9
import { getApi } from './api' ;
9
10
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
+
10
34
/**
11
35
* Submission service.
12
36
*/
@@ -36,8 +60,8 @@ class SubmissionsService {
36
60
37
61
const url = `/submissions?${ qs . stringify ( query , { encode : false } ) } ` ;
38
62
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 ) ;
41
65
}
42
66
43
67
/**
@@ -47,14 +71,14 @@ class SubmissionsService {
47
71
async getScanReviewIds ( ) {
48
72
const reviews = await Promise . all ( [
49
73
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 ) ,
52
76
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 ) ,
55
79
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 ) ,
58
82
] ) . then ( ( [ av , sonar , virus ] ) => ( _ . concat ( av , sonar , virus ) ) ) ;
59
83
60
84
return reviews . map ( r => r . id ) ;
0 commit comments