فهرست منبع

show in-app error only in debug mode

wangweimin 3 سال پیش
والد
کامیت
3abc954180

+ 2 - 0
pywebio/platform/aiohttp.py

@@ -1,3 +1,4 @@
+import os
 import asyncio
 import fnmatch
 import json
@@ -200,6 +201,7 @@ def start_server(applications, port=0, host='', debug=False,
     if auto_open_webbrowser:
         asyncio.get_event_loop().create_task(open_webbrowser_on_server_started('localhost', port))
 
+    debug = Session.debug = os.environ.get('PYWEBIO_DEBUG', debug)
     if debug:
         logging.getLogger("asyncio").setLevel(logging.DEBUG)
 

+ 2 - 0
pywebio/platform/django.py

@@ -6,6 +6,7 @@ import threading
 from django.http import HttpResponse, HttpRequest
 
 from . import utils
+from ..session import Session
 from .httpbased import HttpContext, HttpHandler, run_event_loop
 from .remote_access import start_remote_access_service
 from .utils import make_applications, cdn_validation
@@ -118,6 +119,7 @@ def wsgi_app(applications, cdn=True,
     from django.views.static import serve
 
     cdn = cdn_validation(cdn, 'warn')
+    debug = Session.debug = os.environ.get('PYWEBIO_DEBUG', debug)
 
     max_payload_size = parse_file_size(max_payload_size)
     utils.MAX_PAYLOAD_SIZE = max_payload_size

+ 2 - 0
pywebio/platform/fastapi.py

@@ -1,3 +1,4 @@
+import os
 import asyncio
 import json
 import logging
@@ -201,6 +202,7 @@ def asgi_app(applications, cdn=True, static_dir=None, debug=False, allowed_origi
         You can install it with the following command:
             pip install aiofiles
         """.strip(), n=8)) from None
+    debug = Session.debug = os.environ.get('PYWEBIO_DEBUG', debug)
     cdn = cdn_validation(cdn, 'warn')
     if cdn is False:
         cdn = 'pywebio_static'

+ 3 - 0
pywebio/platform/flask.py

@@ -1,6 +1,7 @@
 """
 Flask backend
 """
+import os
 import json
 import logging
 import threading
@@ -8,6 +9,7 @@ import threading
 from flask import Flask, request, send_from_directory, Response
 
 from . import utils
+from ..session import Session
 from .httpbased import HttpContext, HttpHandler, run_event_loop
 from .remote_access import start_remote_access_service
 from .utils import make_applications, cdn_validation
@@ -159,6 +161,7 @@ def start_server(applications, port=8080, host='', cdn=True,
                    check_origin=check_origin, session_expire_seconds=session_expire_seconds,
                    session_cleanup_interval=session_cleanup_interval, max_payload_size=max_payload_size)
 
+    debug = Session.debug = os.environ.get('PYWEBIO_DEBUG', debug)
     if not debug:
         logging.getLogger('werkzeug').setLevel(logging.WARNING)
 

+ 2 - 0
pywebio/platform/tornado.py

@@ -347,6 +347,8 @@ def start_server(applications, port=0, host='',
 
     utils.MAX_PAYLOAD_SIZE = max_payload_size = parse_file_size(max_payload_size)
 
+    debug = Session.debug = os.environ.get('PYWEBIO_DEBUG', debug)
+
     # Since some cloud server may close idle connections (such as heroku),
     # use `websocket_ping_interval` to  keep the connection alive
     tornado_app_settings.setdefault('websocket_ping_interval', 30)

+ 4 - 0
pywebio/platform/tornado_http.py

@@ -1,3 +1,4 @@
+import os
 import json
 import logging
 
@@ -5,6 +6,7 @@ import tornado.ioloop
 import tornado.web
 
 from . import utils
+from ..session import Session
 from .httpbased import HttpContext, HttpHandler
 from .tornado import set_ioloop, _setup_server, open_webbrowser_on_server_started
 from .utils import cdn_validation
@@ -140,6 +142,8 @@ def start_server(applications, port=8080, host='',
 
     utils.MAX_PAYLOAD_SIZE = max_payload_size = parse_file_size(max_payload_size)
 
+    debug = Session.debug = os.environ.get('PYWEBIO_DEBUG', debug)
+
     tornado_app_settings.setdefault('websocket_max_message_size', max_payload_size)
     tornado_app_settings['websocket_max_message_size'] = parse_file_size(
         tornado_app_settings['websocket_max_message_size'])

+ 4 - 3
pywebio/session/base.py

@@ -46,6 +46,7 @@ class Session:
         后端Backend在接收到用户浏览器的数据后,会通过调用 ``send_client_event`` 来通知会话,进而由Session驱动协程的运行。
         Task内在调用输入输出函数后,会调用 ``send_task_command`` 向会话发送输入输出消息指令, Session将其保存并留给后端Backend处理。
     """
+    debug = False
 
     @staticmethod
     def get_current_session() -> "Session":
@@ -133,12 +134,12 @@ class Session:
 
         toast_msg = "应用发生内部错误" if 'zh' in session_info.user_language else "An internal error occurred in the application"
 
-        type, value, tb = sys.exc_info()
-        lines = traceback.format_exception(type, value, tb)
+        e_type, e_value, e_tb = sys.exc_info()
+        lines = traceback.format_exception(e_type, e_value, e_tb)
         traceback_msg = ''.join(lines)
 
         try:
-            if os.environ.get('PYWEBIO_POPUP_ERROR'):
+            if type(self).debug:
                 popup(title=toast_msg, content=put_error(traceback_msg), size=PopupSize.LARGE)
             else:
                 toast(toast_msg, duration=1, color='error')