{"id":21989048,"url":"https://github.com/imweb/tryjs","last_synced_at":"2025-10-24T22:06:45.325Z","repository":{"id":20863743,"uuid":"24150635","full_name":"imweb/tryjs","owner":"imweb","description":"Wrap async function and catch the error","archived":false,"fork":false,"pushed_at":"2014-12-17T16:21:37.000Z","size":396,"stargazers_count":16,"open_issues_count":1,"forks_count":3,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-30T12:15:26.163Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/imweb.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}},"created_at":"2014-09-17T15:31:01.000Z","updated_at":"2019-11-12T07:07:05.000Z","dependencies_parsed_at":"2022-07-07T22:51:09.157Z","dependency_job_id":null,"html_url":"https://github.com/imweb/tryjs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/imweb/tryjs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imweb%2Ftryjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imweb%2Ftryjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imweb%2Ftryjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imweb%2Ftryjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imweb","download_url":"https://codeload.github.com/imweb/tryjs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imweb%2Ftryjs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280872194,"owners_count":26405652,"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","status":"online","status_checked_at":"2025-10-24T02:00:06.418Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-29T19:27:31.255Z","updated_at":"2025-10-24T22:06:45.294Z","avatar_url":"https://github.com/imweb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"tryjs\n=====\n\n\u003e window.onerror在webkit中对于跨域的脚本错误无法捕获其stack，经常让我们无法定位上报的问题，tryjs利用try-catch将函数包裹起来，让错误捕获变得容易。\n\n原理\n----\n\n对于基于AMD和jQuery的网站，几乎所有业务函数都是通过回调异步触发的，所以我们只需要将所有异步函数包裹起来就可以捕获到大部分错误。\n\n例如，对于require函数，一般是这样使用的：\n\n```javascript\nrequire(['./main'],\n\t// 想办法把这个函数包裹起来 \n\tfunction (main) {\n\t\t// 实际上这里才是在调用\n\t\tmain.init();\n\t});\n```\n\n类似的对于setTimeout函数，一般可以这样：\n\n```javascript\nsetTimeout(\n\t// 想办法把这个函数包裹起来就行了\n\tfunction () {\n\t\tdosomthing();\n\t}, \n\t1000\n);\n```\n\n包裹了什么？\n---------\n\n* setTimeout\n* setInterval\n* jQuery event add\n* jQuery ajax\n* require \u0026 define\n\n为什么不对IE10以下版本进行包裹？\n---------------------------\n\n\u003e 我们建议，在IE10以下版本通过window.onerror来捕获错误，基于以下几点原因：\n\n* IE10以下版本中catch的error不包含error.stack，跟踪方式比起window.onerror没有优势\n* 单论性能，window.onerror比try catch要好\n* IE10以下版本中window.setTimeout为只读方法，无法篡改替换\n* IE6、IE7、IE8中window.setTimeout和window.setInterval为Object，非Function，没有继承apply和call方法","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimweb%2Ftryjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimweb%2Ftryjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimweb%2Ftryjs/lists"}