/
error-handler.ts
40 lines (39 loc) · 1.51 KB
/
error-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { ErrorHandler, Injector, ApplicationRef, Injectable } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
import { debug } from './utils/logger';
import { NotifyService } from './services';
import newGithubIssueUrl from 'new-github-issue-url';
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
constructor(private injector: Injector) {
}
handleError(error: any) {
// const appRef = this.injector.get(ApplicationRef);
if (error instanceof HttpErrorResponse) {
debug.error('Backend returned status code: ', error.status);
debug.error('Response body:', error.message);
} else {
const notifyService = this.injector.get(NotifyService);
const errorMessage = error.message ? error.message : error.toString();
const issueTitle = `Bug: Application error: ${errorMessage}`;
const issueBody = `Error message: ${errorMessage}\n\nError stack:\n\n\`\`\`\n${error.stack}\n\`\`\``;
const issueUrl = newGithubIssueUrl({
user: 'imolorhe',
repo: 'altair',
title: issueTitle,
body: issueBody,
labels: [ 'bug-report' ],
template: 'Bug_report.md',
});
debug.error('Application error:', errorMessage);
notifyService.error(`An error occured: ${errorMessage}`);
notifyService.warning(`If you think this is a bug, click here to report the bug.`, 'Altair', {
disableTimeOut: true,
data: {
url: issueUrl,
}
});
}
// appRef.tick();
}
}