From f8b3c29ab8fa8fbbfc2231b9099f01b80f8757b6 Mon Sep 17 00:00:00 2001 From: shijian <2954700422@qq.com> Date: Thu, 11 Jan 2024 00:26:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=85=B3=E4=BA=8Epromise?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=E5=A0=86=E6=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- noname/init/index.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/noname/init/index.js b/noname/init/index.js index a3b9a5456..b0a625b66 100644 --- a/noname/init/index.js +++ b/noname/init/index.js @@ -752,10 +752,32 @@ function setServerIndex() { } function setWindowListener() { + /** + * @type { [Error, NodeJS.CallSite[]][] } + */ + const errorList = []; + // 这他娘的能捕获浏览器控制台里的输入值,尽量别用 + // 在火狐里无效 + Error.prepareStackTrace = function (e, stack) { + errorList.push([e, stack]); + }; // 但愿有用 - window.addEventListener("unhandledrejection", (error) => { - // 希望error.reason能是个正常的error - throw error.reason; + window.addEventListener("unhandledrejection", error => { + error.promise.catch(e => { + const result = errorList.find(v => v[0] === e); + if (result) { + // @ts-ignore + window.onerror( + result[0].message, + // @ts-ignore + result[1][0].getScriptNameOrSourceURL(), + // @ts-ignore + result[1][0].getLineNumber(), + // @ts-ignore + result[1][0].getColumnNumber(), + result[0]); + } + }); }); window.onkeydown = function (e) { @@ -857,6 +879,7 @@ function setWindowListener() { }; window.onerror = function (msg, src, line, column, err) { + errorList.length = 0; const winPath = window.__dirname ? ('file:///' + (__dirname.replace(new RegExp('\\\\', 'g'), '/') + '/')) : ''; let str = `错误文件: ${typeof src == 'string' ? decodeURI(src).replace(lib.assetURL, '').replace(winPath, '') : '未知文件'}`; str += `\n错误信息: ${msg}`;