如何在JavaScript中格式化连接的异常字符串

时间:2017-05-31 10:31:46

标签: javascript html socket.io

我正在使用flasksocket.io来传输python个例外,并在html模式框中显示例外字符串。问题是模态框中显示的字符串没有格式,看起来像一个长串联字符串,所有异常放在一起,

Traceback (most recent call last): File "/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/connection.py", line 137, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/util/connection.py", line 91, in create_connection raise err File "/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/util/connection.py", line 81, in create_connection sock.connect(sa) File "/usr/local/lib/python3.5/dist-packages/gevent/_socket3.py", line 301, in connect raise error(err, strerror(err)) ConnectionRefusedError: [Errno 111] Connection refused 

我希望它像python console

一样显示
Traceback (most recent call last):
  File "/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/connection.py", line 137, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/util/connection.py", line 91, in create_connection
    raise err
  File "/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/util/connection.py", line 81, in create_connection
    sock.connect(sa)
  File "/usr/local/lib/python3.5/dist-packages/gevent/_socket3.py", line 301, in connect
    raise error(err, strerror(err))
ConnectionRefusedError: [Errno 111] Connection refused

我想知道如何在JavaScript中实现这一目标。

更新

这是使用js

的模态框的<pre>代码

&#13;
&#13;
function show_modal(row, column) {

  var modal_header = $(".modal-header");
  modal_header.text("header_footer");

  $(".modal-body p").append("<pre>" + "Traceback (most recent call last): File "/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/connection.py", line 137, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/util/connection.py", line 91, in create_connection raise err File "/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/util/connection.py", line 81, in create_connection sock.connect(sa) File "/usr/local/lib/python3.5/dist-packages/gevent/_socket3.py", line 301, in connect raise error(err, strerror(err)) ConnectionRefusedError: [Errno 111] Connection refused " + "</pre>");

  var modal_footer = $(".modal-footer");
  modal_footer.text("header_footer");

  modal.style.display = "block";
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您的测试代码会删除基本格式。

此处的工作代码:

&#13;
&#13;
var err = "Traceback (most recent call last):\n  File \"/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/connection.py\", line 137, in _new_conn (self.host, self.port), self.timeout, **extra_kw)\n  File \"/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/util/connection.py\", line 91, in create_connection raise err\n  File \"/home/user_name/.local/lib/python3.5/site-packages/requests/packages/urllib3/util/connection.py\", line 81, in create_connection sock.connect(sa)\n  File \"/usr/local/lib/python3.5/dist-packages/gevent/_socket3.py\", line 301, in connect raise error(err, strerror(err))\nConnectionRefusedError: [Errno 111] Connection refused ";

function show_modal() {

  var $pre = $("<pre>").html(err);
  $(".modal-body p").append($pre);

}

show_modal();
&#13;
pre {
    white-space: pre-wrap
}

.modal-body {
  width: 90%;
  margin: 1em auto;
  padding: 0 1em;
  border: 1px solid silver
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal-body">
  <p></p>
</div>
&#13;
&#13;
&#13;