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
73 lines (63 loc) · 2.91 KB
/
WindowBootstrapper.ts
File metadata and controls
73 lines (63 loc) · 2.91 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
72
73
import { IBootstrapper } from 'IBootstrapper';
import { Configuration } from '../configuration/Configuration';
import { IConfigurationSettings } from '../configuration/IConfigurationSettings';
import { IError } from '../models/IError';
import { WebErrorParser } from '../services/WebErrorParser';
import { WebModuleCollector } from '../services/WebModuleCollector';
import { WebRequestInfoCollector } from '../services/WebRequestInfoCollector';
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 configDefaults = Configuration.defaults;
var settings = this.getDefaultsSettingsFromScriptTag();
if (settings && (settings.apiKey || settings.serverUrl)) {
configDefaults.apiKey = settings.apiKey;
configDefaults.serverUrl = settings.serverUrl;
}
configDefaults.errorParser = new WebErrorParser();
configDefaults.moduleCollector = new WebModuleCollector();
configDefaults.requestInfoCollector = new WebRequestInfoCollector();
configDefaults.submissionClient = new DefaultSubmissionClient();
TraceKit.report.subscribe(this.processUnhandledException);
TraceKit.extendToAsynchronousCallbacks();
if (typeof $ !== 'undefined' && $(document)) {
$(document).ajaxError(this.processJQueryAjaxError);
}
}
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 processUnhandledException(stackTrace:TraceKit.StackTrace, options?:any): void {
var builder = ExceptionlessClient.default.createUnhandledException(new Error(stackTrace.message || (options || {}).status || 'Script error'), 'onerror');
builder.pluginContextData['@@_TraceKit.StackTrace'] = stackTrace;
builder.submit();
}
private processJQueryAjaxError(event, xhr, settings, error:Error): void {
var client = ExceptionlessClient.default;
if (xhr.status === 404) {
client.submitNotFound(settings.url);
} else if (xhr.status !== 401) {
client.createUnhandledException(error, 'JQuery.ajaxError')
.setSource(settings.url)
.setProperty('status', xhr.status)
.setProperty('request', settings.data)
.setProperty('response', xhr.responseText && xhr.responseText.slice ? xhr.responseText.slice(0, 1024) : undefined)
.submit();
}
}
}
declare var $;