forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowBootstrapper.ts
More file actions
71 lines (61 loc) · 2.41 KB
/
WindowBootstrapper.ts
File metadata and controls
71 lines (61 loc) · 2.41 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { IBootstrapper } from 'IBootstrapper';
import { Configuration } from '../configuration/Configuration';
import { IConfigurationSettings } from '../configuration/IConfigurationSettings';
import { IError } from '../models/IError';
import { DefaultSubmissionClient } from '../submission/DefaultSubmissionClient';
import { ExceptionlessClient } from '../ExceptionlessClient';
import { Utils } from '../Utils';
export class WindowBootstrapper implements IBootstrapper {
public register(): void {
if (typeof window === 'undefined' || typeof document === 'undefined') {
return;
}
var settings = this.getDefaultsSettingsFromScriptTag();
if (settings && (settings.apiKey || settings.serverUrl)) {
Configuration.defaults.apiKey = settings.apiKey;
Configuration.defaults.serverUrl = settings.serverUrl;
}
Configuration.defaults.submissionClient = new DefaultSubmissionClient();
this.handleWindowOnError();
}
private getDefaultsSettingsFromScriptTag(): IConfigurationSettings {
if (!document || !document.getElementsByTagName) {
return null;
}
var scripts = document.getElementsByTagName('script');
for (var index = 0; index < scripts.length; index++) {
if (scripts[index].src && scripts[index].src.indexOf('/exceptionless') > -1) {
return Utils.parseQueryString(scripts[index].src.split('?').pop());
}
}
return null;
}
private handleWindowOnError(): void {
var _oldOnErrorHandler:any = window.onerror;
(<any>window).onerror = (message:string, filename:string, lineno:number, colno:number, error:Error) => {
var client = ExceptionlessClient.default;
if (error !== null && typeof error === 'object') {
client.submitUnhandledException(error);
} else {
// Only message, filename and lineno work here.
var e:IError = {
message: message,
stack_trace: [{
file_name: filename,
line_number: lineno,
column: colno
}]
};
client.createUnhandledException(new Error(message)).setMessage(message).setProperty('@error', e).submit();
}
if (_oldOnErrorHandler) {
try {
return _oldOnErrorHandler(message, filename, lineno, colno, error);
} catch (e) {
client.config.log.error(`An error occurred while calling previous error handler: ${e.message}`);
}
}
return false;
}
}
}