crashOverlay.js 27.3 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046

  var iframe = window.document.createElement('iframe');
  applyStyles(iframe, iframeStyle);
  iframeReference = iframe;
  iframe.onload = function() {
    if (iframeReference == null) {
      return;
    }
    var w = iframeReference.contentWindow;
    var document = iframeReference.contentDocument;

    var _createOverlay = createOverlay(
      document,
      name,
      message,
      resolvedFrames,
      CONTEXT_SIZE,
      currReferenceIndex + 1,
      errorReferences.length,
      function(offset) {
        switchError(offset);
      },
      function() {
        unmount();
      }
    ),
      overlay = _createOverlay.overlay,
      additional = _createOverlay.additional;

    if (w != null) {
      w.onkeydown = function(event) {
        keyHandler(
          function(type) {
            return shortcutHandler(type);
          },
          event
        );
      };
    }
    injectCss(iframeReference.contentDocument, css);
    if (document.body != null) {
      document.body.appendChild(overlay);
    }
    additionalReference = additional;
  };
  window.document.body.appendChild(iframe);
1047
1048
}

1049
1050
1051
1052
1053
1054
1055
function renderErrorByIndex(index) {
  currReferenceIndex = index;

  var _getErrorRecord = getErrorRecord(errorReferences[index]),
    error = _getErrorRecord.error,
    unhandledRejection = _getErrorRecord.unhandledRejection,
    enhancedFrames = _getErrorRecord.enhancedFrames;
1056
1057

  if (unhandledRejection) {
1058
1059
1060
    render(
      'Unhandled Rejection (' + error.name + ')',
      error.message,
1061
      enhancedFrames
1062
    );
1063
  } else {
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
    render(error.name, error.message, enhancedFrames);
  }
}

function switchError(offset) {
  var nextView = currReferenceIndex + offset;
  if (nextView < 0 || nextView >= errorReferences.length) {
    return;
  }
  renderErrorByIndex(nextView);
}

function disposeCurrentView() {
  if (iframeReference === null) {
    return;
1079
  }
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
  window.document.body.removeChild(iframeReference);
  iframeReference = null;
  additionalReference = null;
}

function unmount() {
  disposeCurrentView();
  drain();
  errorReferences = [];
  currReferenceIndex = -1;
1090
1091
1092
}

function crash(error) {
1093
1094
1095
  var unhandledRejection = arguments.length > 1 && arguments[1] !== undefined
    ? arguments[1]
    : false;
1096

1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
  if (module.hot && typeof module.hot.decline === 'function') {
    module.hot.decline();
  }
  consume(error, unhandledRejection, CONTEXT_SIZE)
    .then(function(ref) {
      errorReferences.push(ref);
      if (iframeReference !== null && additionalReference !== null) {
        updateAdditional(
          iframeReference.contentDocument,
          additionalReference,
          currReferenceIndex + 1,
          errorReferences.length,
          function(offset) {
            switchError(offset);
1111
          }
1112
1113
1114
1115
        );
      } else {
        if (errorReferences.length !== 1) {
          throw new Error('Something is *really* wrong.');
1116
        }
1117
        renderErrorByIndex((currReferenceIndex = 0));
1118
1119
1120
      }
    })
    .catch(function(e) {
1121
      console.log('Could not consume error:', e);
1122
    });
1123
1124
}

1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
function shortcutHandler(type) {
  switch (type) {
    case SHORTCUT_ESCAPE: {
      unmount();
      break;
    }
    case SHORTCUT_LEFT: {
      switchError(-1);
      break;
    }
    case SHORTCUT_RIGHT: {
      switchError(1);
      break;
1138
1139
    }
  }
1140
}
1141

1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
function inject() {
  registerUnhandledError(window, function(error) {
    return crash(error);
  });
  registerUnhandledRejection(window, function(error) {
    return crash(error, true);
  });
  registerShortcuts(window, shortcutHandler);
  registerStackTraceLimit();
}
1152

1153
1154
1155
1156
1157
1158
function uninject() {
  unregisterStackTraceLimit();
  unregisterShortcuts(window);
  unregisterUnhandledRejection(window);
  unregisterUnhandledError(window);
}
1159

1160
1161
inject();
if (module.hot && typeof module.hot.dispose === 'function') {
1162
  module.hot.dispose(function() {
1163
    uninject();
1164
1165
  });
}
For faster browsing, not all history is shown. View entire blame