Skip to content

Commit

Permalink
fix: better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
conradbekondo committed Feb 4, 2025
1 parent 287d950 commit 76ef0fd
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/app/interceptors/access-token.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import { catchError, concatMap, EMPTY, throwError } from 'rxjs';
import { accessToken, RefreshAccessToken } from '../state/user';
import { environment } from '@env/environment.development';

function fixedErrorMessage(error: HttpErrorResponse) {
return throwError(() => {
if (error.status == 0) return new Error('Cannot connect to server');
return error.error?.message ?? error.message;
})
}

export const accessTokenInterceptor: HttpInterceptorFn = (req, next) => {
const store = inject(Store);
const token = store.selectSnapshot(accessToken);

if (req.url.startsWith(environment.apiOrigin) && token) {
return next(req.clone({ setHeaders: { 'Authorization': `Bearer ${token}` } })).pipe(
catchError((e: HttpErrorResponse) => {
if (e.status == 401 && req.url != environment.apiOrigin + '/auth/revoke-token')
if ((e.status == 401 || e.status == 403) && req.url != environment.apiOrigin + '/auth/revoke-token')
return store.dispatch(RefreshAccessToken).pipe(
concatMap(() => next(req.clone({ setHeaders: { authorization: `Bearer ${store.selectSnapshot(accessToken)}` } }))),
catchError((e: HttpErrorResponse) => {
Expand All @@ -25,8 +32,11 @@ export const accessTokenInterceptor: HttpInterceptorFn = (req, next) => {
})
);
return throwError(() => e);
})
}),
catchError(fixedErrorMessage)
);
}
return next(req);
return next(req).pipe(
catchError(fixedErrorMessage)
);
};

0 comments on commit 76ef0fd

Please sign in to comment.