百度智能云

All Product Document

          Relational Database Service

          Exceptions Handling

          GO language identifies errors by error types, and RDS supports two kinds of errors as shown in the following table:

          Error type Description
          BceClientError Error caused by user operation
          BceServiceError Error returned by RDS service

          Users call RDS related interfaces by using SDK, the interfaces will not only return the required results, but also return errors, users can obtain related errors and deal with them. The instance is as follows:

          // rdsClient is the object of RDS Client created
          result, err := client.ListRds()
          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)
          	}
          } 

          Exceptions at client end

          Exceptions at client end refer to the exceptions occurred when the client end is trying to send a request to RDS and during the process of data transmission. For example, if the network connection is unavailable when the client end is trying to send a request, BceClientError will be returned.

          Exceptions at server end

          When any exception occurs in RDS server, RDS server will return corresponding error message to users for problem locating.

          SDK Log

          RDS GO SDK supports log modules with six levels, three outputs (standard output, standard error and file) and basic format settings, and the load path isgithub.com/baidubce/bce-sdk-go/util/log. When a file is output, five kinds of log rotating modes (no rotating, by day, by hour, by minute and by size) can be set, and a directory of the output log file should also be set at the same time.

          Default Log

          RDS GO SDK uses a package-level global log object, which does not record logs by default. if it is required to output SDK-related logs, users need to specify the output mode and level by themselves, see details in the following instances:

          // import "github.com/baidubce/bce-sdk-go/util/log"
          
          // specify output to standard error, output INFO and above level
          log.SetLogHandler(log.STDERR)
          log.SetLogLevel(log.INFO)
          
          // specify output to standard error and file, DEBUG and above, rotate every 1GB
          log.SetLogHandler(log.STDERR | log.FILE)
          log.SetLogDir("/tmp/gosdk-log")
          log.SetRotateType(log.ROTATE_SIZE)
          log.SetRotateSize(1 << 30)
          
          // output to standard output, only output level and log message
          log.SetLogHandler(log.STDOUT)
          log.SetLogFormat([]string{log.FMT_LEVEL, log.FMT_MSG})

          Description:

          1. the default output level of log is ` DEBUG`
          2. if it is set as output to file, the default log output directory is ` /tmp `,  the default rotating mode is by hour
          3. if it is set as output to file and rotate by size, the default rotating size is 1GB4
          4. the default log output format is: `FMT_LEVEL, FMT_LTIME, FMT_LOCATION, FMT_MSG`

          Use in Projects

          The log module has no external dependence, users can use GO SDK to develop projects by directly referring to the log module, and users can continue to use package-level log objects used by GO SDK or create new log objects, see details in the following instance:

          // directly use the package-level global log object (which will be output together with GO SDK log)
          log.SetLogHandler(log.STDERR)
          log.Debugf("%s", "logging message using the log package in the RDS go sdk")
          
          // create a new log object (output the log according to the custom settings, separated from the 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 RDS go sdk")
          Previous
          White List Management
          Next
          Primary Interfaces