4
4
*/
5
5
6
6
import { createActions } from 'redux-actions' ;
7
- import { decodeToken } from 'tc-accounts ' ;
7
+ import { decodeToken } from '@topcoder-platform/ tc-auth-lib ' ;
8
8
import { getApiV3 , getApiV5 } from '../services/api' ;
9
+ import { setErrorIcon , ERROR_ICON_TYPES } from '../utils/errors' ;
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 === 403 ) {
20
+ setErrorIcon ( ERROR_ICON_TYPES . API , 'Auth0' , 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
+ }
9
33
10
34
/**
11
35
* @static
@@ -22,8 +46,7 @@ function loadProfileDone(userTokenV3) {
22
46
apiV3 . get ( `/members/${ user . handle } ` )
23
47
. then ( res => res . json ( ) ) . then ( res => ( res . result . status === 200 ? res . result . content : { } ) ) ,
24
48
apiV5 . get ( `/groups?memberId=${ user . userId } &membershipType=user` )
25
- . then ( res => ( res . ok ? res . json ( ) : new Error ( res . statusText ) ) )
26
- . then ( res => ( res . message ? new Error ( res . message ) : res ) ) ,
49
+ . then ( checkErrorV5 ) . then ( res => res . result || [ ] ) ,
27
50
] ) . then ( ( [ profile , groups ] ) => ( { ...profile , groups } ) ) ;
28
51
}
29
52
0 commit comments