Skip to content

Commit 3691120

Browse files
committed
You can now get javascript stack trace, see: [cefpython].GetJavascriptStackTrace() and [cefpython].GetJavascriptStackTraceFormatted().
1 parent 7af7539 commit 3691120

File tree

7 files changed

+70
-13
lines changed

7 files changed

+70
-13
lines changed

cefexample/_changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Version 0.42 not yet released.
22
* Rebind/reload example added to cefadvanced.py.
33
* en-US locale was outdated, came from revision `607`, locales/ directory contains all locales now, the default one is "en-US", others are optional and do not need to be distributed.
4+
* OnConsoleMessage example added to cefadvanced.py
5+
* You can now get javascript stack trace, see: [cefpython].GetJavascriptStackTrace() and [cefpython].GetJavascriptStackTraceFormatted().
46

57
Version 0.41 released on 2012-09-14.
68
* New options in [ApplicationSettings]: auto_detect_proxy_settings_enabled, pack_loading_disabled.

cefexample/cefadvanced.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ def HandleJavascriptError(errorMessage, url, lineNumber):
140140
url = re.sub(r"[/\\]+", re.escape(os.sep), url)
141141
url = re.sub(r"%s" % re.escape(cefpython.GetRealPath()), "", url, flags=re.IGNORECASE)
142142
url = re.sub(r"^%s" % re.escape(os.sep), "", url)
143-
raise Exception("%s\n in %s on line %s" % (errorMessage, url, lineNumber))
143+
stackTrace = cefpython.GetJavascriptStackTraceFormatted()
144+
raise Exception("%s\n in %s on line %s\n\n%s" % (errorMessage, url, lineNumber, stackTrace))
144145

145146
class Python:
146147

@@ -291,6 +292,9 @@ def OnConsoleMessage(self, browser, message, source, line):
291292
appdir = appdir[0].upper() + appdir[1:]
292293
source = source.replace("file:///", "")
293294
source = source.replace(appdir, "")
295+
#if (message.lower().find("error") != -1): # DOESN'T WORK, stack trace is empty in this context.
296+
#stackTrace = cefpython.GetJavascriptStackTraceFormatted()
297+
#raise Exception("%s\n in %s on line %s\n\n%s" % (message, source, line, stackTrace))
294298
print("Console message: %s (%s:%s)\n" % (message, source, line));
295299
return False
296300

cefpython.pyx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,34 @@ def IsKeyModifier(key, modifiers):
258258
return ((KEY_SHIFT | KEY_CTRL | KEY_ALT) & modifiers) == 0
259259
# Same as: return (KEY_CTRL & modifiers) != KEY_CTRL and (KEY_ALT & modifiers) != KEY_ALT and (KEY_SHIFT & modifiers) != KEY_SHIFT
260260
return (key & modifiers) == key
261+
262+
def GetJavascriptStackTrace(frameLimit=100):
263+
264+
cdef CefRefPtr[CefV8StackTrace] cefTrace = cef_v8_stack.GetCurrent(int(frameLimit))
265+
cdef int frameCount = (<CefV8StackTrace*>(cefTrace.get())).GetFrameCount()
266+
cdef CefRefPtr[CefV8StackFrame] cefFrame
267+
cdef CefV8StackFrame* framePtr
268+
pyTrace = []
269+
270+
for frameNo in range(0, frameCount):
271+
cefFrame = (<CefV8StackTrace*>(cefTrace.get())).GetFrame(frameNo)
272+
framePtr = <CefV8StackFrame*>(cefFrame.get())
273+
pyFrame = {}
274+
pyFrame["script"] = CefStringToPyString(framePtr.GetScriptName())
275+
pyFrame["scriptOrSourceURL"] = CefStringToPyString(framePtr.GetScriptNameOrSourceURL())
276+
pyFrame["function"] = CefStringToPyString(framePtr.GetFunctionName())
277+
pyFrame["line"] = framePtr.GetLineNumber()
278+
pyFrame["column"] = framePtr.GetColumn()
279+
pyFrame["isEval"] = framePtr.IsEval()
280+
pyFrame["isConstructor"] = framePtr.IsConstructor()
281+
pyTrace.append(pyFrame)
282+
283+
return pyTrace
284+
285+
def GetJavascriptStackTraceFormatted(frameLimit=100):
286+
287+
trace = GetJavascriptStackTrace(frameLimit)
288+
formatted = "Stack trace:\n"
289+
for frameNo, frame in enumerate(trace):
290+
formatted += "\t[%s] %s() in %s on line %s (col:%s)\n" % (frameNo, frame["function"], frame["scriptOrSourceURL"], frame["line"], frame["column"])
291+
return formatted

imports.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ cimport cef_types # cannot cimport *, that would cause name conflicts with const
6161
cimport cef_types_win # same as cef_types.
6262
from cef_v8 cimport *
6363
cimport cef_v8_static
64+
cimport cef_v8_stack
6465
from v8functionhandler cimport *
6566
from cef_request cimport *
6667
from cef_response cimport *

pyinclude/cef_v8.pxd

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,18 @@ cdef extern from "include/cef_v8.h":
7979
CefRefPtr[CefV8Exception] GetException()
8080
cbool ClearException()
8181

82-
cdef cppclass CefV8StackTrace(CefBase):
83-
pass
82+
cdef cppclass CefV8StackTrace(CefBase):
83+
84+
int GetFrameCount()
85+
CefRefPtr[CefV8StackFrame] GetFrame(int index)
8486

8587
cdef cppclass CefV8StackFrame(CefBase):
86-
pass
87-
88-
89-
88+
89+
CefString GetScriptName()
90+
CefString GetScriptNameOrSourceURL()
91+
CefString GetFunctionName()
92+
int GetLineNumber()
93+
int GetColumn()
94+
cbool IsEval()
95+
cbool IsConstructor()
9096

pyinclude/cef_v8_stack.pxd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2012 CefPython Authors. All rights reserved.
2+
# License: New BSD License.
3+
# Website: http://code.google.com/p/cefpython/
4+
5+
from cef_ptr cimport CefRefPtr
6+
from cef_v8 cimport CefV8StackTrace
7+
8+
# Importing static methods only in this file. This is in a separate file as we do not want
9+
# these names to be imported into global namespace, you will be using them like this:
10+
# > cimport cef_v8_stack
11+
# > cef_v8_stack.GetCurrent()
12+
13+
cdef extern from "include/cef_v8.h" namespace "CefV8StackTrace":
14+
15+
cdef CefRefPtr[CefV8StackTrace] GetCurrent(int frame_limit)

pyinclude/cef_v8_static.pxd

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ from cef_v8 cimport CefV8Context
1111
from cef_base cimport CefBase
1212
from libcpp cimport bool as cbool
1313

14-
# Importing static methods.
15-
# This is in a separate file as we do not want these names to be imported
16-
# into global namespace, you will be using them like this:
17-
#
18-
# cimport cef_v8_static
19-
# cef_v8_static.CreateArray()
14+
# Importing static methods only in this file. This is in a separate file as we do not want
15+
# these names to be imported into global namespace, you will be using them like this:
16+
# > cimport cef_v8_static
17+
# > cef_v8_static.CreateArray()
2018

2119
cdef extern from "include/cef_v8.h" namespace "CefV8Value":
2220

0 commit comments

Comments
 (0)