自定义错误处理程序

云眼About 1 min

自定义错误处理程序

本主题介绍如何为云眼灰度发布(特性标帜)AB实验 JavaScript(浏览器)SDK 创建自己的错误处理程序逻辑。

在生产环境中,需要完全控制和查看应用程序中发生的错误,包括源自 SDK 的错误。

云眼灰度发布(特性标帜)AB实验 SDK 在 SDK 中提供错误处理程序的默认实现。 下面是使用 SDK 中的默认错误处理程序的示例:

使用 NPM

const EyeofcloudSdk = require("@eyeofcloud/eyeofcloud-sdk"); // 3.0.0 SDK var defaultErrorHandler = require("@eyeofcloud/eyeofcloud-sdk/lib/plugins/error_handler"); // 3.0.1 SDK and above var defaultErrorHandler = require("@eyeofcloud/eyeofcloud-sdk").errorHandler; EyeofcloudSdk.setLogger(EyeofcloudSdk.logging.createLogger()) const eyeofcloudClient = EyeofcloudSdk.createInstance({ // No datafile will trigger the custom error handler, // but the default error handler is a no-op datafile: null, errorHandler: defaultErrorHandler });

但是,为了进一步控制和查看来自云眼灰度发布(特性标帜)AB实验 JavaScript(浏览器)SDK 的错误,我们建议实现您自己的自定义错误处理程序。

使用自定义错误处理程序,可以选择如何处理错误,无论是将错误记录到控制台还是将错误发送到另一个错误监视服务一样简单。

下面是使用自定义错误处理程序将错误记录到控制台的示例:

使用 NPM

const EyeofcloudSdk = require("@eyeofcloud/eyeofcloud-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('****'); } } EyeofcloudSdk.setLogger(EyeofcloudSdk.logging.createLogger()) const eyeofcloudClientInstance = EyeofcloudSdk.createInstance({ // No datafile will trigger the custom error handler, datafile: null, errorHandler: customErrorHandler, });

Last update:
Contributors: “zhangweixue”