Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Commit 0632300

Browse files
Merge branch 'master' into tests
2 parents 14fa758 + 6f2fb7f commit 0632300

File tree

8 files changed

+521
-887
lines changed

8 files changed

+521
-887
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "CodeQL"
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
# The branches below must be a subset of the branches above
8+
branches: [master]
9+
schedule:
10+
- cron: '0 11 * * 0'
11+
12+
jobs:
13+
analyse:
14+
name: Analyse
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v2
20+
with:
21+
# We must fetch at least the immediate parents so that if this is
22+
# a pull request then we can checkout the head.
23+
fetch-depth: 2
24+
25+
# If this run was triggered by a pull request event, then checkout
26+
# the head of the pull request instead of the merge commit.
27+
- run: git checkout HEAD^2
28+
if: ${{ github.event_name == 'pull_request' }}
29+
30+
# Initializes the CodeQL tools for scanning.
31+
- name: Initialize CodeQL
32+
uses: github/codeql-action/init@v1
33+
# Override language selection by uncommenting this and choosing your languages
34+
with:
35+
languages: javascript, python
36+
37+
- name: Perform CodeQL Analysis
38+
uses: github/codeql-action/analyze@v1

eel/__init__.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from builtins import range
2+
import traceback
23
from io import open
34

45
from gevent.threading import Timer
@@ -262,16 +263,32 @@ def _repeated_send(ws, msg):
262263

263264
def _process_message(message, ws):
264265
if 'call' in message:
265-
return_val = _exposed_functions[message['name']](*message['args'])
266+
error_info = {}
267+
try:
268+
return_val = _exposed_functions[message['name']](*message['args'])
269+
status = 'ok'
270+
except Exception as e:
271+
err_traceback = traceback.format_exc()
272+
traceback.print_exc()
273+
return_val = None
274+
status = 'error'
275+
error_info['errorText'] = repr(e)
276+
error_info['errorTraceback'] = err_traceback
266277
_repeated_send(ws, _safe_json({ 'return': message['call'],
267-
'value': return_val }))
278+
'status': status,
279+
'value': return_val,
280+
'error': error_info,}))
268281
elif 'return' in message:
269282
call_id = message['return']
270283
if call_id in _call_return_callbacks:
271-
callback = _call_return_callbacks.pop(call_id)
272-
callback(message['value'])
284+
callback, error_callback = _call_return_callbacks.pop(call_id)
285+
if message['status'] == 'ok':
286+
callback(message['value'])
287+
elif message['status'] == 'error' and error_callback is not None:
288+
error_callback(message['error'], message['stack'])
273289
else:
274290
_call_return_values[call_id] = message['value']
291+
275292
else:
276293
print('Invalid message received: ', message)
277294

@@ -316,9 +333,9 @@ def _call_return(call):
316333
global _js_result_timeout
317334
call_id = call['call']
318335

319-
def return_func(callback=None):
336+
def return_func(callback=None, error_callback=None):
320337
if callback is not None:
321-
_call_return_callbacks[call_id] = callback
338+
_call_return_callbacks[call_id] = (callback, error_callback)
322339
else:
323340
for w in range(_js_result_timeout):
324341
if call_id in _call_return_values:

eel/eel.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ eel = {
7676
_call_return: function(call) {
7777
return function(callback = null) {
7878
if(callback != null) {
79-
eel._call_return_callbacks[call.call] = callback;
79+
eel._call_return_callbacks[call.call] = {resolve: callback};
8080
} else {
81-
return new Promise(function(resolve) {
82-
eel._call_return_callbacks[call.call] = resolve;
81+
return new Promise(function(resolve, reject) {
82+
eel._call_return_callbacks[call.call] = {resolve: resolve, reject: reject};
8383
});
8484
}
8585
}
@@ -131,13 +131,27 @@ eel = {
131131
if(message.hasOwnProperty('call') ) {
132132
// Python making a function call into us
133133
if(message.name in eel._exposed_functions) {
134-
let return_val = eel._exposed_functions[message.name](...message.args);
135-
eel._websocket.send(eel._toJSON({'return': message.call, 'value': return_val}));
134+
try {
135+
let return_val = eel._exposed_functions[message.name](...message.args);
136+
eel._websocket.send(eel._toJSON({'return': message.call, 'status':'ok', 'value': return_val}));
137+
} catch(err) {
138+
debugger
139+
eel._websocket.send(eel._toJSON(
140+
{'return': message.call,
141+
'status':'error',
142+
'error': err.message,
143+
'stack': err.stack}));
144+
}
136145
}
137146
} else if(message.hasOwnProperty('return')) {
138147
// Python returning a value to us
139148
if(message['return'] in eel._call_return_callbacks) {
140-
eel._call_return_callbacks[message['return']](message.value);
149+
if(message['status']==='ok'){
150+
eel._call_return_callbacks[message['return']].resolve(message.value);
151+
}
152+
else if(message['status']==='error' && eel._call_return_callbacks[message['return']].reject) {
153+
eel._call_return_callbacks[message['return']].reject(message['error']);
154+
}
141155
}
142156
} else {
143157
throw 'Invalid message ' + message;
@@ -146,7 +160,7 @@ eel = {
146160
};
147161
});
148162
}
149-
}
163+
};
150164

151165
eel._init();
152166

examples/02 - callbacks/callbacks.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,31 @@
77
def py_random():
88
return random.random()
99

10+
@eel.expose
11+
def py_exception(error):
12+
if error:
13+
raise ValueError("Test")
14+
else:
15+
return "No Error"
16+
1017
def print_num(n):
1118
print('Got this from Javascript:', n)
1219

20+
21+
def print_num_failed(error, stack):
22+
print("This is an example of what javascript errors would look like:")
23+
print("\tError: ", error)
24+
print("\tStack: ", stack)
25+
1326
# Call Javascript function, and pass explicit callback function
1427
eel.js_random()(print_num)
1528

1629
# Do the same with an inline callback
1730
eel.js_random()(lambda n: print('Got this from Javascript:', n))
1831

32+
# Show error handling
33+
eel.js_with_error()(print_num, print_num_failed)
34+
35+
1936
eel.start('callbacks.html', size=(400, 300))
37+

examples/02 - callbacks/web/callbacks.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
function js_random() {
1111
return Math.random();
1212
}
13+
14+
eel.expose(js_with_error);
15+
function js_with_error() {
16+
var test = 0;
17+
test.something("does not exist");
18+
}
1319

1420
function print_num(n) {
1521
console.log('Got this from Python: ' + n);
@@ -20,7 +26,25 @@
2026

2127
// Do the same with an inline callback
2228
eel.py_random()(n => console.log('Got this from Python: ' + n));
23-
29+
30+
// show usage with promises
31+
// show no error
32+
eel.py_exception(false)().then((result) => {
33+
// this will execute since we said no error
34+
console.log("No Error")
35+
}).catch((result) => {
36+
console.log("This won't be seen if no error")
37+
}
38+
);
39+
// show if an error occurrs
40+
eel.py_exception(true)().then((result) => {
41+
// this will not execute
42+
console.log("No Error")
43+
}).catch((result) => {
44+
console.log("This is the repr(e) for an exception " + result.errorText);
45+
console.log("This is the full traceback:\n" + result.errorTraceback);
46+
}
47+
)
2448
</script>
2549
</head>
2650

examples/07 - CreateReactApp/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)