From fe6f548dd2f04abaa5bd146e29800d3e5a1e12f9 Mon Sep 17 00:00:00 2001 From: ougongchang Date: Sat, 19 Sep 2020 18:23:35 +0800 Subject: [PATCH] escape some special string to avoid the frontend crash and change the default http code to 400 in MindInsightException --- mindinsight/datavisual/data_transform/graph/graph.py | 8 ++++++++ mindinsight/datavisual/data_transform/graph/msgraph.py | 7 +++++++ mindinsight/utils/exceptions.py | 4 ++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mindinsight/datavisual/data_transform/graph/graph.py b/mindinsight/datavisual/data_transform/graph/graph.py index d6781595..8248beba 100644 --- a/mindinsight/datavisual/data_transform/graph/graph.py +++ b/mindinsight/datavisual/data_transform/graph/graph.py @@ -28,6 +28,14 @@ from .node import NodeTypeEnum from .node import Node +def escape_html(string): + """Escape some html special string to avoid the frontend crash.""" + string = string.replace('<', '<') + string = string.replace('>', '>') + string = string.replace('"', '"') + return string + + class EdgeTypeEnum(Enum): """Node edge type enum.""" CONTROL = 'control' diff --git a/mindinsight/datavisual/data_transform/graph/msgraph.py b/mindinsight/datavisual/data_transform/graph/msgraph.py index 66441d6e..83516f9a 100644 --- a/mindinsight/datavisual/data_transform/graph/msgraph.py +++ b/mindinsight/datavisual/data_transform/graph/msgraph.py @@ -20,6 +20,7 @@ from .node import Node from .node import NodeTypeEnum from .graph import Graph from .graph import EdgeTypeEnum +from .graph import escape_html class MSGraph(Graph): @@ -63,6 +64,12 @@ class MSGraph(Graph): base_name=f'{node_proto.op_type}{node_proto.name}') else: node_name = node_proto.full_name + + # Because the Graphviz plug-in that the UI USES can't handle these special characters, + # the special characters are HTML escaped to avoid UI crash. + # Doing this on the backend prevents the frontend from doing it every time. + node_name = escape_html(node_name) + node = Node(name=node_name, node_id=node_proto.name) node.full_name = node_proto.full_name node.type = node_proto.op_type diff --git a/mindinsight/utils/exceptions.py b/mindinsight/utils/exceptions.py index 12f68625..43c80998 100644 --- a/mindinsight/utils/exceptions.py +++ b/mindinsight/utils/exceptions.py @@ -32,14 +32,14 @@ class MindInsightException(Exception): LEVEL = 0 SYSID = 42 - def __init__(self, error, message, http_code=500): + def __init__(self, error, message, http_code=400): """ Initialization of MindInsightException. Args: error (Enum): Error value for specified case. message (str): Description for exception. - http_code (int): Http code for exception. Default is 500. + http_code (int): Http code for exception. Default is 400. """ if isinstance(message, str): message = ' '.join(message.split())