百度智能云

All Product Document

          Virtual Private Cloud

          Exception Handling

          Error Handling

          GO language identifies errors with the error type, and defines the following two error types:

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

          The user uses the SDK to call the relevant interface of each service. In addition to returning the required result, it also returns an error, and the user can obtain the detailed information of the relevant error for processing. Instance is as follows”

          // vpcClient is the Client object of the created VPC service 
          createVPCResult, err := vpcClient.CreateVPC("test-vpc") 
          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) 
          	 } 
          } 
          fmt.Println("create vpc success, vpcId:", createVPCResult.VPCID) 

          Client exception

          Client exception indicates an exception encountered when the client attempts to send a request to the Baidu Cloud service 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

          In case of exception of service end, Baidu Cloud service end returns corresponding error information to users 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
          Subnet