-
Notifications
You must be signed in to change notification settings - Fork 9
/
exceptions.js
1194 lines (1123 loc) · 47.3 KB
/
exceptions.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global window, console, document */
(function () {
"use strict";
var ArgumentException,
InvalidOperationException,
NotImplementedException,
EvalException,
RangeException,
ReferenceException,
SyntaxException,
TypeException,
URIException,
handler,
exceptions,
utilities,
_scopeOption;
//polyfill for Object.create so we can support inheritance in ie8
if (typeof Object.create != 'function') {
(function () {
var F = function () { };
Object.create = function (o) {
if (arguments.length > 1) {
throw Error('Second argument not supported');
}
if (o === null) {
throw Error('Cannot set a null [[Prototype]]');
}
if (typeof o != 'object') {
throw TypeError('Argument must be an object');
}
F.prototype = o;
return new F();
};
})();
}
utilities = {
/**
* Add a script tag on the page to load a script. Then execute the callback when the
* script has loaded.
* @function scriptTag
* @param {string} url for the script
* @param {function} optional callback to execute after the script loads
*/
scriptTag: function scriptTag(url, callback) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = url;
s.async = false;
s.onreadystatechange = s.onload = function () {
try {
var state = s.readyState;
if (callback && !callback.done && (!state || /loaded|complete/.test(state))) {
callback.done = true;
callback();
}
} catch (e) {
console.log("Error in exceptions.js!");
console.log(e);
}
};
// use body if available. more safe in IE
(document.body || document.head).appendChild(s);
},
getBrowser: function() {
var ua= navigator.userAgent,
tem,
M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])){
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return 'IE '+ (tem[1] || '');
}
if (M[1] === 'Chrome'){
tem = ua.match(/\bOPR\/(\d+)/);
if (tem !== null) {
return 'Opera ' + tem[1];
}
}
M = M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i)) !== null) M.splice(1, 1, tem[1]);
return M.join(' ');
},
/**
* Get the name of a function
* @function functionName
* @param {function} function
*/
functionName: function (fun) {
var ret = fun.toString();
ret = ret.substr('function '.length);
ret = ret.substr(0, ret.indexOf('('));
return ret;
}
};
/**
* Options for an exception. Options include retrieving a stacktrace, printing a screenshot,
* posting a serialized JSON representation of an exception to a specified url when the
* exception is reported, and/or excecuting a callback that recieves the exception when
* the exception is reported.
* @constructor
*/
function Options() {
if (!(this instanceof Options)) {
return new Options();
}
this._stacktrace = false;
this._screenshot = false;
this._post = false;
this._callback = false;
this._postToExceptionsJsServer = false
}
Options.prototype = {
/**
* Get or set the retrieve stacktrace option.
* @param {bool} Return the current option if undefined. Enable the stacktrace option
* if enable is true. Disable the stacktrace option if enable is false.
* @return Options object if enable is defined, value of the stacktrace option if
* enable is not defined.
*/
stacktrace: function (enable) {
if (enable !== undefined) {
this._stacktrace = Boolean(enable);
return this;
}
return this._stacktrace;
},
/**
* Get or set the take screenshot option.
* @param {bool} Return the current option if undefined. Enable the screenshot option
* if enable is true. Disable the screenshot option if enable is false.
* @return Options object if enable is defined, value of the screenshot option if
* enable is not defined.
*/
screenshot: function (enable) {
if (enable !== undefined) {
this._screenshot = Boolean(enable);
return this;
}
return this._screenshot;
},
/**
* Get or set the retrieve stacktrace option.
* @param {bool} Return the current option if undefined. Enable the post option
* if enable is true. Disable the post option if enable is false.
* @return Options object if enable is defined, value of the post option if
* enable is not defined.
*/
post: function (enable) {
if (enable !== undefined) {
this._post = Boolean(enable);
return this;
}
return this._post;
},
/**
* Get or set the execute handler callback option.
* @param {bool} Return the current option if undefined. Enable the callback option
* if enable is true. Disable the callback option if enable is false.
* @return Options object if enable is defined, value of the callback option if
* enable is not defined.
*/
callback: function (enable) {
if (enable !== undefined) {
this._callback = Boolean(enable);
return this;
}
return this._callback;
},
/**
* Post to exceptions.js platform
* @param {bool} Return the current option if undefined. Enable the postToExceptionsJsPlatform option
* if enable is true. Disable the postToExceptionsJsPlatform option if enable is false.
* @return Options object if enable is defined, value of the postToExceptionsJsPlatform option if
* enable is not defined.
*/
postToExceptionsJsPlatform: function (enable) {
if (enable !== undefined) {
this._postToExceptionsJsPlatform = Boolean(enable);
return this;
}
return this._postToExceptionsJsPlatform;
},
/**
* Toggle all options according to the enable parameter
* @param {bool} Enable all options if true. Disable all options if false or undefined.
* @return Options object
*/
toggleAll: function (enable) {
return this.stacktrace(enable)
.screenshot(enable)
.post(enable)
.callback(enable)
.postToExceptionsJsPlatform(enable);
}
};
/**
* Create a custom exception class with the createCustomException function
* @param {object} config object for creating an exception
* exception - {function} Constructor for the custom exception. This constructor should
* call its base exception's constructor. For debugging convenience,
* you'll probably want this function to have a name.
* baseException - {Exception} Exception that the custom exception will inherit from
* defaultOptionsFunc - {function} Provide a function that takes in an Options object
* and returns that Options object with enabled or disabled options.
* You'll usually want to enable all options by default.
* @return Custom exception. The type will be the function you provided in the config.exception property.
*/
function createCustomException(config) {
if (ArgumentException && ArgumentException.throwIf) {
ArgumentException.throwIf(!utilities.functionName(config.exception), "Your exception constructor must have a name. See examples on github.")
}
createCustomException._mixStaticFunctions(config.exception);
createCustomException._inherits(
config.exception,
config.baseException);
createCustomException._setupDefaults(
config.exception,
config.baseException,
config.defaultOptionsFunc);
return config.exception;
}
createCustomException._createThrowIf = function (Exception) {
/**
* Throw an exception if the condition is true.
* @function throwIf
* @param {bool} throw the exception if true
* @param {string} optional - create an exception with the
* message if provided. Else fallback to a generic message.
*/
return function throwIf(condition, message) {
if (condition) {
var except = new Exception(message || "Condition evaluated to truthy");
throw except;
}
};
};
createCustomException._createReportIf = function (Exception) {
/**
* Report an exception if the condition is true.
* @function reportIf
* @param {bool} report the exception if true
* @param {string} optional - create an exception with the
* message if provided. Else fallback to a generic message.
*/
return function reportIf(condition, message) {
if (condition) {
var except = new Exception(message || "Condition evaluated to truthy");
except.report();
}
};
};
createCustomException._createDefaultOptions = function (exception) {
return function defaultOptionsFunc(optionsFunc){
if (optionsFunc) {
exception._defaultOptionsFunc = optionsFunc;
return exception;
}
return exception._defaultOptionsFunc;
};
};
createCustomException._mixStaticFunctions = function (exception) {
var creators = {
throwIf: createCustomException._createThrowIf(exception),
reportIf: createCustomException._createReportIf(exception),
defaultOptionsFunc: createCustomException._createDefaultOptions(exception)
},
staticFunction;
for (staticFunction in creators) {
if (!exception[staticFunction]) {
exception[staticFunction] = creators[staticFunction];
}
}
};
createCustomException._inherits = function(sub, base) {
sub.prototype = Object.create(base.prototype);
sub.prototype.constructor = sub;
};
createCustomException._setupDefaults = function (sub, base, defaultOptionsFunc) {
sub.defaultOptionsFunc(defaultOptionsFunc || base.defaultOptionsFunc());
};
/**
* Exception is the base Exception class that wraps an error and provides extra
* functionality that the native Error class does not provide. The Exception class
* can be extended to create custom exceptions with the exceptions.createCustomException.
* @Constructor
* @param {string|Error} create an Exception with an Error object or error message. This
* constructor will create a new Error(message) if you pass in a message or simply
* use the provided Error as the underlying Error it wraps.
* @param {Object} optional - Configure the exception with a config object. All properties
* on the config are optional.
* name - {string} provide a name for the exception. If no name is provided, we check if you
* manually set the name on the error created from this exception. Otherwise, we fallback
* to the name of this exception's constructor. Name is purely used for reporting purposes.
* No functionality pivots off of name. And the common case should be to not provide a name.
* innerException - {Exception} Exceptions are recursive, so you can create an inner
* exception that is wrapped by the current exception.
* data - {object} - Provide any information you want to associate with this Exception.
* You'll notice a screenshot property is added to the data object when the screenshot
* option is enabled for this Exception. Also, a browser property is added to the
* data object.
* optionsFunc - {function} - Provide a function that takes in an Options object and returns
* that Options object with enabled or disabled options. The received options
* object will be Options object returned from the defaultOptionsFunc for the
* exception. In most cases, the defaultOptionsFunc returns an Options object
* with all options enabled.
*
*/
function Exception(message, config) {
try {
if (!(this instanceof Exception)) {
return new Exception(message, config);
}
var defaultOptions = this.constructor.defaultOptionsFunc()(new Options());
this._error = (message instanceof Error) ? message : new Error(message);
config = config || {};
this._name = config.name || this._getName();
this._stacktrace = null;
this._innerException = config.innerException || null;
this._data = config.data || {};
this._options = config.optionsFunc ? config.optionsFunc(defaultOptions) : defaultOptions;
this._guardedOptions = exceptions.handler.guard()._restrict(this._options, this);
this._listeners = [];
this._populateDefaultDataProperties();
if (this._guardedOptions.stacktrace()) {
this._stacktrace = this._retrieveStacktrace();
}
if (this._guardedOptions.screenshot()) {
this._takeScreenshot();
}
}
catch (e) {
this._screenshotComplete = true;
console.log("Error in exceptions.js:");
console.log(e);
}
}
//static functions
createCustomException._mixStaticFunctions(Exception);
//member functions
Exception.prototype = {
/**
* Get the inner exception
* @method
* @return inner exception
*/
innerException: function () {
return this._innerException;
},
/**
* Get the stacktrace
* @method
* @return stacktrace
*/
stacktrace: function () {
return this._stacktrace;
},
/**
* Get the data object
* @method
* @return data object
*/
data: function () {
return this._data;
},
/**
* Get the options
* @method
*/
options: function () {
return this._options;
},
/**
* Get the name
* @method
*/
name: function () {
return this._name;
},
/**
* Get the underlying Error
* @method
*/
error: function () {
return this._error;
},
/**
* Get the error message
* @method
*/
message: function () {
return this.error().message;
},
/**
* Report the exception (without throwing it). Reporting an exception involves
* making a post request with a serialized exception object if the post option is
* enabled and/or executing a callback if the callback option is enabled. The post
* request uses the url returned from exception.handler.postUrl() and headers returned
* from exception.handler.postHeaders(). It will not make a post request if no url
* is specified. The callback will execute the function returned from
* exceptions.handlers.callback and will not execute the callback if no function is
* specified.
*
*
*/
report: function () {
try {
if (!this._screenshotComplete && this._guardedOptions.screenshot()) {
this._listeners.push(this._report);
}
else {
this._report();
}
}
catch (e) {
console.log("Error in exceptions.js:");
console.log(e);
}
},
/**
* Convert an Exception into a simple object that is easier to serialize.
* @return {object} - {
* name: name,
* message: message,
* stacktrace: stacktrace,
* data: data,
* innerException: inner exception,
* error: underlying error
* }
*/
toSerializableObject: function () {
var innerException = this.innerException(),
simpleObject = {
name: this.name(),
message: this.message(),
stacktrace: this.stacktrace(),
data: this.data(),
innerException: innerException ? innerException.toSerializableObject() : null,
error: this.error()
};
return simpleObject;
},
/**
* Convert a serializable exception object created from toSerializableObject into a JSON string.
* @return {string} JSON string of serializable exception object
*/
toJSONString: function () {
var simpleObject = this.toSerializableObject();
return JSON.stringify(simpleObject);
},
toString: function () {
if (this.message()) {
return this.name() + " - " + this.message();
}
return this.name();
},
_getName: function () {
var data = this.data(),
error = this.error();
return (error.name && error.name !== utilities.functionName(error.constructor)) ? error.name : utilities.functionName(this.constructor);
},
_populateDefaultDataProperties: function () {
var data = this.data();
this._mergeErrorIntoData();
data.browser = data.browser || utilities.getBrowser();
data.url = data.url || window.location.href;
data.date = data.date || Date();
},
_mergeErrorIntoData: function () {
var data = this.data(),
error = this.error();
//Microsoft specific extension
if (error.description && !data.description) {
data.description = error.description;
}
if (error.number && !data.number) {
data.number = error.number;
}
//Mozilla specific extensions
if (error.fileName && !data.fileName) {
data.fileName = error.fileName;
}
if (error.lineNumber && !data.lineNumber) {
data.lineNumber = error.lineNumber;
}
if (error.columnNumber && !data.columnNumber) {
data.columnNumber = error.columnNumber;
}
if (error.toSource && !data.source) {
data.source = error.toSource();
}
//Note, we do not include error.message nor error.stack. We treat those differently
//because we consider them to be first order properties on an exception which don't
//need to live in the data object.
},
_report: function () {
if (this._guardedOptions.post()) {
this._post();
}
if (this._guardedOptions.postToExceptionsJsPlatform()) {
this._postToExceptionsJsPlatform();
}
if (this._guardedOptions.callback()) {
this._callback();
}
handler._pushReportedException(this);
console.log("exceptions.js exception: " + this.toString());
console.log(this.toSerializableObject());
},
_retrieveStacktrace: function () {
var stack = this.error().stack;
if (stack) {
return this.error().stack;
}
if (window.printStackTrace) {
return window.printStackTrace().join("\n");
}
if (!handler._attemptedToLoadStacktraceJs) {
handler.loadStacktraceJs();
}
return "Unable to retrieve stacktrace";
},
_takeScreenshot: function() {
var callback, self = this;
if (!window.html2canvas && !handler._attemptedToLoadHtml2Canvas) {
handler.loadHtml2Canvas(function () {
self._takeScreenshot();
});
}
else if (!window.html2canvas) {
this._screenshotComplete = true;
}
else {
callback = function (canvas) {
this.data().screenshot = canvas.toDataURL("image/png");
this._screenshotComplete = true;
for (var i in this._listeners) {
this._listeners[i].call(self);
}
};
window.html2canvas(document.body, {
onrendered: function (canvas) {
try {
callback.call(self, canvas);
} catch (e) {
this._screenshotComplete = true;
console.log("Error in exceptions.js:");
console.log(e);
}
}
});
}
},
_post: function () {
var http = new window.XMLHttpRequest(),
postHeaders = exceptions.handler.postHeaders(),
jsonString = this.toJSONString(),
i;
http.open("POST", exceptions.handler.postUrl(), true);
if (postHeaders) {
for (i = 0; i < postHeaders.length; i += 1) {
http.setRequestHeader(postHeaders[i].bstrHeader, postHeaders[i].bstrValue);
}
}
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
http.send("exception=" + encodeURIComponent(jsonString));
},
_postToExceptionsJsPlatform: function () {
var http = new window.XMLHttpRequest(),
postHeaders = exceptions.handler.postHeaders(),
jsonString = this.toJSONString(),
clientId = handler.clientId(),
to = handler.to(),
i;
http.open("POST", "https://www.platform.exceptionsjs.com/v0.1/reportWithClientId/", true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
http.send("exception=" + encodeURIComponent(jsonString) +
"&clientId=" + encodeURIComponent(clientId) +
"&to=" + encodeURIComponent(to));
},
_callback: function () {
var callback = exceptions.handler.callback();
callback(this);
},
constructor: Exception
};
//setup
Exception.defaultOptionsFunc(function (o) {
return o.toggleAll(true);
});
/**
* ArgumentException inherits from Exception. It has the same
* static functions and methods as Exception. However, it's default type is
* "ArgumentException" rather than "Exception." Use ArgumentException to throw
* or report invalid arguments.
* @constructor
*/
ArgumentException = createCustomException({
exception: function ArgumentException(message, config) {
if (!(this instanceof ArgumentException)) {
return new ArgumentException(message, config);
}
Exception.call(this, message, config);
},
baseException: Exception
});
/**
* InvalidOperationException inherits from Exception. It has the
* same static functions and methods as Exception. However,
* it's default type is "InvalidOperationException" rather than "Exception."
* Use InvalidOperationException to throw or report invalid operations.
* @constructor
*/
InvalidOperationException = createCustomException({
exception: function InvalidOperationException(message, config) {
if (!(this instanceof InvalidOperationException)) {
return new InvalidOperationException(message, config);
}
Exception.call(this, message, config);
},
baseException: Exception
});
/**
* NotImplementedException inherits from Exception. It has the same
* static functions and methods as Exception. However, it's default
* type is "NotImplementedException" rather than "Exception." Use
* NotImplementedException to throw or report attempts of executed
* code that is not implemented.
* @constructor
*/
NotImplementedException = createCustomException({
exception: function NotImplementedException(message, config) {
if (!(this instanceof NotImplementedException)) {
return new NotImplementedException(message, config);
}
Exception.call(this, message, config);
},
baseException: Exception
});
EvalException = createCustomException({
exception: function EvalException(message, config) {
if (!(this instanceof EvalException)) {
return new EvalException(message, config);
}
var error = (message instanceof Error) ? message : new EvalError(message);
Exception.call(this, error, config);
},
baseException: Exception
});
RangeException = createCustomException({
exception: function RangeException(message, config) {
if (!(this instanceof RangeException)) {
return new RangeException(message, config);
}
var error = (message instanceof Error) ? message : new RangeError(message);
Exception.call(this, error, config);
},
baseException: Exception
});
ReferenceException = createCustomException({
exception: function ReferenceException(message, config) {
if (!(this instanceof ReferenceException)) {
return new ReferenceException(message, config);
}
var error = (message instanceof Error) ? message : new ReferenceError(message);
Exception.call(this, error, config);
},
baseException: Exception
});
SyntaxException = createCustomException({
exception: function SyntaxException(message, config) {
if (!(this instanceof SyntaxException)) {
return new SyntaxException(message, config);
}
var error = (message instanceof Error) ? message : new SyntaxError(message);
Exception.call(this, error, config);
},
baseException: Exception
});
TypeException = createCustomException({
exception: function TypeException(message, config) {
if (!(this instanceof TypeException)) {
return new TypeException(message, config);
}
var error = (message instanceof Error) ? message : new TypeError(message);
Exception.call(this, error, config);
},
baseException: Exception
});
URIException = createCustomException({
exception: function URIException(message, config) {
if (!(this instanceof URIException)) {
return new URIException(message, config);
}
var error = (message instanceof Error) ? message : new URIError(message);
Exception.call(this, error, config);
},
baseException: Exception
});
/**
* Performing exception operations can be expensive or superfluous sometimes. For example, you may
* not want to take a screenshot of your page if you've hit 10 errors in a row because it could cause
* noticable performance errors. Specify a guard with exceptions.handler.guard() to disable exception
* options you do not wish to perform. The guard restricts options for all reported exceptions.
* exceptions.js does not expose a way to create a Guard object. Instead, it passes a Guard object to
* the guardFunc specified in handler.guard. The guardFunc is expected to manipulate and return the Guard.
*/
function Guard() {
if (!(this instanceof Guard)) {
return new Guard();
}
this._restrictions = [];
}
Guard.prototype = {
_restrictByAvailableOptions: function (o, exception) {
if (!exception.error().stack && !exceptions.handler.stacktraceUrl()){
o.stacktrace(false);
}
if (!exceptions.handler.html2canvasUrl() || !window.getComputedStyle){
o.screenshot(false);
}
if (!exceptions.handler.postUrl()){
o.post(false);
}
if (!exceptions.handler.callback()){
o.callback(false);
}
if (!exceptions.handler.clientId()) {
o.postToExceptionsJsPlatform(false);
}
return o;
},
/**
* Disable Exception options if the exception reported count threshold has been exceeded.
* See handler.reportedExceptions for more information about how we defined a reported exception.
* @param {int} - Threshold that must not be exceed lest you'll disable Exception options.
* @param {int} - Last number of seconds for which we care to count exceptions. If not
* specified, we'll use the total number of exceptions reported since the exception
* handler was setup.
* @param {function} - function that enables/disables and returns the options if the
* exception threshold has been exceeded. If not specified, we'll disable all
* options for the Exception. You'll likely only want to disable options in this
* function
*/
restrictByExceptionsCount: function (count, seconds, optionsFunc) {
var restrictFunc = function (o) {
var exceptionsCount = handler.retrieveReportedExceptionsCount(seconds);
if (exceptionsCount > count) {
o = optionsFunc ? optionsFunc(o) : function (o) { return o.toggleAll(false); };
}
return o;
};
this._restrictions.push(restrictFunc);
return this;
},
/**
* Disable Exception options with a specified restriction function. Note: see
* window.handler.retrieveReportedExceptionsCount and window.handler.reportedExceptions
* for a convient utilities.
* @param {function} Function that disables Exception options and returns the options object.
* The function will receive two parameters: the current options for the Exception and
* the Exception itself. It should return the provided
*/
restrictBy: function (restrictFunc) {
this._restrictions.push(restrictFunc);
return this;
},
_restrict: function (o, exception) {
for(var i in this._restrictions) {
this._restrictions[i](o, exception);
}
this._restrictByAvailableOptions(o, exception);
return o;
}
};
_scopeOption = {
none: 0,
exceptions: 1,
all: 2
};
/**
* Handler as exposed by exceptions.handler. The handler is responsible for handling errors thrown that
* hit window.onerror and specifying global configurations including the stacktrace.js url, html2canvas.js url,
* post url (to make a post request when an error is reported), post headers, callback (function executed when
* an error is reported).
*/
handler = {
_guard: null,
_postUrl: null,
_postHeaders: null,
_html2canvasUrl: null,
_stacktraceUrl: null,
_callback: null,
_isSetup: false,
_scope: _scopeOption.all,
_reportedExceptions: [],
_clientId: null,
_to: null,
/**
* Scope options for the handler. Options are none, exceptions, and all. Setting the scope to none signals that the handler
* won't handle anything in window.onerror. Setting the scope to exceptions signals that the handler
* will handle only thrown Exceptions, nothing else that is thrown. Setting the scope to all signals that
* the handler will handle everything in window.onerror.
*/
scopeOption: _scopeOption,
/**
* Get or set the scope of the handler when executed in window.onerror. Scope refers to scopeOption
* which has three options: none, exceptions, and all. Setting the scope to none signals that the handler
* won't handle anything in window.onerror. Setting the scope to exceptions signals that the handler
* will handle only thrown Exceptions, nothing else that is thrown. Setting the scope to all signals that
* the handler will handle everything in window.onerror.
* @param {int} Set the handler scope if specified. Use window.handler.scopeOption.
* @return The handler if scope is defined, value of the handler scope is not defined.
*/
scope: function (scope) {
if (scope !== undefined) {
handler._scope = scope;
return handler;
}
return handler._scope;
},
/**
* Get or set a guard that will be used to restrict Exception options.
* @param {function} Function that receives one parameter: Guard and should return
* the received Guard.
* @return The handler if guardFunc is defined. Handler's guard if guardFunc is not defined.
*/
guard: function (guardFunc) {
if (guardFunc) {
handler._guard = guardFunc(new Guard());
return this;
}
else {
return handler._guard;
}
},
/**
* Get or set url that pulls html2canvas.js.
* @param {string} url to html2canvas.js
* @return Handler if html2canvasUrl is defined. Url to html2canvas.js if the html2canvasUrl is not defined.
*/
html2canvasUrl: function (html2canvasUrl) {
if (html2canvasUrl) {
handler._html2canvasUrl = html2canvasUrl;
return handler;
}
else {
return handler._html2canvasUrl;
}
},
/**
* Get or set url that pulls stacktrace.js.
* @param {string} url to stacktrace.js
* @return Handler if stacktraceUrl is defined. Url to stacktrace.js if stacktraceUrl is not defined.
*/
stacktraceUrl: function (stacktraceUrl) {
if (stacktraceUrl) {
handler._stacktraceUrl = stacktraceUrl;
return handler;
}
else {
return handler._stacktraceUrl;
}
},
/**
* Get or set url used to post the serialized exception when reported.
* @param {string} post request url
* @return Handler if postUrl is defined. Url for post request if postUrl is not defined.
*/
postUrl: function (postUrl) {
if (postUrl) {
handler._postUrl = postUrl;
return handler;
}
else {
return handler._postUrl;
}
},
/**
* Get or set HTTP headers used to post the serialized exception when reported.
* @param {array} Array of objects with the form { bstrHeader: "header", bstrValue: "value" }
* @return Handler if postHeaders is defined. Post headers for post request if postHeaders is not defined.
*/
postHeaders: function (postHeaders) {
if (postHeaders) {
handler._postHeaders = postHeaders;
return handler;
}
else {
return handler._postHeaders;
}
},
/**
* Enable posting to exceptionsjs platform. The exceptionsjs platform handles
* your Javascript error by parsing the serialized exception and constructing a
* useful exception email that includes stacktraces, screenshots, and extra information.
* Register for exceptionsjs platform at https://exceptionsjs.com. This option only works
* if you've enabled the option to allow unsecure reporting. If you have enabled secure
* reporting you must send your exceptions to excpetionsjs platform using the full oauth2
* process. See https://exceptionsjs.com for useful libraries in many languages that make
* submitting exceptions with the full oauth2 process easy.
* @param {string} clientId that will be used with exceptionsjs platform
* @param {string} post request url
* @return Handler if postUrl is defined. Url for post request if postUrl is not defined.
*/
postToExceptionsJsPlatform: function (clientId, to) {
if (clientId !== undefined) {
handler.clientId(clientId);
if (to) {
handler.to(to);
}
return handler;
}
return handler.clientId();
},
clientId: function (clientId) {
if (clientId) {
handler._clientId = clientId;
return handler;
}
return handler._clientId;
},
to: function (to) {
if (to) {
handler._to = to;
return handler;
}
return handler._to;
},
/**
* Get or set callback that will be executed when an Exception is reported.
* @param {function} callback that will be executed when the Exception is reported.
* @return Handler if callback is defined. Callback if callback is not defined.
*/
callback: function (callback) {
if (callback) {
handler._callback = callback;
return handler;
}
else {
return handler._callback;
}
},
/**
* Asynchronously load stacktrace.js
*/