Toaster - default error handler

This commit is contained in:
ziajka 2018-07-02 12:11:43 +02:00
parent fff1439238
commit 5f4515e2fb
6 changed files with 57 additions and 8 deletions

View File

@ -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,

View File

@ -62,6 +62,8 @@ export class ProjectsComponent implements OnInit {
.list(this.server)
.subscribe((projects: Project[]) => {
this.projectDatabase.addProjects(projects);
}, null, () => {
});
}

View File

@ -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();
});
});

View File

@ -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');
};
}
}

View File

@ -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"]);
});
});

View File

@ -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);
}
}