问题描述
我正在使用代理包装导入的 SDK 类,以便我最终可以捕获 RequestExceptions,即当没有网络连接来显示错误弹出窗口时.
该应用程序在远程调试模式下正常工作,但是,当我禁用它时,会抛出错误 Can't find Variable: Proxy
.我是否需要以某种方式明确地导入它?或者是否有其他方法来包装一个类,以便我可以捕获它的所有异常?
下面是代理包装器的代码.
从'backend-sdk'导入后端;从 'backend-sdk/src/exceptions/RequestException' 导入 RequestException;让处理程序 = {获取:(目标,姓名,接收者)=> {尝试 {返回 Reflect.get(target, name,receiver);}赶上(e){if (e instanceof RequestException) {控制台错误(e);//TODO 为失败的API请求添加toast通知} 别的 {扔e;}}}};导出默认的新代理(新后端(),处理程序);默认情况下,代理在 react native 中不会被填充.它适用于 chrome 调试器,因为 React Native 在调试过程中使用 chrome js 引擎,请参阅 文档js环境.您可以尝试使用 代理 pollyfill.
I'm wrapping an imported SDK class with a Proxy so that I can eventually catch RequestExceptions, i.e. when there is no network connection to display error popups.
The app is working without issues in remote debugging mode, however, when I disable it the error Can't find Variable: Proxy
is thrown. Do I need to import this explicitly somehow? Or is there an alternative method to wrap a class so that I can catch all of its exceptions?
Below is the code for the Proxy wrapper.
import Backend from 'backend-sdk'; import RequestException from 'backend-sdk/src/exceptions/RequestException'; let handler = { get: (target, name, receiver) => { try { return Reflect.get(target, name, receiver); } catch (e) { if (e instanceof RequestException) { console.error(e); //TODO Add a toast notification for failed API requests } else { throw e; } } } }; export default new Proxy(new Backend(), handler);
Proxy is not pollyfilled in react native by default. It works in chrome debugger because react native uses chrome js engine during debugging see Document on js environment. You may try using Proxy pollyfill.
这篇关于不在远程调试模式下时,未在 React Native 中定义 ES6 代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!