fix: fail gracefully when livecode_to_svg crashes
That seems to be happening in some cases and couldn't really figure out the reason. Handling the error to gracefully to show an empty image in those cases.
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
import websocket
|
import websocket
|
||||||
import json
|
import json
|
||||||
from .svg import SVG
|
from .svg import SVG
|
||||||
|
import frappe
|
||||||
|
|
||||||
# Files to pass to livecode server
|
# Files to pass to livecode server
|
||||||
# The same code is part of livecode-canvas.js
|
# The same code is part of livecode-canvas.js
|
||||||
@@ -62,6 +63,7 @@ clear()
|
|||||||
def livecode_to_svg(livecode_ws_url, code, *, timeout=3):
|
def livecode_to_svg(livecode_ws_url, code, *, timeout=3):
|
||||||
"""Renders the code as svg.
|
"""Renders the code as svg.
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
ws = websocket.WebSocket()
|
ws = websocket.WebSocket()
|
||||||
ws.settimeout(timeout)
|
ws.settimeout(timeout)
|
||||||
ws.connect(livecode_ws_url)
|
ws.connect(livecode_ws_url)
|
||||||
@@ -82,6 +84,8 @@ def livecode_to_svg(livecode_ws_url, code, *, timeout=3):
|
|||||||
commands = [m for m in messages if m['msgtype'] == 'draw']
|
commands = [m for m in messages if m['msgtype'] == 'draw']
|
||||||
img = draw_image(commands)
|
img = draw_image(commands)
|
||||||
return img.tostring()
|
return img.tostring()
|
||||||
|
except websocket.WebSocketException as e:
|
||||||
|
frappe.log_error(frappe.get_traceback(), 'livecode_to_svg failed')
|
||||||
|
|
||||||
def _read_messages(ws):
|
def _read_messages(ws):
|
||||||
messages = []
|
messages = []
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ import frappe
|
|||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
from . import livecode
|
from . import livecode
|
||||||
|
|
||||||
|
DEFAULT_IMAGE = """
|
||||||
|
<svg viewBox="0 0 300 300" width="300" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
</svg>
|
||||||
|
"""
|
||||||
|
|
||||||
class LMSSketch(Document):
|
class LMSSketch(Document):
|
||||||
@property
|
@property
|
||||||
def sketch_id(self):
|
def sketch_id(self):
|
||||||
@@ -48,8 +53,9 @@ class LMSSketch(Document):
|
|||||||
else:
|
else:
|
||||||
ws_url = self.get_livecode_ws_url()
|
ws_url = self.get_livecode_ws_url()
|
||||||
value = livecode.livecode_to_svg(ws_url, self.code)
|
value = livecode.livecode_to_svg(ws_url, self.code)
|
||||||
|
if value:
|
||||||
cache.set(key, value)
|
cache.set(key, value)
|
||||||
return value
|
return value or DEFAULT_IMAGE
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_recent_sketches(limit=100, owner=None):
|
def get_recent_sketches(limit=100, owner=None):
|
||||||
|
|||||||
Reference in New Issue
Block a user