123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package main
- import (
- "fmt"
- "net/http"
- "dy-pkg/errors"
- )
- const (
- ErrDatabase = iota + 100000
- ErrEncodingFailed
- )
- func init() {
- errors.RegisterCode(ErrDatabase, http.StatusInternalServerError, "数据错误", "")
- errors.RegisterCode(ErrEncodingFailed, http.StatusInternalServerError, "绑定错误", "")
- }
- func main() {
- if err := bindUser(); err != nil {
- // %s: Returns the user-safe error string mapped to the error code or the error message if none is specified.
- fmt.Println("====================> %s <====================")
- fmt.Printf("%s\n\n", err)
- // %v: Alias for %s.
- fmt.Println("====================> %v <====================")
- fmt.Printf("%v\n\n", err)
- //
- // %-v: Output caller details, useful for troubleshooting.
- fmt.Println("====================> %-v <====================")
- fmt.Printf("%-v\n\n", err)
- //
- // %+v: Output full error stack details, useful for debugging.
- fmt.Println("====================> %+v <====================")
- fmt.Printf("%+v\n\n", err)
- //
- // %#-v: Output caller details, useful for troubleshooting with JSON formatted output.
- fmt.Println("====================> %#-v <====================")
- fmt.Printf("%#-v\n\n", err)
- //
- // %#+v: Output full error stack details, useful for debugging with JSON formatted output.
- fmt.Println("====================> %#+v <====================")
- fmt.Printf("%#+v\n\n", err)
- //
- // do some business process based on the error type
- if errors.IsCode(err, ErrEncodingFailed) {
- fmt.Println("this is a ErrEncodingFailed error")
- }
- //
- if errors.IsCode(err, ErrDatabase) {
- fmt.Println("this is a ErrDatabase error")
- }
- // we can also find the cause error
- fmt.Println(errors.Cause(err))
- }
- }
- func bindUser() error {
- if err := getUser(); err != nil {
- // Step3: Wrap the error with a new error message and a new error code if needed.
- return errors.WrapC(err, ErrEncodingFailed, "encoding user 'Lingfei Kong' failed.")
- }
- return nil
- }
- func getUser() error {
- if err := queryDatabase(); err != nil {
- // Step2: Wrap the error with a new error message.
- return errors.Wrap(err, "get user failed.")
- }
- return nil
- }
- func queryDatabase() error {
- // Step1. Create error with specified error code.
- return errors.WithCode(ErrDatabase, "user 'Lingfei Kong' not found.")
- }
|