/
index.js
152 lines (136 loc) · 3.76 KB
/
index.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
// @flow
import { dispatch, actions } from 'codesandbox-api';
import { getCurrentManager } from 'app/src/sandbox/compile';
import type TranspiledModule from 'app/src/sandbox/eval/transpiled-module';
import type { ErrorRecord } from 'sandbox-hooks/react-error-overlay/utils/errorRegister';
type TModuleError = Error & {
hideLine?: boolean,
line?: number,
path?: Object,
severity?: string,
payload?: Object,
tModule?: TranspiledModule,
};
function buildErrorMessage(e: TModuleError) {
const title = e.name;
const message = e.message;
let line = null;
let column = null;
if (!e.hideLine) {
// Safari
if (e.line != null) {
line = e.line;
// FF
} else if (e.lineNumber != null) {
line = e.lineNumber;
// Chrome
} else if (e.stack) {
const matched = e.stack.match(/<anonymous>:(\d+):(\d+)/);
if (matched) {
line = matched[1];
column = matched[2];
} else {
// Maybe it's a babel transpiler error
const babelMatched = e.stack.match(/(\d+):(\d+)/);
if (babelMatched) {
line = babelMatched[1];
column = babelMatched[2];
}
}
}
}
return {
path: e.tModule ? e.tModule.module.path : e.path,
title,
message,
line: parseInt(line, 10),
column: parseInt(column, 10),
payload: e.payload || {},
severity: e.severity || 'error',
};
}
const wrappedResolveModule = (manager, path) => {
try {
return manager && manager.resolveTranspiledModule(path, '/');
} catch (e) {
return null;
}
};
function buildDynamicError(ref: ErrorRecord) {
// TODO remove this logic for the SSE handler, move it to something more generic
// like a file store
const manager = getCurrentManager();
const relevantFrame = ref.enhancedFrames.find(r =>
wrappedResolveModule(
manager,
(r._originalFileName || r.fileName || '')
.replace(location.origin, '')
.replace('file://', '')
)
);
if (relevantFrame && manager) {
const fileName = relevantFrame._originalFileName || relevantFrame.fileName;
if (fileName) {
const tModule = manager.resolveTranspiledModule(
fileName.replace(location.origin, '').replace('file://', ''),
'/'
);
if (tModule) {
const module = tModule.module;
return {
type: 'action',
action: 'show-error',
path: module.parent ? module.parent.path : module.path,
title: ref.error.name,
message: ref.error.message,
line: relevantFrame._originalLineNumber,
column: relevantFrame._originalColumnNumber,
payload: {},
severity: 'error',
};
}
}
} else {
const error: TModuleError = ref.error;
const tModule =
error.tModule ||
wrappedResolveModule(
manager,
(error.fileName || '')
.replace(location.origin, '')
.replace('file://', '')
);
if (tModule) {
const newError = {
...buildErrorMessage(error),
path: error.fileName,
type: 'action',
action: 'show-error',
};
return newError;
}
}
return null;
}
/* eslint-disable no-underscore-dangle */
export default function showError(ref: ErrorRecord) {
const errorToSend = buildDynamicError(ref);
if (errorToSend) {
dispatch(
actions.error.show(errorToSend.title, errorToSend.message, {
line: errorToSend.line,
column: errorToSend.column,
path: errorToSend.path,
payload: errorToSend.payload,
})
);
} else {
// Show based on error
actions.error.show(ref.error.name, ref.error.message, {
line: ref.error.lineNumber,
column: ref.error.columnNumber,
path: ref.error.fileName,
payload: {},
});
}
}