@@ -18,11 +18,29 @@ const endpoint = '/graphql';
1818const clientNameHeader = 'apollographql-client-name' ;
1919const clientName = 'UOP frontend' ;
2020
21+ export async function sendClientLog (
22+ errorPayload : string ,
23+ token ?: string | null
24+ ) {
25+ if ( ! token ) return ;
26+
27+ try {
28+ await getSdk (
29+ new GraphQLClient ( endpoint )
30+ . setHeader ( clientNameHeader , clientName )
31+ . setHeader ( 'authorization' , `Bearer ${ token } ` )
32+ ) . addClientLog ( { error : errorPayload } ) ;
33+ } catch ( sendError ) {
34+ console . error ( 'Failed to send client log' , sendError ) ;
35+ }
36+ }
37+
2138const notifyAndLog = async (
2239 enqueueSnackbar : WithSnackbarProps [ 'enqueueSnackbar' ] ,
2340 userMessage : string ,
2441 error : ClientError | string ,
25- writeToServerLog = true
42+ writeToServerLog = true ,
43+ token ?: string
2644) => {
2745 enqueueSnackbar ( userMessage , {
2846 variant : 'error' ,
@@ -33,10 +51,7 @@ const notifyAndLog = async (
3351 console . error ( { userMessage, error } ) ;
3452
3553 if ( writeToServerLog ) {
36- await getSdk (
37- // eslint-disable-next-line @typescript-eslint/no-use-before-define
38- new UnauthorizedGraphQLClient ( endpoint , enqueueSnackbar , true )
39- ) . addClientLog ( { error : JSON . stringify ( { userMessage, error } ) } ) ;
54+ await sendClientLog ( JSON . stringify ( { userMessage, error } ) , token ) ;
4055 }
4156} ;
4257
@@ -49,8 +64,7 @@ export function getUnauthorizedApi() {
4964class UnauthorizedGraphQLClient extends GraphQLClient {
5065 constructor (
5166 private endpoint : string ,
52- private enqueueSnackbar : WithSnackbarProps [ 'enqueueSnackbar' ] ,
53- private skipErrorReport ?: boolean
67+ private enqueueSnackbar : WithSnackbarProps [ 'enqueueSnackbar' ]
5468 ) {
5569 super ( endpoint ) ;
5670 this . setHeader ( clientNameHeader , clientName ) ;
@@ -63,13 +77,6 @@ class UnauthorizedGraphQLClient extends GraphQLClient {
6377 return super
6478 . request < T , V > ( query as RequestQuery < T , V > , ...variablesAndRequestHeaders )
6579 . catch ( ( error ) => {
66- // if the `notificationWithClientLog` fails
67- // and it fails while reporting an error, it can
68- // easily cause an infinite loop
69- if ( this . skipErrorReport ) {
70- throw error ;
71- }
72-
7380 if ( ! error || ! error . response ) {
7481 notifyAndLog (
7582 this . enqueueSnackbar ,
@@ -132,11 +139,16 @@ class AuthorizedGraphQLClient extends GraphQLClient {
132139 const newToken = data . token ;
133140 this . setHeader ( 'authorization' , `Bearer ${ newToken } ` ) ;
134141 this . tokenRenewed && this . tokenRenewed ( newToken as string ) ;
142+ this . token = newToken ;
143+ this . renewalDate = this . getRenewalDate ( this . token ) ;
144+ this . externalToken = this . getExternalToken ( this . token ) ;
135145 } catch ( error ) {
136146 notifyAndLog (
137147 this . enqueueSnackbar ,
138148 'Server rejected user credentials' ,
139- JSON . stringify ( error )
149+ JSON . stringify ( error ) ,
150+ true ,
151+ this . token
140152 ) ;
141153 this . onSessionExpired ( ) ;
142154 }
@@ -154,7 +166,8 @@ class AuthorizedGraphQLClient extends GraphQLClient {
154166 this . enqueueSnackbar ,
155167 'No response received from server' ,
156168 error ,
157- false
169+ false ,
170+ this . token
158171 ) ;
159172 } else if (
160173 error . response . error &&
@@ -164,7 +177,8 @@ class AuthorizedGraphQLClient extends GraphQLClient {
164177 this . enqueueSnackbar ,
165178 'Connection problem!' ,
166179 error ,
167- false
180+ false ,
181+ this . token
168182 ) ;
169183 } else if (
170184 error . response . errors &&
@@ -175,15 +189,17 @@ class AuthorizedGraphQLClient extends GraphQLClient {
175189 this . enqueueSnackbar ,
176190 'Your session has expired, you will need to log in again through the external homepage' ,
177191 error ,
178- false
192+ false ,
193+ this . token
179194 ) ;
180195 this . onSessionExpired ( ) ;
181196 } else if ( ( jwtDecode ( this . token ) as any ) . exp < nowTimestampSeconds ) {
182197 notifyAndLog (
183198 this . enqueueSnackbar ,
184199 'Your session has expired, you will need to log in again.' ,
185200 error ,
186- false
201+ false ,
202+ this . token
187203 ) ;
188204 this . onSessionExpired ( ) ;
189205 } else {
@@ -192,7 +208,9 @@ class AuthorizedGraphQLClient extends GraphQLClient {
192208 notifyAndLog (
193209 this . enqueueSnackbar ,
194210 graphQLError ?. message || 'Something went wrong!' ,
195- error
211+ error ,
212+ true ,
213+ this . token
196214 ) ;
197215 }
198216
0 commit comments