@@ -474,6 +474,39 @@ var escapeHTML = function(s) {
474
474
});
475
475
}
476
476
477
+ var getErrorMessage = function(error, fromUnhandledRejection) {
478
+ var message = fromUnhandledRejection ? 'Promise rejected: ' : '';
479
+
480
+ if (error.stack) {
481
+ // Replace blob url with 'example'
482
+ var stack = error.stack.replace(/blob:https?:\/\/.*?\/[^:]*/g, 'example')
483
+
484
+ // FireFox/Safari style error
485
+ if (/@/.test(stack)) {
486
+ // Strip asyncFunctionResume added by Safari
487
+ console.log('here', JSON.stringify(stack));
488
+ stack = stack.replace(/\s*asyncFunctionResume.*\n.*/g, '');
489
+ // Indent
490
+ stack = stack.replace(/^/gm, ' ');
491
+ // Prepend name/message
492
+ message += error.name + ': ' + error.message + '\n' + stack;
493
+ }
494
+ else {
495
+ message += stack;
496
+ }
497
+ }
498
+ else if (error.name) {
499
+ message += error.name + ': ' + error.message;
500
+ }
501
+ else {
502
+ message += String(error);
503
+ }
504
+ if (error.cause) {
505
+ message += '\nCaused by: ' + getErrorMessage(error.cause);
506
+ }
507
+ return message;
508
+ }
509
+
477
510
var lastRun;
478
511
var recordRun = function(prop) {
479
512
if (!init && lastRun !== prop) {
@@ -576,15 +609,15 @@ var executeExample = function(example, prop) {
576
609
}
577
610
};
578
611
579
- iframe.contentWindow.javascriptureReportError = function(error) {
580
- errormessage.innerHTML = escapeHTML (error);
612
+ iframe.contentWindow.javascriptureReportError = function(error, fromUnhandledRejection ) {
613
+ errormessage.innerHTML = getErrorMessage (error, fromUnhandledRejection );
581
614
errormessage.style.display = 'block';
582
615
};
583
616
584
617
doc.writeln('<!DOCTYPE html>');
585
618
doc.writeln('<script >' );
586
- doc.writeln(' window .onerror = function (err ) { javascriptureReportError (err); };' );
587
- doc.writeln(' window .onunhandledrejection = function (evt ) { javascriptureReportError (" Promise rejected: " + evt .reason ); evt .preventDefault (); };' );
619
+ doc.writeln(' window .onerror = function (msg , file , line , column , err ) { javascriptureReportError (err || msg ); };' );
620
+ doc.writeln(' window .onunhandledrejection = function (evt ) { javascriptureReportError (evt .reason , true ); evt .preventDefault (); };' );
588
621
doc.writeln(' </script >');
589
622
590
623
var scriptBody;
@@ -666,15 +699,15 @@ var executeHTMLExample = function(example, prop) {
666
699
667
700
errormessage.style.display = 'none';
668
701
669
- iframe.contentWindow.javascriptureReportError = function(error) {
670
- errormessage.innerHTML = escapeHTML (error);
702
+ iframe.contentWindow.javascriptureReportError = function(error, fromUnhandledRejection ) {
703
+ errormessage.innerHTML = getErrorMessage (error, fromUnhandledRejection );
671
704
errormessage.style.display = 'block';
672
705
};
673
706
674
707
doc.writeln('<!DOCTYPE html>');
675
708
doc.writeln('<script >' );
676
- doc.writeln(' window .onerror = function (err ) { javascriptureReportError (err); };' );
677
- doc.writeln(' window .onunhandledrejection = function (evt ) { javascriptureReportError (" Promise rejected: " + evt .reason ); evt .preventDefault (); };' );
709
+ doc.writeln(' window .onerror = function (msg , file , line , column , err ) { javascriptureReportError (err || msg ); };' );
710
+ doc.writeln(' window .onunhandledrejection = function (evt ) { javascriptureReportError (evt .reason , true ); evt .preventDefault (); };' );
678
711
doc.writeln(' </script >');
679
712
doc.writeln(code.value);
680
713
doc.close();
0 commit comments