Skip to content

Commit

Permalink
Merge pull request #1210 from eclipse-tractusx/chore/#1188-duplicated…
Browse files Browse the repository at this point in the history
…-notification-message-fix

Chore/#1188 duplicated notification message fix
  • Loading branch information
ds-mwesener authored Jul 12, 2024
2 parents ad96861 + 65e92eb commit 4a16c35
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _**For better traceability add the corresponding GitHub issue number in each cha
- #1173 Update IRS-Helm from 7.1.4 to 7.2.0 - updated Compatibility Matrix
- #1082 fix duplicate key errors when synchronizing assets with IRS
- #970 fixed bug where the right operand of policies was not showing up in table and detailed view
- #1188 prevent retry requests for notification actions to prevent duplicate error messages in history

## [12.0.0 - 05.07.2024]

Expand Down
79 changes: 54 additions & 25 deletions frontend/src/app/modules/core/api/http-error.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { ToastService } from 'src/app/modules/shared/components/toasts/toast.ser

export class HttpErrorInterceptor implements HttpInterceptor {

// List of request.url that should not automatically display a toast but are handled custom (Can be extended later by METHOD)
private avoidList = [ '/api/notifications', '/api/notifications/*/approve' ];
// List of request.url that should not automatically display a toast but are handled custom (Can be extended later by METHOD) and should not be retried
private avoidList = [ '/api/notifications', '/api/notifications/*/approve', '/api/notifications/*/cancel', '/api/notifications/*/close', '/api/notifications/*/update' ];

constructor(private readonly toastService: ToastService) {
}
Expand All @@ -36,36 +36,65 @@ export class HttpErrorInterceptor implements HttpInterceptor {
request: HttpRequest<Record<string, unknown>>,
next: HttpHandler,
): Observable<HttpEvent<Record<string, unknown>>> {
return next.handle(request).pipe(
retry(1),
catchError((errorResponse: HttpErrorResponse) => {
// Possible ToDos:
// Add error code for specific errors "AUTH0" => Unauthorized, etc.
// Add logging server to store errors from the FE
// Intercept "console.error" and send to logging server for further analysis
const { error, message } = errorResponse;
const errorMessage = !error.message ? message : `Backend returned code ${ error.status }: ${ error.message }`;

// Check if the request URL matches any pattern in the avoidList
if (!this.isOnAlreadyHandledUrlList(request.url)) {
this.toastService.error(errorMessage);
}

return throwError(() => errorResponse);
}),
);
const requestUrl = this.stripBaseUrl(request.url);
const isHandled = this.isOnAlreadyHandledUrlList(requestUrl);

if (isHandled) {
// Handle the request without retry if it matches the avoidList
return next.handle(request).pipe(
catchError((errorResponse: HttpErrorResponse) => this.handleError(request, errorResponse)),
);
} else {
// Retry the request once if it does not match the avoidList
return next.handle(request).pipe(
retry(1),
catchError((errorResponse: HttpErrorResponse) => this.handleError(request, errorResponse)),
);
}
}

private handleError(request: HttpRequest<any>, errorResponse: HttpErrorResponse): Observable<never> {
const { error, message } = errorResponse;
const errorMessage = !error.message ? message : `Backend returned code ${ error.status }: ${ error.message }`;

// Check if the request URL matches any pattern in the avoidList
if (!this.isOnAlreadyHandledUrlList(this.stripBaseUrl(request.url))) {
this.toastService.error(errorMessage);
}

return throwError(() => errorResponse);
}

// Helper method to check if the URL matches any pattern in the avoidList
// Helper method to check if the URL matches any pattern in the avoidList
private isOnAlreadyHandledUrlList(url: string): boolean {
return !this.avoidList.some(pattern => this.urlMatchesPattern(url, pattern));
return this.avoidList.some(pattern => this.urlMatchesPattern(url, pattern));
}

// Helper method to check if the URL matches a wildcard pattern
// Helper method to check if the URL matches a wildcard pattern
private urlMatchesPattern(url: string, pattern: string): boolean {
const regexPattern = pattern.replace(/\*/g, '.*');
const regex = new RegExp(`^${regexPattern}$`);
// Convert wildcard pattern to regex
const escapedPattern = pattern.split('*').map(escapeRegExp).join('.*');
const regexPattern = `^${ escapedPattern }$`;
const regex = new RegExp(regexPattern);
return regex.test(url);
}

// Helper method to strip base URL from the request URL
private stripBaseUrl(url: string): string {
const baseUrlPattern = /^(https?:\/\/[^/]+)(\/.*)?$/;
return baseUrlPattern.exec(url)?.[2] ?? url;
}
}

// Helper function to escape regex special characters in a string
function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}








0 comments on commit 4a16c35

Please sign in to comment.