mirror of
https://github.com/GNS3/gns3-web-ui.git
synced 2024-12-21 22:07:48 +00:00
Toaster - default error handler
This commit is contained in:
parent
fff1439238
commit
5f4515e2fb
@ -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,
|
||||
|
@ -62,6 +62,8 @@ export class ProjectsComponent implements OnInit {
|
||||
.list(this.server)
|
||||
.subscribe((projects: Project[]) => {
|
||||
this.projectDatabase.addProjects(projects);
|
||||
}, null, () => {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
@ -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');
|
||||
};
|
||||
}
|
||||
}
|
||||
|
30
src/app/toaster-error-handler.spec.ts
Normal file
30
src/app/toaster-error-handler.spec.ts
Normal 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"]);
|
||||
});
|
||||
});
|
15
src/app/toaster-error-handler.ts
Normal file
15
src/app/toaster-error-handler.ts
Normal 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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user