{"id":18688798,"url":"https://github.com/ouvens/tryjs","last_synced_at":"2025-10-11T07:40:51.853Z","repository":{"id":93556991,"uuid":"88115795","full_name":"ouvens/tryjs","owner":"ouvens","description":"JS try-catch error captrue","archived":false,"fork":false,"pushed_at":"2019-02-13T07:04:58.000Z","size":664,"stargazers_count":20,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-12T05:38:23.854Z","etag":null,"topics":["react-error-capture","try-catch-wrap"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ouvens.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-13T02:20:19.000Z","updated_at":"2023-01-27T09:46:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"dbfae0f6-08ac-4944-a534-326ebda8e030","html_url":"https://github.com/ouvens/tryjs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ouvens%2Ftryjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ouvens%2Ftryjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ouvens%2Ftryjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ouvens%2Ftryjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ouvens","download_url":"https://codeload.github.com/ouvens/tryjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248525168,"owners_count":21118616,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["react-error-capture","try-catch-wrap"],"created_at":"2024-11-07T10:38:25.861Z","updated_at":"2025-10-11T07:40:46.796Z","avatar_url":"https://github.com/ouvens.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n\u0026emsp;\u0026emsp;目前retcode上报使用的log上报使用的是window.onerror上报，对于跨域脚本出现80%以上的错误信息是script error，这些对于开发者来说是无效的，所以需要使用try...catch可以抓取绝大多数作用域下的js运行错误堆栈信息，有利于我们发现问题，提升业务质量。\n\n\u0026emsp;\u0026emsp;目标在于进一步获取onerror中的详细错误信息。以下是核心实现：\n\n```javascript\n// 包裹单个函数作用域，将原有函数封装，形成带有try...catch包裹的函数,ES5写法\nfunction _wrapFunction(fn) {\n    // ES6 的语法\n    return function() {\n\n        try {\n            return fn.apply(this, arguments); // 将函数参数用 try...catch 包裹 \n        } catch (e) {\n            _errorProcess(e);\n        }\n    }\n}\n```\n\n###### 使用方法一：\n\n\u0026emsp;\u0026emsp;在引入加载器后引入tryjs\n\n```html\n\u003cscript src=\"https://b.alicdn.com/@sys/loader/1.x/mloader.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://b.alicdn.com/@alife/tryjs/1.x/index.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"://domain.com/path/react.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"://domain.com/path/react-dom.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n\n\t// 添加常用模块化错误捕获和错误处理\n\tTryjs.init({\n\t\t// 定义需要使用try-catch包裹的全局函数，主要的入口函数内传入的函数内容将在try-catch的包裹下运行，不传则默认可以是['__def', 'require', 'define', 'setTimeout', 'setInterval']\n\t\tfnObject: ['__def', '__WPO','require', 'define', 'setTimeout', 'setInterval'] \n\t\terror: function(e) {\n\t\t\t// 错误处理函数，即如果发现错误的方法\n\t\t}\n\t});\n\n\t// 或者使用defineError包裹函数运行，并返回一个新的函数\n\n\tfunction fn() {\n\t\t// 函数中业务内容\n\t}\n\n\tTryjs.defineError(fn)();  // 将使fn函数内容被try-catch包裹运行，使用defineError(fn)();的效果与此相同\n\u003c/script\u003e\n```\n\n###### 使用方法二：\n\n\u0026emsp;\u0026emsp;作为模块引入，初始化默认的：\n\n```javascript\nvar tryjs = require('tryjs');\n// 或者 import tryjs from 'tryjs'；\n// 或者 define(['tryjs'], function(Tryjs){Tryjs.init()});\n\ntryjs.init({});\n\n```\n\n##### 使用方法三：\n\n\u0026emsp;\u0026emsp;对于React组件中函数进行包裹运行处理：\n\n```javascript\nclass MyComponent extends React.Component {\n    render() {\n        return \u003cdiv\u003erender something here\u003c/div\u003e;\n    }\n}\n\nexports default defineError(MyComponent);\n```\n\n\u0026emsp;\u0026emsp;或者使用decorator:\n\n```javascript\n\n@defineError\nclass MyComponent extends React.Component {\n    render() {\n        return \u003cdiv\u003erender something here\u003c/div\u003e;\n    }\n}\n\nexports default MyComponent;\n```\n\n##### 使用方法四：\n\n\u0026emsp;\u0026emsp;对于对象或单个函数中的内容包裹运行。\n\n```javascript\nfunction fn(){\n\t// 函数中业务内容\n}\n\nmodule.exports = defineError(fn);\n\n// 或者\n\nvar pageMod = {\n\tinit: function(){\n\t\t// 模块中初始化函数\n\t}\t\n}\n\nmodule.exports = defindError(pageMod);\n\n```\n\n\u003e 注意的是，Tryjs不能获取所有的脚本错误，但可以比较大的解决具体错误信息的内容\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fouvens%2Ftryjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fouvens%2Ftryjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fouvens%2Ftryjs/lists"}