使用TypeScript将JSON(从Sentry)转换为HTML [英] Convert JSON (from Sentry) to HTML with TypeScript

查看:151
本文介绍了使用TypeScript将JSON(从Sentry)转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习TypeScript.

I want to learn TypeScript.

我有一个哨兵方法event_from_exception()(Python)返回的JSON字典.

I have a JSON dictionary returned by the sentry method event_from_exception() (Python).

我想将其格式化为具有可扩展局部变量以及pre_和post_context的漂亮HTML.结果应大致如下:

I would like to format it as nice HTML with expandable local variables and pre_ and post_context. The result should look roughly like this:

这是一个示例json:

Here is an example json:

{
  "exception": {
    "values": [
      {
        "stacktrace": {
          "frames": [
            {
              "function": "main", 
              "abs_path": "/home/modlink_cok_d/src/sentry-json.py", 
              "pre_context": [
                "from sentry_sdk.utils import event_from_exception", 
                "", 
                "def main():", 
                "    local_var = 1", 
                "    try:"
              ], 
              "lineno": 9, 
              "vars": {
                "exc": "ValueError()", 
                "local_var": "1"
              }, 
              "context_line": "        raise ValueError()", 
              "post_context": [
                "    except Exception as exc:", 
                "        event, info = event_from_exception(sys.exc_info(), with_locals=True)", 
                "        print(json.dumps(event, indent=2))", 
                "", 
                "main()"
              ], 
              "module": "__main__", 
              "filename": "sentry-json.py"
            }
          ]
        }, 
        "type": "ValueError", 
        "value": "", 
        "module": "exceptions", 
        "mechanism": null
      }
    ]
  }, 
  "level": "error"
}

如何用TypeScript做到这一点?

How could this be done with TypeScript?

推荐答案

  1. 为数据创建架构.这将帮助您使用TypeScript和IDE.

您可以使用提供给您的 https://app.quicktype.io .

You can use https://app.quicktype.io which give you.

export interface Welcome {
    exception: Exception;
    level:     string;
}

export interface Exception {
    values: Value[];
}

export interface Value {
    stacktrace: Stacktrace;
    type:       string;
    value:      string;
    module:     string;
    mechanism:  null;
}

export interface Stacktrace {
    frames: Frame[];
}

export interface Frame {
    function:     string;
    abs_path:     string;
    pre_context:  string[];
    lineno:       number;
    vars:         Vars;
    context_line: string;
    post_context: string[];
    module:       string;
    filename:     string;
}

export interface Vars {
    exc:       string;
    local_var: string;
}

  1. 从您的data呈现HTML.
  1. Render HTML from your data.

您可以使用模板文字 如果您不喜欢使用Web框架(React,Vue).

You can use template literal if you do not have prefer web framework (React, Vue).

const data = JSON.parse(json);
const html = `
    <div>${data.exception.values.map(value => `
        <div>${value.stacktrace.frames.map(frame => `
            <div>
                <pre>${frame.abs_path} in ${frame.function}</pre>
                <div style="margin-left:2rem">
                    ${frame.pre_context.map((line, i) =>`
                        <pre>${frame.lineno + i - frame.pre_context.length}. ${line}</pre>
                    `).join("")}

                    <pre><strong>${frame.lineno}. ${frame.context_line}</strong></pre>
                    ${frame.post_context.map((line, i) => `
                        <pre>${frame.lineno + i + 1}. ${line}</pre>
                    `).join("")}
                </div>
            </div>
        `).join("")}</div>
    `).join("")}</div>
`;
document.body.innerHTML = html;

例如: https://codesandbox.io/s/52x8r17zo4

这篇关于使用TypeScript将JSON(从Sentry)转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆