From 887cf39321bc3d867df719a22a7750440aeb1487 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Wed, 8 Nov 2023 15:27:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/modules/userApi/renderer/preload.js | 48 ++++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/main/modules/userApi/renderer/preload.js b/src/main/modules/userApi/renderer/preload.js index 558096d4..a106025c 100644 --- a/src/main/modules/userApi/renderer/preload.js +++ b/src/main/modules/userApi/renderer/preload.js @@ -137,6 +137,13 @@ const handleShowUpdateAlert = (data, resolve, reject) => { resolve() } +const onError = (errorMessage) => { + if (isInitedApi) return + isInitedApi = true + if (errorMessage.length > 1024) errorMessage = errorMessage.substring(0, 1024) + '...' + sendMessage(USER_API_RENDERER_EVENT_NAME.init, null, false, errorMessage) +} + const initEnv = (userApi) => { contextBridge.exposeInMainWorld('lx', { EVENT_NAMES, @@ -158,22 +165,26 @@ const initEnv = (userApi) => { let request = needle.request(method, url, data, options, (err, resp, body) => { // console.log(err, resp, body) - if (err) { - callback(err, null, null) - } else { - body = resp.body = resp.raw.toString() - try { - resp.body = JSON.parse(resp.body) - } catch (_) {} - body = resp.body - callback(err, { - statusCode: resp.statusCode, - statusMessage: resp.statusMessage, - headers: resp.headers, - bytes: resp.bytes, - raw: resp.raw, - body, - }, body) + try { + if (err) { + callback.call(this, err, null, null) + } else { + body = resp.body = resp.raw.toString() + try { + resp.body = JSON.parse(resp.body) + } catch (_) {} + body = resp.body + callback.call(this, err, { + statusCode: resp.statusCode, + statusMessage: resp.statusMessage, + headers: resp.headers, + bytes: resp.bytes, + raw: resp.raw, + body, + }, body) + } + } catch (err) { + onError(err.message) } }).request @@ -290,10 +301,7 @@ const initEnv = (userApi) => { contextBridge.exposeInMainWorld('__lx_init_error_handler__', { sendError(errorMessage) { - if (isInitedApi) return - isInitedApi = true - if (errorMessage.length > 1024) errorMessage = errorMessage.substring(0, 1024) + '...' - sendMessage(USER_API_RENDERER_EVENT_NAME.init, null, false, errorMessage) + onError(errorMessage) }, })