forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.js
More file actions
200 lines (178 loc) · 4.98 KB
/
Copy patherror.js
File metadata and controls
200 lines (178 loc) · 4.98 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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// ReactJS
const PropTypes = require("prop-types");
// Utils
const { getGripType, isGrip, wrapRender } = require("./rep-utils");
const { cleanFunctionName } = require("./function");
const { isLongString } = require("./string");
const { MODE } = require("./constants");
const dom = require("react-dom-factories");
const { span } = dom;
const IGNORED_SOURCE_URLS = ["debugger eval code"];
/**
* Renders Error objects.
*/
ErrorRep.propTypes = {
object: PropTypes.object.isRequired,
// @TODO Change this to Object.values when supported in Node's version of V8
mode: PropTypes.oneOf(Object.keys(MODE).map(key => MODE[key]))
};
function ErrorRep(props) {
const object = props.object;
const preview = object.preview;
let name;
if (preview && preview.name && preview.kind) {
switch (preview.kind) {
case "Error":
name = preview.name;
break;
case "DOMException":
name = preview.kind;
break;
default:
throw new Error("Unknown preview kind for the Error rep.");
}
} else {
name = "Error";
}
const content = [];
if (props.mode === MODE.TINY) {
content.push(name);
} else {
content.push(`${name}: "${preview.message}"`);
}
if (preview.stack && props.mode !== MODE.TINY) {
content.push("\n", getStacktraceElements(props, preview));
}
return span(
{
"data-link-actor-id": object.actor,
className: "objectBox-stackTrace"
},
content
);
}
/**
* Returns a React element reprensenting the Error stacktrace, i.e.
* transform error.stack from:
*
* semicolon@debugger eval code:1:109
* jkl@debugger eval code:1:63
* asdf@debugger eval code:1:28
* @debugger eval code:1:227
*
* Into a column layout:
*
* semicolon (<anonymous>:8:10)
* jkl (<anonymous>:5:10)
* asdf (<anonymous>:2:10)
* (<anonymous>:11:1)
*/
function getStacktraceElements(props, preview) {
const stack = [];
if (!preview.stack) {
return stack;
}
const isStacktraceALongString = isLongString(preview.stack);
const stackString = isStacktraceALongString
? preview.stack.initial
: preview.stack;
stackString.split("\n").forEach((frame, index) => {
if (!frame) {
// Skip any blank lines
return;
}
let functionName;
let location;
// Given the input: "functionName@scriptLocation:2:100"
// Result: [
// "functionName@scriptLocation:2:100",
// "functionName",
// "scriptLocation:2:100"
// ]
const result = frame.match(/^(.*)@(.*)$/);
if (result && result.length === 3) {
functionName = result[1];
// If the resource was loaded by base-loader.js, the location looks like:
// resource://devtools/shared/base-loader.js -> resource://path/to/file.js .
// What's needed is only the last part after " -> ".
location = result[2].split(" -> ").pop();
}
if (!functionName) {
functionName = "<anonymous>";
}
let onLocationClick;
// Given the input: "scriptLocation:2:100"
// Result:
// ["scriptLocation:2:100", "scriptLocation", "2", "100"]
const locationParts = location.match(/^(.*):(\d+):(\d+)$/);
if (
props.onViewSourceInDebugger &&
location &&
!IGNORED_SOURCE_URLS.includes(locationParts[1]) &&
locationParts
) {
const [, url, line, column] = locationParts;
onLocationClick = e => {
// Don't trigger ObjectInspector expand/collapse.
e.stopPropagation();
props.onViewSourceInDebugger({
url,
line: Number(line),
column: Number(column)
});
};
}
stack.push(
span(
{
key: `fn${index}`,
className: "objectBox-stackTrace-fn"
},
cleanFunctionName(functionName)
),
span(
{
key: `location${index}`,
className: "objectBox-stackTrace-location",
onClick: onLocationClick,
title: onLocationClick
? `View source in debugger → ${location}`
: undefined
},
location
)
);
});
if (isStacktraceALongString) {
// Remove the last frame (i.e. 2 last elements in the array, the function
// name and the location) which is certainly incomplete.
// Can be removed when https://bugzilla.mozilla.org/show_bug.cgi?id=1448833
// is fixed.
stack.splice(-2);
}
return span(
{
key: "stack",
className: "objectBox-stackTrace-grid"
},
stack
);
}
// Registration
function supportsObject(object, noGrip = false) {
if (noGrip === true || !isGrip(object)) {
return false;
}
return (
(object.preview && getGripType(object, noGrip) === "Error") ||
object.class === "DOMException"
);
}
// Exports from this module
module.exports = {
rep: wrapRender(ErrorRep),
supportsObject
};