Skip to content

Commit 9a3bc6b

Browse files
author
Ives van Hoorne
committed
Enable runtime error transformation
1 parent cfbc4e7 commit 9a3bc6b

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

src/sandbox/errors/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function buildErrorMessage(e) {
3636
}
3737

3838
return {
39-
moduleId: e.module ? e.module.id : e.moduleId,
39+
moduleId: e.tModule ? e.tModule.module.id : e.moduleId,
4040
title,
4141
message,
4242
line: parseInt(line, 10),
@@ -81,9 +81,9 @@ function buildDynamicError(ref: ErrorRecord) {
8181
}
8282
} else {
8383
const error = ref.error;
84-
const module = error.module;
84+
const tModule = error.tModule;
8585

86-
if (module) {
86+
if (tModule) {
8787
const newError = {
8888
...buildErrorMessage(error),
8989
type: 'action',

src/sandbox/eval/transpiled-module.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export default class TranspiledModule {
357357
finalSourceMap = sourceMap;
358358
} catch (e) {
359359
e.fileName = loaderContext.path;
360-
e.module = this.module;
360+
e.tModule = this;
361361
this.resetTranspilation();
362362
throw e;
363363
}
@@ -459,24 +459,7 @@ export default class TranspiledModule {
459459
}
460460
return exports;
461461
} catch (e) {
462-
e.module = e.module || module;
463-
464-
try {
465-
const errorDecorations = transformError(
466-
e,
467-
this,
468-
manager.getTranspiledModules(),
469-
manager.getDirectories()
470-
);
471-
if (errorDecorations) {
472-
e.name = errorDecorations.name || e.name;
473-
e.message = errorDecorations.message;
474-
e.suggestions = errorDecorations.suggestions;
475-
}
476-
} catch (ex) {
477-
/* Decorating went wrong, don't throw */
478-
console.error(ex);
479-
}
462+
e.tModule = e.tModule || transpiledModule;
480463

481464
throw e;
482465
}

src/sandbox/react-error-overlay/overlay.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99

1010
/* @flow */
11+
import { transformError } from 'codesandbox-api';
12+
1113
import {
1214
register as registerError,
1315
unregister as unregisterError,
@@ -49,6 +51,7 @@ import { createOverlay } from './components/overlay';
4951
import { updateAdditional } from './components/additional';
5052

5153
import buildError from '../errors';
54+
import { getCurrentManager } from '../compile';
5255

5356
const CONTEXT_SIZE: number = 3;
5457
let iframeReference: HTMLIFrameElement | null = null;
@@ -166,6 +169,61 @@ function sendErrorsToEditor() {
166169
});
167170
}
168171

172+
/**
173+
* Transforms the error with give transformers to codesandbox-api, this adds
174+
* suggestions and can alter the error name + message.
175+
*/
176+
function transformErrors() {
177+
const manager = getCurrentManager();
178+
if (manager) {
179+
errorReferences.forEach(ref => {
180+
const errRef = getErrorRecord(ref);
181+
182+
const relevantFrame = errRef.enhancedFrames.find(r => {
183+
try {
184+
return (
185+
manager &&
186+
!!manager.resolveTranspiledModule(r._originalFileName || r.fileName)
187+
);
188+
} catch (e) {
189+
/* don't do anything */
190+
return false;
191+
}
192+
});
193+
194+
let tModule = errRef.error.tModule;
195+
196+
if (!tModule && relevantFrame) {
197+
const fileName =
198+
relevantFrame._originalFileName || relevantFrame.fileName;
199+
tModule = manager.resolveTranspiledModule(fileName);
200+
}
201+
202+
if (!tModule) {
203+
return;
204+
}
205+
206+
try {
207+
const transformation = transformError(
208+
errRef.error,
209+
tModule,
210+
manager.getTranspiledModules(),
211+
manager.getDirectories()
212+
);
213+
214+
if (transformation) {
215+
errRef.error.name = transformation.name || errRef.error.name;
216+
errRef.error.message = transformation.message;
217+
errRef.error.suggestions = transformation.suggestions;
218+
}
219+
} catch (ex) {
220+
/* just catch */
221+
console.error(ex);
222+
}
223+
});
224+
}
225+
}
226+
169227
function crash(error: Error, unhandledRejection = false) {
170228
if (module.hot && typeof module.hot.decline === 'function') {
171229
module.hot.decline();
@@ -180,6 +238,7 @@ function crash(error: Error, unhandledRejection = false) {
180238
errorReferences.push(ref);
181239

182240
sendErrorsToEditor();
241+
transformErrors();
183242
if (iframeReference !== null && additionalReference !== null) {
184243
updateAdditional(
185244
iframeReference.contentDocument,

0 commit comments

Comments
 (0)