Skip to content

Commit a27312d

Browse files
feat(events): add onError
1 parent 625798b commit a27312d

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

src/executable-code/executable-fragment.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
242242

243243
execute() {
244244
const {
245-
onOpenConsole, targetPlatform, waitingForOutput, compilerVersion, onRun,
245+
onOpenConsole, targetPlatform, waitingForOutput, compilerVersion, onRun, onError,
246246
args, theme, hiddenDependencies, onTestPassed, onTestFailed, onCloseConsole, jsLibs, outputHeight, getJsCode
247247
} = this.state;
248248
if (waitingForOutput) {
@@ -270,6 +270,7 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
270270
} else {
271271
if (onCloseConsole) onCloseConsole();
272272
}
273+
if ((state.errors.length > 0 || state.exception) && onError) onError();
273274
this.update(state);
274275
},
275276
() => this.update({waitingForOutput: false})
@@ -284,13 +285,14 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
284285
if (getJsCode) getJsCode(jsCode);
285286
let errors = state.errors.filter(error => error.severity === "ERROR");
286287
if (errors.length > 0) {
288+
if (onError) onError();
287289
state.output = processErrors(errors);
288290
state.openConsole = true;
289291
state.exception = null;
290292
this.update(state);
291293
} else {
292294
this.jsExecutor.executeJsCode(jsCode, jsLibs, targetPlatform,
293-
outputHeight, theme).then(output => {
295+
outputHeight, theme, onError).then(output => {
294296
if (output) {
295297
state.openConsole = true;
296298
state.output = output;

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ polyfill();
1818
* @property {Function} onTestPassed
1919
* @property {Function} onTestFailed
2020
* @property {Function} onRun
21+
* @property {Function} onError
2122
* @property {Function} onConsoleOpen
2223
* @property {Function} onConsoleClose
2324
* @property {Function} callBack

src/js-executor/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,29 @@ export default class JsExecutor {
1212
this.kotlinVersion = kotlinVersion;
1313
}
1414

15-
async executeJsCode(jsCode, jsLibs, platform, outputHeight, theme) {
15+
async executeJsCode(jsCode, jsLibs, platform, outputHeight, theme, onError) {
1616
if (platform === TargetPlatform.CANVAS) {
1717
this.iframe.style.display = "block";
1818
if (outputHeight) this.iframe.style.height = `${outputHeight}px`;
1919
}
20-
return await this.execute(jsCode, jsLibs, theme);
20+
return await this.execute(jsCode, jsLibs, theme, onError);
2121
}
2222

23-
async execute(jsCode, jsLibs, theme) {
23+
async execute(jsCode, jsLibs, theme, onError) {
2424
const loadedScripts = (this.iframe.contentDocument || this.iframe.document).getElementsByTagName('script').length;
2525
// 2 scripts by default: INIT_SCRIPT + kotlin stdlib
2626
if (loadedScripts === jsLibs.size + 2) {
2727
try {
2828
const output = this.iframe.contentWindow.eval(jsCode);
2929
return output ? `<span class="standard-output ${theme}">${processingHtmlBrackets(output)}</span>` : "";
3030
} catch (e) {
31+
if (onError) onError();
3132
let exceptionOutput = showJsException(e);
3233
return `<span class="error-output">${exceptionOutput}</span>`;
3334
}
3435
}
3536
await this.timeout(400);
36-
return await this.execute(jsCode, jsLibs, theme);
37+
return await this.execute(jsCode, jsLibs, theme, onError);
3738
}
3839

3940
timeout(ms) {

0 commit comments

Comments
 (0)