Skip to content

Commit

Permalink
display exception from sync echo function #997
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Feb 22, 2025
1 parent f0a61a9 commit 2d02425
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![npm](https://img.shields.io/badge/npm-DEV-blue.svg)](https://www.npmjs.com/package/jquery.terminal)
![bower](https://img.shields.io/badge/bower-DEV-yellow.svg)
[![Build and test](https://github.com/jcubic/jquery.terminal/actions/workflows/build.yaml/badge.svg?branch=devel&event=push)](https://github.com/jcubic/jquery.terminal/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&f835e1e451a9a328fcbd9f39c4dc0103)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&1687bf76f899dace3e9c668c6d0414bf)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
![NPM Downloads](https://img.shields.io/npm/dm/jquery.terminal.svg?style=flat)
[![jsDelivr Downloads](https://data.jsdelivr.com/v1/package/npm/jquery.terminal/badge?style=rounded&n=1)](https://www.jsdelivr.com/package/npm/jquery.terminal)
[![Paid Support](https://img.shields.io/badge/paid-support-354465.svg)](https://support.jcubic.pl/)
Expand Down
43 changes: 43 additions & 0 deletions __tests__/__snapshots__/terminal.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,49 @@ exports[`Terminal plugin interprer return should rener promise of array 1`] = `
"foo" "bar" 10"
`;

exports[`Terminal plugin jQuery Terminal methods echo should show error in sync echo 1`] = `
"[[;;;terminal-error][ECHO]: x is not defined]
[[;;;terminal-error;ReferenceError: x is not defined]ReferenceError: x is not defined]
[[;;;terminal-error; at jQuery.fn.init.x (/home/kuba/projects/jcubic/terminal/repo/__tests__/terminal.spec.js:6953:21)] at jQuery.fn.init.x (/home/kuba/projects/jcubic/terminal/repo/__tests__/terminal.spec.js:6953:21]
[[;;;terminal-error; at jQuery.fn.init.x (/home/kuba/projects/jcubic/terminal/repo/__tests__/terminal.spec.js:6953:21)])]
[[;;;terminal-error; at value (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:12273:25)] at value (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:12273:25)]
[[;;;terminal-error; at stringify_value (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:8561:30)] at stringify_value (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:8561:30)]
[[;;;terminal-error; at process_line (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:11299:40)] at process_line (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:11299:40)]
[[;;;terminal-error; at callback (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:1208:24)] at callback (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:1208:24)]
[[;;;terminal-error; at unpromise (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:11280:25)] at unpromise (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:11280:25)]
[[;;;terminal-error; at jQuery.fn.init.echo (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:11359:21)] at jQuery.fn.init.echo (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:11359]
[[;;;terminal-error; at jQuery.fn.init.echo (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:11359:21)]:21)]
[[;;;terminal-error; at jQuery.fn.init.apply [as echo] (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:12093:32)] at jQuery.fn.init.apply [as echo] (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-s]
[[;;;terminal-error; at jQuery.fn.init.apply [as echo] (/home/kuba/projects/jcubic/terminal/repo/js/jquery.terminal-src.js:12093:32)]rc.js:12093:32)]
[[;;;terminal-error; at Object.echo (/home/kuba/projects/jcubic/terminal/repo/__tests__/terminal.spec.js:6952:22)] at Object.echo (/home/kuba/projects/jcubic/terminal/repo/__tests__/terminal.spec.js:6952:22)]
[[;;;terminal-error; at Promise.then.completed (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/utils.js:298:28)] at Promise.then.completed (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/bui]
[[;;;terminal-error; at Promise.then.completed (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/utils.js:298:28)]ld/utils.js:298:28)]
[[;;;terminal-error; at new Promise (<anonymous>)] at new Promise (<anonymous>)]
[[;;;terminal-error; at callAsyncCircusFn (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/utils.js:231:10)] at callAsyncCircusFn (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/ut]
[[;;;terminal-error; at callAsyncCircusFn (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/utils.js:231:10)]ils.js:231:10)]
[[;;;terminal-error; at _callCircusTest (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:316:40)] at _callCircusTest (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.]
[[;;;terminal-error; at _callCircusTest (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:316:40)]js:316:40)]
[[;;;terminal-error; at _runTest (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:252:3)] at _runTest (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:252:]
[[;;;terminal-error; at _runTest (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:252:3)]3)]
[[;;;terminal-error; at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:126:9)] at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/]
[[;;;terminal-error; at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:126:9)]build/run.js:126:9)]
[[;;;terminal-error; at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:121:9)] at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/]
[[;;;terminal-error; at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:121:9)]build/run.js:121:9)]
[[;;;terminal-error; at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:121:9)] at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/]
[[;;;terminal-error; at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:121:9)]build/run.js:121:9)]
[[;;;terminal-error; at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:121:9)] at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/]
[[;;;terminal-error; at _runTestsForDescribeBlock (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:121:9)]build/run.js:121:9)]
[[;;;terminal-error; at run (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:71:3)] at run (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/run.js:71:3)]
[[;;;terminal-error; at runAndTransformResultsToJestFormat (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)] at runAndTransformResultsToJestFormat (/home/kuba/projects/jcubic/terminal/repo/node_modules/jes]
[[;;;terminal-error; at runAndTransformResultsToJestFormat (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)]t-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)]
[[;;;terminal-error; at jestAdapter (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)] at jestAdapter (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/legacy-c]
[[;;;terminal-error; at jestAdapter (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)]ode-todo-rewrite/jestAdapter.js:79:19)]
[[;;;terminal-error; at runTestInternal (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-runner/build/runTest.js:367:16)] at runTestInternal (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-runner/build/runT]
[[;;;terminal-error; at runTestInternal (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-runner/build/runTest.js:367:16)]est.js:367:16)]
[[;;;terminal-error; at runTest (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-runner/build/runTest.js:444:34)] at runTest (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-runner/build/runTest.js:4]
[[;;;terminal-error; at runTest (/home/kuba/projects/jcubic/terminal/repo/node_modules/jest-runner/build/runTest.js:444:34)]44:34)]"
`;
exports[`Terminal plugin jQuery Terminal methods exec should invoke array of commands when each command pause terminal 1`] = `
"> exec_async_array
> async_command 0
Expand Down
7 changes: 7 additions & 0 deletions __tests__/terminal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6947,6 +6947,13 @@ describe('Terminal plugin', function() {
]);
});
});
it('should show error in sync echo', function() {
term.clear();
term.echo(function() {
x();
});
expect(term.get_output()).toMatchSnapshot();
});
});
describe('error', function() {
var term = $('<div/>').terminal($.noop, {
Expand Down
18 changes: 15 additions & 3 deletions js/jquery.terminal-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,15 @@
// when value is undefined
// when moving this line outside if
// it breaks all completion unit tests
return callback(value);
try {
return callback(value);
} catch(e) {

Check failure on line 1209 in js/jquery.terminal-src.js

View workflow job for this annotation

GitHub Actions / build

Expected space(s) after "catch"
if (is_function(error)) {
error(e);
} else {
throw e;
}
}
}
}
// -----------------------------------------------------------------------
Expand Down Expand Up @@ -8537,7 +8545,7 @@
return string;
}
// ---------------------------------------------------------------------
function process_line(line) {
function process_line(line, safe_throw) {
// prevent exception in display exception
try {
var use_cache = !is_function(line.value);
Expand Down Expand Up @@ -8625,6 +8633,10 @@
}
buffer.append(arg, line.index, line_settings, raw);
} catch (e) {
if (safe_throw) {
// exception from echo function is handled by echo #997
throw e;
}
buffer.clear();
// don't display exception if exception throw in terminal
if (is_function(settings.exceptionHandler)) {
Expand Down Expand Up @@ -11288,7 +11300,7 @@
value: value,
options: locals,
index: index
});
}, true);
// queue async functions in echo
if (is_promise(next)) {
echo_promise = true;
Expand Down
22 changes: 17 additions & 5 deletions js/jquery.terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* broken image by Sophia Bai from the Noun Project (CC-BY)
*
* Date: Sat, 22 Feb 2025 15:55:40 +0000
* Date: Sat, 22 Feb 2025 16:29:23 +0000
*/
/* global define, Map, BigInt */
/* eslint-disable */
Expand Down Expand Up @@ -1204,7 +1204,15 @@
// when value is undefined
// when moving this line outside if
// it breaks all completion unit tests
return callback(value);
try {
return callback(value);
} catch(e) {
if (is_function(error)) {
error(e);
} else {
throw e;
}
}
}
}
// -----------------------------------------------------------------------
Expand Down Expand Up @@ -5448,7 +5456,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: 'DEV',
date: 'Sat, 22 Feb 2025 15:55:40 +0000',
date: 'Sat, 22 Feb 2025 16:29:23 +0000',
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -8537,7 +8545,7 @@
return string;
}
// ---------------------------------------------------------------------
function process_line(line) {
function process_line(line, safe_throw) {
// prevent exception in display exception
try {
var use_cache = !is_function(line.value);
Expand Down Expand Up @@ -8625,6 +8633,10 @@
}
buffer.append(arg, line.index, line_settings, raw);
} catch (e) {
if (safe_throw) {
// exception from echo function is handled by echo #997
throw e;
}
buffer.clear();
// don't display exception if exception throw in terminal
if (is_function(settings.exceptionHandler)) {
Expand Down Expand Up @@ -11288,7 +11300,7 @@
value: value,
options: locals,
index: index
});
}, true);
// queue async functions in echo
if (is_promise(next)) {
echo_promise = true;
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/jquery.terminal.min.js.map

Large diffs are not rendered by default.

0 comments on commit 2d02425

Please sign in to comment.