自定义错误处理程序
2023年5月11日大约 1 分钟
自定义错误处理程序
本主题介绍如何为功能实验 React Native SDK 创建自己的错误处理程序逻辑。
在生产环境中,你将希望完全控制和查看应用程序中发生的错误,包括源自云眼灰度标帜(Feature Flag)AB实验 SDK 的错误。
云眼灰度标帜(Feature Flag)AB实验 SDK 在 SDK 中提供错误处理程序的默认实现,该处理程序是无操作处理程序。下面是使用 SDK 中的默认错误处理程序的示例:
React
import {   errorHandler,   createInstance, } from '@eyeofcloud/react-sdk'  
const eyeofcloud = createInstance({   
    // No datafile will trigger the custom error handler,   
    // but the default error handler is a no-op   
    datafile: null,   
    errorHandler: errorHandler 
});但是,为了进一步控制和查看来自 Eyeofcloud 功能实验 SDK 的错误,我们建议实现自己的自定义错误处理程序。
使用自定义错误处理程序,可以选择如何处理错误,无论是将错误记录到控制台还是将错误发送到另一个错误监视服务一样简单。
下面是使用自定义错误处理程序将错误记录到控制台的示例:
React
import {   createInstance, } from '@eyeofcloud/react-sdk'  
/** 
* customErrorHandler  
*  
* Object that has a property `handleError` which will be called  
* when an error is thrown in the SDK.  
*/ 
const customErrorHandler = {   
    /**    
    * handleError    
    *    
    * Function which gets called when an error is thrown in the SDK    
    * @param {Object} error - error object    
    * @param {String} error.message - message of the error    
    * @param {String} error.stack - stack trace for the error    
    */   
    handleError: function(error) {     
        console.log('CUSTOM_ERROR_HANDLER');     
        console.log('****');     
        console.log(`Error Message: ${error.message}`);     
        console.log(`Error Stack: ${error.stack}`);     
        console.log('****');   
    } 
}   
const eyeofcloudClientInstance = EyeofcloudSdk.createInstance({   
    // No datafile will trigger the custom error handler,   
    datafile: null,   
    errorHandler: customErrorHandler, 
});🚧 重要
关闭
setLogger(null)日志记录 using 将禁止错误处理程序处理错误。