Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: interceptor fixes to make new call if access_token is not present and not throw access_token errors to sentry #3263

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/app/core/interceptors/httpInterceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ describe('HttpConfigInterceptor', () => {
routerAuthService.fetchAccessToken.and.rejectWith(new Error('error'));

httpInterceptor.refreshAccessToken().subscribe({
error: (err) => {
expect(err).toBeTruthy();
complete: () => {
expect(userEventService.logout).toHaveBeenCalledTimes(1);
expect(secureStorageService.clearAll).toHaveBeenCalledTimes(1);
expect(storageService.clearAll).toHaveBeenCalledTimes(1);
Expand Down
10 changes: 5 additions & 5 deletions src/app/core/interceptors/httpInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@angular/common/http';
import { Injectable } from '@angular/core';

import { BehaviorSubject, Observable, forkJoin, from, iif, of, throwError } from 'rxjs';
import { BehaviorSubject, EMPTY, Observable, forkJoin, from, iif, of, throwError } from 'rxjs';
import { catchError, concatMap, filter, mergeMap, take } from 'rxjs/operators';

import { JwtHelperService } from '../services/jwt-helper.service';
Expand Down Expand Up @@ -67,12 +67,12 @@ export class HttpConfigInterceptor implements HttpInterceptor {
refreshAccessToken(): Observable<string> {
return from(this.tokenService.getRefreshToken()).pipe(
concatMap((refreshToken) => this.routerAuthService.fetchAccessToken(refreshToken)),
catchError((error) => {
catchError(() => {
this.userEventService.logout();
this.secureStorageService.clearAll();
this.storageService.clearAll();
globalCacheBusterNotifier.next();
return throwError(error);
return EMPTY;
}),
Comment on lines +70 to 76
Copy link

@coderabbitai coderabbitai bot Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Listen up! Let's add some style to our error handling, machan!

The silent error handling with EMPTY is cool, but we might miss important token refresh failures. Let's add some logging before we go silent.

Here's how to do it with style:

 catchError(() => {
+  console.warn('Token refresh failed - cleaning up and logging out silently');
   this.userEventService.logout();
   this.secureStorageService.clearAll();
   this.storageService.clearAll();
   globalCacheBusterNotifier.next();
   return EMPTY;
 }),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
catchError(() => {
this.userEventService.logout();
this.secureStorageService.clearAll();
this.storageService.clearAll();
globalCacheBusterNotifier.next();
return throwError(error);
return EMPTY;
}),
catchError(() => {
console.warn('Token refresh failed - cleaning up and logging out silently');
this.userEventService.logout();
this.secureStorageService.clearAll();
this.storageService.clearAll();
globalCacheBusterNotifier.next();
return EMPTY;
}),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aniruddha-Shriwant, no worries, boss! We'll leave it as it is.

concatMap((authResponse) => this.routerAuthService.newAccessToken(authResponse.access_token)),
concatMap(() => from(this.tokenService.getAccessToken()))
Expand All @@ -88,7 +88,7 @@ export class HttpConfigInterceptor implements HttpInterceptor {
getAccessToken(): Observable<string> {
return from(this.tokenService.getAccessToken()).pipe(
concatMap((accessToken) => {
if (this.expiringSoon(accessToken)) {
if (!accessToken || this.expiringSoon(accessToken)) {
if (!this.accessTokenCallInProgress) {
this.accessTokenCallInProgress = true;
this.accessTokenSubject.next(null);
Expand Down Expand Up @@ -168,7 +168,7 @@ export class HttpConfigInterceptor implements HttpInterceptor {
this.secureStorageService.clearAll();
this.storageService.clearAll();
globalCacheBusterNotifier.next();
return throwError(error);
return EMPTY;
Copy link

@coderabbitai coderabbitai bot Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Kabali style suggestion: Add telemetry before going silent!

While returning EMPTY for 401s is stylish, we should track these occurrences for monitoring purposes.

Here's how to do it with panache:

+              // Track authentication failures for monitoring
+              const errorContext = {
+                status: error.status,
+                blocked: error.headers.get('X-Mobile-App-Blocked'),
+                url: request.url
+              };
+              console.warn('Authentication failed - handling silently', errorContext);
               return EMPTY;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return EMPTY;
// Track authentication failures for monitoring
const errorContext = {
status: error.status,
blocked: error.headers.get('X-Mobile-App-Blocked'),
url: request.url
};
console.warn('Authentication failed - handling silently', errorContext);
return EMPTY;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nopes, access_token 401 is not something we should track

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aniruddha-Shriwant, got it, boss! We'll skip tracking access_token 401 errors. Moving ahead!


✏️ Learnings added
Learnt from: Aniruddha-Shriwant
PR: fylein/fyle-mobile-app#3263
File: src/app/core/interceptors/httpInterceptor.ts:171-171
Timestamp: 2024-11-18T04:24:04.253Z
Learning: In the `HttpConfigInterceptor` class in `src/app/core/interceptors/httpInterceptor.ts`, do not track access_token 401 errors.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

}
}
return throwError(error);
Expand Down
Loading