百度智能云

All Product Document

          Elastic IP

          Exception Handling

          GO language marks error with error type, and BOS supports 2 errors in the table below:

          Error type Description
          BceClientError Error arising from user operation
          BceServiceError Error returned by BOS service

          You call BOS related interface with SDK, in addition to the results required, error is returned, and users can get relevant errors for handling. Instance is as follows”

          // EIP_CLIENT is the created EIP Client object 
          err := EIP_CLIENT.DeleteEip(EIP_ADDRESS, getClientToken()) 
          if err != nil { 
              switch realErr := err.(type) { 
              case *bce.BceClientError: 
                  fmt.Println("client occurs error:", realErr.Error()) 
              case *bce.BceServiceError: 
                  fmt.Println("service occurs error:", realErr.Error()) 
              default: 
                  fmt.Println("unknown error:", err) 
              } 
          } else { 
              fmt.Println("delete eip success") 
          } 

          Client Exception

          Client exception indicates an exception encountered when the client attempts to send a request to the BOS and transmits data. For example, when the network connection is unavailable at the time of sending request, BceClientError is returned; and in case of IO exception during file upload, BceClientError is also thrown.

          Server Exception

          When an exception occurs on the server, the Baidu Cloud server will return the corresponding error message to the user in order to locate the problem. For each server exception, please refer to the official website documents of each service.

          SDK Log

          GO SDK realizes the supporting of log module of 6 levels, 3 outputs (standard output, standard error and file) and basic format setting, with import path github.com/baidubce/bce-sdk-go/util/log. When the output is file, setting of 5 log rolling modes (not rolling, by day, by hour, by minute and by size) is supported, and in this case, directory where the log file is output also needs to be set.

          The log module has no external dependencies. Developers use GO SDK to develop projects and can directly reference the log module to use in the project. You can use the package-level log object used by the GO SDK, or you can create a new log object. See the following example for details:

          // Use global log object of package level directly (output together with GO SDK own log) 
          log.SetLogHandler(log.STDERR) 
          log.Debugf("%s", "logging message using the log package in the sdk") 
          
          // Create a log object (set output log based on customization, separated with GO SDK log output) 
          myLogger := log.NewLogger() 
          myLogger.SetLogHandler(log.FILE) 
          myLogger.SetLogDir("/home/log") 
          myLogger.SetRotateType(log.ROTATE_SIZE) 
          myLogger.Info("this is my own logger from the sdk") 
          Previous
          Initialization
          Next
          EIP