From 5f4515e2fbaceff47b5d4feaeffabde33decf70f Mon Sep 17 00:00:00 2001 From: ziajka Date: Mon, 2 Jul 2018 12:11:43 +0200 Subject: [PATCH] Toaster - default error handler --- src/app/app.module.ts | 4 +-- .../components/projects/projects.component.ts | 2 ++ src/app/raven-error-handler.spec.ts | 4 +-- src/app/raven-error-handler.ts | 10 ++++--- src/app/toaster-error-handler.spec.ts | 30 +++++++++++++++++++ src/app/toaster-error-handler.ts | 15 ++++++++++ 6 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 src/app/toaster-error-handler.spec.ts create mode 100644 src/app/toaster-error-handler.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 5407a7f3..bfb0904a 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -75,11 +75,11 @@ import { ProjectMapShortcutsComponent } from './components/project-map/project-m import { SettingsComponent } from './components/settings/settings.component'; import { SettingsService } from "./services/settings.service"; -import { RavenErrorHandler } from "./raven-error-handler"; import { LocalServerComponent } from './components/local-server/local-server.component'; import { ProgressComponent } from './common/progress/progress.component'; import { ProgressService } from "./common/progress/progress.service"; import { version } from "./version"; +import { HttpErrorHandler } from "./toaster-error-handler"; Raven @@ -145,7 +145,7 @@ Raven ], providers: [ SettingsService, - { provide: ErrorHandler, useClass: RavenErrorHandler }, + { provide: ErrorHandler, useClass: HttpErrorHandler }, D3Service, VersionService, ProjectService, diff --git a/src/app/components/projects/projects.component.ts b/src/app/components/projects/projects.component.ts index 47c327c7..1d2e7c02 100644 --- a/src/app/components/projects/projects.component.ts +++ b/src/app/components/projects/projects.component.ts @@ -62,6 +62,8 @@ export class ProjectsComponent implements OnInit { .list(this.server) .subscribe((projects: Project[]) => { this.projectDatabase.addProjects(projects); + }, null, () => { + }); } diff --git a/src/app/raven-error-handler.spec.ts b/src/app/raven-error-handler.spec.ts index 0216aa86..6ca84027 100644 --- a/src/app/raven-error-handler.spec.ts +++ b/src/app/raven-error-handler.spec.ts @@ -31,11 +31,11 @@ describe('RavenErrorHandler', () => { it('should handle error', () => { settingsService.set('crash_reports', true); environment.production = true; - expect(handler.shouldSend()).toBeTruthy() + expect(handler.shouldSend()()).toBeTruthy(); }); it('should not handle when crash reports are disabled', () => { settingsService.set('crash_reports', false); - expect(handler.shouldSend()).toBeFalsy(); + expect(handler.shouldSend()()).toBeFalsy(); }); }); diff --git a/src/app/raven-error-handler.ts b/src/app/raven-error-handler.ts index ccb940ca..c3288588 100644 --- a/src/app/raven-error-handler.ts +++ b/src/app/raven-error-handler.ts @@ -7,16 +7,18 @@ import { environment } from "../environments/environment"; export class RavenErrorHandler implements ErrorHandler { - constructor(@Inject(Injector) private injector: Injector) {} + constructor(@Inject(Injector) protected injector: Injector) {} handleError(err: any): void { - Raven.setShouldSendCallback(this.shouldSend); + Raven.setShouldSendCallback(this.shouldSend()); console.error(err.originalError || err); } shouldSend() { - const settingsService: SettingsService = this.injector.get(SettingsService); - return environment.production && settingsService.get('crash_reports'); + return () => { + const settingsService: SettingsService = this.injector.get(SettingsService); + return environment.production && settingsService.get('crash_reports'); + }; } } diff --git a/src/app/toaster-error-handler.spec.ts b/src/app/toaster-error-handler.spec.ts new file mode 100644 index 00000000..679f8f49 --- /dev/null +++ b/src/app/toaster-error-handler.spec.ts @@ -0,0 +1,30 @@ +import { TestBed } from '@angular/core/testing'; +import { ToasterService } from "./services/toaster.service"; +import { MockedToasterService } from "./services/toaster.service.spec"; +import { ToasterErrorHandler } from "./toaster-error-handler"; +import { RavenErrorHandler } from "./raven-error-handler"; + + +describe('ToasterErrorHandler', () => { + let handler: ToasterErrorHandler; + let toasterService: MockedToasterService; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + { provide: ToasterService, useClass: MockedToasterService }, + RavenErrorHandler, + ToasterErrorHandler, + ] + }); + + handler = TestBed.get(ToasterErrorHandler); + toasterService = TestBed.get(ToasterService); + }); + + + it('should call toaster with error message', () => { + handler.handleError(new Error("message")); + expect(toasterService.errors).toEqual(["message"]); + }); +}); diff --git a/src/app/toaster-error-handler.ts b/src/app/toaster-error-handler.ts new file mode 100644 index 00000000..eabe381c --- /dev/null +++ b/src/app/toaster-error-handler.ts @@ -0,0 +1,15 @@ +import { RavenErrorHandler } from "./raven-error-handler"; +import { ToasterService } from "./services/toaster.service"; + + +export class ToasterErrorHandler extends RavenErrorHandler { + + handleError(err: any): void { + super.handleError(err); + + const toasterService = this.injector.get(ToasterService); + const error = err.originalError || err; + toasterService.error(error.message); + } + +}