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(Core): resolve infinite loop between MessageService and the ErrorInterceptor #1419

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
1 change: 0 additions & 1 deletion packages/core/src/lib/message/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './message.enum';
export * from './message.interface';
export * from './message.service';
export { ActiveToast } from 'ngx-toastr';
49 changes: 26 additions & 23 deletions packages/core/src/lib/request/error.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Injector } from '@angular/core';
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpHandler,
Expand All @@ -16,7 +16,7 @@ import { MessageService } from '../message/shared/message.service';
providedIn: 'root'
})
export class ErrorInterceptor implements HttpInterceptor {
constructor(private injector: Injector) {}
constructor(private messageService: MessageService) {}

intercept(
originalReq: HttpRequest<any>,
Expand All @@ -33,8 +33,16 @@ export class ErrorInterceptor implements HttpInterceptor {
return next.handle(req).pipe(
catchError((error) => this.handleError(error, errorContainer)),
finalize(() => {
this.handleCaughtError(errorContainer);
this.handleUncaughtError(errorContainer);
const httpError = errorContainer.httpError;
if (!httpError) {
return;
}

if (httpError.error.toDisplay) {
this.handleCaughtError(httpError);
} else if (!httpError.error.caught) {
this.handleUncaughtError(httpError);
}
})
);
}
Expand All @@ -61,26 +69,21 @@ export class ErrorInterceptor implements HttpInterceptor {
return throwError(httpError);
}

private handleCaughtError(errorContainer: { httpError: HttpErrorResponse }) {
const httpError = errorContainer.httpError;
if (httpError && httpError.error.toDisplay) {
httpError.error.caught = true;
const messageService = this.injector.get(MessageService);
messageService.error(httpError.error.message, httpError.error.title);
}
private handleCaughtError(httpError: HttpErrorResponse) {
httpError.error.caught = true;
this.messageService.error(httpError.error.message, httpError.error.title, {
progressBar: false
});
}

private handleUncaughtError(errorContainer: {
httpError: HttpErrorResponse;
}) {
const httpError = errorContainer.httpError;
if (httpError && !httpError.error.caught) {
const messageService = this.injector.get(MessageService);
httpError.error.caught = true;
messageService.error(
'igo.core.errors.uncaught.message',
'igo.core.errors.uncaught.title'
);
}
private handleUncaughtError(httpError: HttpErrorResponse) {
httpError.error.caught = true;
this.messageService.error(
'igo.core.errors.uncaught.message',
'igo.core.errors.uncaught.title',
{
progressBar: false
}
);
}
}
3 changes: 2 additions & 1 deletion packages/geo/src/lib/query/shared/query.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { HttpClientModule } from '@angular/common/http';
import { TranslateModule } from '@ngx-translate/core';

import { QueryService } from './query.service';
import { IgoMessageModule } from '@igo2/core';

describe('QueryService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, TranslateModule.forRoot()],
imports: [HttpClientModule, TranslateModule.forRoot(), IgoMessageModule],
providers: [QueryService]
});
});
Expand Down