From d109706ad3ad068f7888c27fff7eb2f7dc0fd226 Mon Sep 17 00:00:00 2001 From: John Guo Date: Sat, 26 Jun 2021 18:34:26 +0800 Subject: [PATCH] unify error package to gerror --- container/garray/garray_normal_any.go | 10 +++---- container/garray/garray_normal_int.go | 10 +++---- container/garray/garray_normal_str.go | 11 ++++---- container/gpool/gpool.go | 6 ++--- crypto/gaes/gaes.go | 16 ++++++------ crypto/gdes/gdes.go | 26 +++++++++---------- database/gredis/gredis_conn.go | 6 ++--- encoding/gcharset/gcharset.go | 11 ++++---- encoding/gini/gini.go | 4 +-- encoding/gjson/gjson_api_new_load.go | 4 +-- errors/gerror/gerror.go | 2 +- i18n/gi18n/gi18n_manager.go | 4 +-- net/ghttp/ghttp_request_param_file.go | 5 ++-- net/ghttp/ghttp_server_admin_process.go | 10 +++---- net/ghttp/ghttp_server_graceful.go | 4 +-- net/ghttp/ghttp_server_router.go | 4 +-- net/ghttp/internal/client/client_request.go | 5 ++-- net/gipv4/gipv4_ip.go | 4 +-- net/gtcp/gtcp_server.go | 4 +-- net/gudp/gudp_server.go | 4 +-- os/gcfg/gcfg_config.go | 3 +-- os/gcfg/gcfg_config_api.go | 20 +++++++------- os/gcmd/gcmd_handler.go | 10 +++---- os/gcmd/gcmd_parser.go | 6 ++--- os/gcmd/gcmd_parser_handler.go | 12 ++++----- os/gcron/gcron_cron.go | 5 ++-- os/gcron/gcron_schedule.go | 15 +++++------ os/gfsnotify/gfsnotify.go | 5 ++-- os/gfsnotify/gfsnotify_watcher.go | 5 ++-- os/glog/glog_logger_config.go | 10 +++---- os/glog/glog_logger_level.go | 5 ++-- os/gproc/gproc_comm.go | 4 +-- os/gproc/gproc_comm_send.go | 4 +-- os/gproc/gproc_process.go | 4 +-- os/grpool/grpool.go | 7 +++-- os/gsession/gsession_session.go | 6 ++--- os/gspath/gspath.go | 11 ++++---- os/gview/gview_config.go | 13 +++++----- os/gview/gview_parse.go | 3 +-- ...nv_z_unit_struct_marshal_unmarshal_test.go | 8 +++--- util/gvalid/gvalid_validator_check_value.go | 14 +++++----- 41 files changed, 151 insertions(+), 169 deletions(-) diff --git a/container/garray/garray_normal_any.go b/container/garray/garray_normal_any.go index 985c02fc7ff..a3b450b541f 100644 --- a/container/garray/garray_normal_any.go +++ b/container/garray/garray_normal_any.go @@ -8,8 +8,8 @@ package garray import ( "bytes" - "errors" "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/empty" "github.com/gogf/gf/internal/json" "github.com/gogf/gf/text/gstr" @@ -123,7 +123,7 @@ func (a *Array) Set(index int, value interface{}) error { a.mu.Lock() defer a.mu.Unlock() if index < 0 || index >= len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", index, len(a.array))) + return gerror.Newf("index %d out of array range %d", index, len(a.array)) } a.array[index] = value return nil @@ -176,7 +176,7 @@ func (a *Array) InsertBefore(index int, value interface{}) error { a.mu.Lock() defer a.mu.Unlock() if index < 0 || index >= len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", index, len(a.array))) + return gerror.Newf("index %d out of array range %d", index, len(a.array)) } rear := append([]interface{}{}, a.array[index:]...) a.array = append(a.array[0:index], value) @@ -189,7 +189,7 @@ func (a *Array) InsertAfter(index int, value interface{}) error { a.mu.Lock() defer a.mu.Unlock() if index < 0 || index >= len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", index, len(a.array))) + return gerror.Newf("index %d out of array range %d", index, len(a.array)) } rear := append([]interface{}{}, a.array[index+1:]...) a.array = append(a.array[0:index+1], value) @@ -545,7 +545,7 @@ func (a *Array) Fill(startIndex int, num int, value interface{}) error { a.mu.Lock() defer a.mu.Unlock() if startIndex < 0 || startIndex > len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", startIndex, len(a.array))) + return gerror.Newf("index %d out of array range %d", startIndex, len(a.array)) } for i := startIndex; i < startIndex+num; i++ { if i > len(a.array)-1 { diff --git a/container/garray/garray_normal_int.go b/container/garray/garray_normal_int.go index 05a10f0feaf..3e43a56dbc8 100644 --- a/container/garray/garray_normal_int.go +++ b/container/garray/garray_normal_int.go @@ -8,8 +8,8 @@ package garray import ( "bytes" - "errors" "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/json" "math" "sort" @@ -104,7 +104,7 @@ func (a *IntArray) Set(index int, value int) error { a.mu.Lock() defer a.mu.Unlock() if index < 0 || index >= len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", index, len(a.array))) + return gerror.Newf("index %d out of array range %d", index, len(a.array)) } a.array[index] = value return nil @@ -175,7 +175,7 @@ func (a *IntArray) InsertBefore(index int, value int) error { a.mu.Lock() defer a.mu.Unlock() if index < 0 || index >= len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", index, len(a.array))) + return gerror.Newf("index %d out of array range %d", index, len(a.array)) } rear := append([]int{}, a.array[index:]...) a.array = append(a.array[0:index], value) @@ -188,7 +188,7 @@ func (a *IntArray) InsertAfter(index int, value int) error { a.mu.Lock() defer a.mu.Unlock() if index < 0 || index >= len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", index, len(a.array))) + return gerror.Newf("index %d out of array range %d", index, len(a.array)) } rear := append([]int{}, a.array[index+1:]...) a.array = append(a.array[0:index+1], value) @@ -559,7 +559,7 @@ func (a *IntArray) Fill(startIndex int, num int, value int) error { a.mu.Lock() defer a.mu.Unlock() if startIndex < 0 || startIndex > len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", startIndex, len(a.array))) + return gerror.Newf("index %d out of array range %d", startIndex, len(a.array)) } for i := startIndex; i < startIndex+num; i++ { if i > len(a.array)-1 { diff --git a/container/garray/garray_normal_str.go b/container/garray/garray_normal_str.go index f1754e80d3f..832a70cb6cb 100644 --- a/container/garray/garray_normal_str.go +++ b/container/garray/garray_normal_str.go @@ -8,8 +8,7 @@ package garray import ( "bytes" - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/json" "github.com/gogf/gf/text/gstr" "math" @@ -91,7 +90,7 @@ func (a *StrArray) Set(index int, value string) error { a.mu.Lock() defer a.mu.Unlock() if index < 0 || index >= len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", index, len(a.array))) + return gerror.Newf("index %d out of array range %d", index, len(a.array)) } a.array[index] = value return nil @@ -163,7 +162,7 @@ func (a *StrArray) InsertBefore(index int, value string) error { a.mu.Lock() defer a.mu.Unlock() if index < 0 || index >= len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", index, len(a.array))) + return gerror.Newf("index %d out of array range %d", index, len(a.array)) } rear := append([]string{}, a.array[index:]...) a.array = append(a.array[0:index], value) @@ -176,7 +175,7 @@ func (a *StrArray) InsertAfter(index int, value string) error { a.mu.Lock() defer a.mu.Unlock() if index < 0 || index >= len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", index, len(a.array))) + return gerror.Newf("index %d out of array range %d", index, len(a.array)) } rear := append([]string{}, a.array[index+1:]...) a.array = append(a.array[0:index+1], value) @@ -563,7 +562,7 @@ func (a *StrArray) Fill(startIndex int, num int, value string) error { a.mu.Lock() defer a.mu.Unlock() if startIndex < 0 || startIndex > len(a.array) { - return errors.New(fmt.Sprintf("index %d out of array range %d", startIndex, len(a.array))) + return gerror.Newf("index %d out of array range %d", startIndex, len(a.array)) } for i := startIndex; i < startIndex+num; i++ { if i > len(a.array)-1 { diff --git a/container/gpool/gpool.go b/container/gpool/gpool.go index 0ac91913917..e4125aaacaa 100644 --- a/container/gpool/gpool.go +++ b/container/gpool/gpool.go @@ -8,7 +8,7 @@ package gpool import ( - "errors" + "github.com/gogf/gf/errors/gerror" "time" "github.com/gogf/gf/container/glist" @@ -66,7 +66,7 @@ func New(ttl time.Duration, newFunc NewFunc, expireFunc ...ExpireFunc) *Pool { // Put puts an item to pool. func (p *Pool) Put(value interface{}) error { if p.closed.Val() { - return errors.New("pool is closed") + return gerror.New("pool is closed") } item := &poolItem{ value: value, @@ -117,7 +117,7 @@ func (p *Pool) Get() (interface{}, error) { if p.NewFunc != nil { return p.NewFunc() } - return nil, errors.New("pool is empty") + return nil, gerror.New("pool is empty") } // Size returns the count of available items of pool. diff --git a/crypto/gaes/gaes.go b/crypto/gaes/gaes.go index 6e2f84d5ae3..bdb04d78a00 100644 --- a/crypto/gaes/gaes.go +++ b/crypto/gaes/gaes.go @@ -11,7 +11,7 @@ import ( "bytes" "crypto/aes" "crypto/cipher" - "errors" + "github.com/gogf/gf/errors/gerror" ) var ( @@ -63,7 +63,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error) { } blockSize := block.BlockSize() if len(cipherText) < blockSize { - return nil, errors.New("cipherText too short") + return nil, gerror.New("cipherText too short") } ivValue := ([]byte)(nil) if len(iv) > 0 { @@ -72,7 +72,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error) { ivValue = []byte(IVDefaultValue) } if len(cipherText)%blockSize != 0 { - return nil, errors.New("cipherText is not a multiple of the block size") + return nil, gerror.New("cipherText is not a multiple of the block size") } blockModel := cipher.NewCBCDecrypter(block, ivValue) plainText := make([]byte, len(cipherText)) @@ -93,22 +93,22 @@ func PKCS5Padding(src []byte, blockSize int) []byte { func PKCS5UnPadding(src []byte, blockSize int) ([]byte, error) { length := len(src) if blockSize <= 0 { - return nil, errors.New("invalid blocklen") + return nil, gerror.New("invalid blocklen") } if length%blockSize != 0 || length == 0 { - return nil, errors.New("invalid data len") + return nil, gerror.New("invalid data len") } unpadding := int(src[length-1]) if unpadding > blockSize || unpadding == 0 { - return nil, errors.New("invalid padding") + return nil, gerror.New("invalid padding") } padding := src[length-unpadding:] for i := 0; i < unpadding; i++ { if padding[i] != byte(unpadding) { - return nil, errors.New("invalid padding") + return nil, gerror.New("invalid padding") } } @@ -146,7 +146,7 @@ func DecryptCFB(cipherText []byte, key []byte, unPadding int, iv ...[]byte) ([]b return nil, err } if len(cipherText) < aes.BlockSize { - return nil, errors.New("cipherText too short") + return nil, gerror.New("cipherText too short") } ivValue := ([]byte)(nil) if len(iv) > 0 { diff --git a/crypto/gdes/gdes.go b/crypto/gdes/gdes.go index 55d936dfe21..c60f7c190bf 100644 --- a/crypto/gdes/gdes.go +++ b/crypto/gdes/gdes.go @@ -11,7 +11,7 @@ import ( "bytes" "crypto/cipher" "crypto/des" - "errors" + "github.com/gogf/gf/errors/gerror" ) const ( @@ -66,7 +66,7 @@ func DecryptECB(cipherText []byte, key []byte, padding int) ([]byte, error) { // The length of the should be either 16 or 24 bytes. func EncryptECBTriple(plainText []byte, key []byte, padding int) ([]byte, error) { if len(key) != 16 && len(key) != 24 { - return nil, errors.New("key length error") + return nil, gerror.New("key length error") } text, err := Padding(plainText, padding) @@ -100,7 +100,7 @@ func EncryptECBTriple(plainText []byte, key []byte, padding int) ([]byte, error) // The length of the should be either 16 or 24 bytes. func DecryptECBTriple(cipherText []byte, key []byte, padding int) ([]byte, error) { if len(key) != 16 && len(key) != 24 { - return nil, errors.New("key length error") + return nil, gerror.New("key length error") } var newKey []byte @@ -138,7 +138,7 @@ func EncryptCBC(plainText []byte, key []byte, iv []byte, padding int) ([]byte, e } if len(iv) != block.BlockSize() { - return nil, errors.New("iv length invalid") + return nil, gerror.New("iv length invalid") } text, err := Padding(plainText, padding) @@ -161,7 +161,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv []byte, padding int) ([]byte, } if len(iv) != block.BlockSize() { - return nil, errors.New("iv length invalid") + return nil, gerror.New("iv length invalid") } text := make([]byte, len(cipherText)) @@ -179,7 +179,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv []byte, padding int) ([]byte, // EncryptCBCTriple encrypts using TripleDES and CBC mode. func EncryptCBCTriple(plainText []byte, key []byte, iv []byte, padding int) ([]byte, error) { if len(key) != 16 && len(key) != 24 { - return nil, errors.New("key length invalid") + return nil, gerror.New("key length invalid") } var newKey []byte @@ -196,7 +196,7 @@ func EncryptCBCTriple(plainText []byte, key []byte, iv []byte, padding int) ([]b } if len(iv) != block.BlockSize() { - return nil, errors.New("iv length invalid") + return nil, gerror.New("iv length invalid") } text, err := Padding(plainText, padding) @@ -214,7 +214,7 @@ func EncryptCBCTriple(plainText []byte, key []byte, iv []byte, padding int) ([]b // DecryptCBCTriple decrypts <cipherText> using TripleDES and CBC mode. func DecryptCBCTriple(cipherText []byte, key []byte, iv []byte, padding int) ([]byte, error) { if len(key) != 16 && len(key) != 24 { - return nil, errors.New("key length invalid") + return nil, gerror.New("key length invalid") } var newKey []byte @@ -231,7 +231,7 @@ func DecryptCBCTriple(cipherText []byte, key []byte, iv []byte, padding int) ([] } if len(iv) != block.BlockSize() { - return nil, errors.New("iv length invalid") + return nil, gerror.New("iv length invalid") } text := make([]byte, len(cipherText)) @@ -262,12 +262,12 @@ func Padding(text []byte, padding int) ([]byte, error) { switch padding { case NOPADDING: if len(text)%8 != 0 { - return nil, errors.New("text length invalid") + return nil, gerror.New("text length invalid") } case PKCS5PADDING: return PaddingPKCS5(text, 8), nil default: - return nil, errors.New("padding type error") + return nil, gerror.New("padding type error") } return text, nil @@ -277,12 +277,12 @@ func UnPadding(text []byte, padding int) ([]byte, error) { switch padding { case NOPADDING: if len(text)%8 != 0 { - return nil, errors.New("text length invalid") + return nil, gerror.New("text length invalid") } case PKCS5PADDING: return UnPaddingPKCS5(text), nil default: - return nil, errors.New("padding type error") + return nil, gerror.New("padding type error") } return text, nil } diff --git a/database/gredis/gredis_conn.go b/database/gredis/gredis_conn.go index f33d4528e1d..2826affcd9c 100644 --- a/database/gredis/gredis_conn.go +++ b/database/gredis/gredis_conn.go @@ -8,8 +8,8 @@ package gredis import ( "context" - "errors" "github.com/gogf/gf/container/gvar" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/json" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/util/gconv" @@ -50,7 +50,7 @@ func (c *Conn) do(timeout time.Duration, commandName string, args ...interface{} if timeout > 0 { conn, ok := c.Conn.(redis.ConnWithTimeout) if !ok { - return gvar.New(nil), errors.New(`current connection does not support "ConnWithTimeout"`) + return gvar.New(nil), gerror.New(`current connection does not support "ConnWithTimeout"`) } return conn.DoWithTimeout(timeout, commandName, args...) } @@ -107,7 +107,7 @@ func (c *Conn) ReceiveVar() (*gvar.Var, error) { func (c *Conn) ReceiveVarWithTimeout(timeout time.Duration) (*gvar.Var, error) { conn, ok := c.Conn.(redis.ConnWithTimeout) if !ok { - return gvar.New(nil), errors.New(`current connection does not support "ConnWithTimeout"`) + return gvar.New(nil), gerror.New(`current connection does not support "ConnWithTimeout"`) } return resultToVar(conn.ReceiveWithTimeout(timeout)) } diff --git a/encoding/gcharset/gcharset.go b/encoding/gcharset/gcharset.go index c976fa6060d..0aac86b2790 100644 --- a/encoding/gcharset/gcharset.go +++ b/encoding/gcharset/gcharset.go @@ -21,8 +21,7 @@ package gcharset import ( "bytes" - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "io/ioutil" "golang.org/x/text/encoding" @@ -60,11 +59,11 @@ func Convert(dstCharset string, srcCharset string, src string) (dst string, err transform.NewReader(bytes.NewReader([]byte(src)), e.NewDecoder()), ) if err != nil { - return "", fmt.Errorf("%s to utf8 failed. %v", srcCharset, err) + return "", gerror.Newf("%s to utf8 failed. %v", srcCharset, err) } src = string(tmp) } else { - return dst, errors.New(fmt.Sprintf("unsupport srcCharset: %s", srcCharset)) + return dst, gerror.Newf("unsupport srcCharset: %s", srcCharset) } } // Do the converting from UTF-8 to <dstCharset>. @@ -74,11 +73,11 @@ func Convert(dstCharset string, srcCharset string, src string) (dst string, err transform.NewReader(bytes.NewReader([]byte(src)), e.NewEncoder()), ) if err != nil { - return "", fmt.Errorf("utf to %s failed. %v", dstCharset, err) + return "", gerror.Newf("utf to %s failed. %v", dstCharset, err) } dst = string(tmp) } else { - return dst, errors.New(fmt.Sprintf("unsupport dstCharset: %s", dstCharset)) + return dst, gerror.Newf("unsupport dstCharset: %s", dstCharset) } } else { dst = src diff --git a/encoding/gini/gini.go b/encoding/gini/gini.go index 8ca3770bb4b..6394d239314 100644 --- a/encoding/gini/gini.go +++ b/encoding/gini/gini.go @@ -10,8 +10,8 @@ package gini import ( "bufio" "bytes" - "errors" "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/json" "io" "strings" @@ -70,7 +70,7 @@ func Decode(data []byte) (res map[string]interface{}, err error) { } if haveSection == false { - return nil, errors.New("failed to parse INI file, section not found") + return nil, gerror.New("failed to parse INI file, section not found") } return res, nil } diff --git a/encoding/gjson/gjson_api_new_load.go b/encoding/gjson/gjson_api_new_load.go index bd6dcb5041d..780b3a16ff0 100644 --- a/encoding/gjson/gjson_api_new_load.go +++ b/encoding/gjson/gjson_api_new_load.go @@ -8,8 +8,8 @@ package gjson import ( "bytes" - "errors" "fmt" + "github.com/gogf/gf/errors/gerror" "reflect" "github.com/gogf/gf/internal/json" @@ -264,7 +264,7 @@ func doLoadContentWithOptions(dataType string, data []byte, options Options) (*J return nil, err } default: - err = errors.New("unsupported type for loading") + err = gerror.New("unsupported type for loading") } if err != nil { return nil, err diff --git a/errors/gerror/gerror.go b/errors/gerror/gerror.go index 99b8b45fd85..8b029b23f87 100644 --- a/errors/gerror/gerror.go +++ b/errors/gerror/gerror.go @@ -4,7 +4,7 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -// Package errors provides simple functions to manipulate errors. +// Package gerror provides simple functions to manipulate errors. // // Very note that, this package is quite a base package, which should not import extra // packages except standard packages, to avoid cycle imports. diff --git a/i18n/gi18n/gi18n_manager.go b/i18n/gi18n/gi18n_manager.go index d289e205309..cc223e32d80 100644 --- a/i18n/gi18n/gi18n_manager.go +++ b/i18n/gi18n/gi18n_manager.go @@ -8,8 +8,8 @@ package gi18n import ( "context" - "errors" "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "strings" "sync" @@ -101,7 +101,7 @@ func (m *Manager) SetPath(path string) error { } else { realPath, _ := gfile.Search(path) if realPath == "" { - return errors.New(fmt.Sprintf(`%s does not exist`, path)) + return gerror.Newf(`%s does not exist`, path) } m.options.Path = realPath } diff --git a/net/ghttp/ghttp_request_param_file.go b/net/ghttp/ghttp_request_param_file.go index 47811dfde9d..997b82ed4b8 100644 --- a/net/ghttp/ghttp_request_param_file.go +++ b/net/ghttp/ghttp_request_param_file.go @@ -8,7 +8,6 @@ package ghttp import ( "context" - "errors" "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/os/gfile" @@ -36,7 +35,7 @@ type UploadFiles []*UploadFile // Note that it will OVERWRITE the target file if there's already a same name file exist. func (f *UploadFile) Save(dirPath string, randomlyRename ...bool) (filename string, err error) { if f == nil { - return "", errors.New("file is empty, maybe you retrieve it from invalid field name or form enctype") + return "", gerror.New("file is empty, maybe you retrieve it from invalid field name or form enctype") } if !gfile.Exists(dirPath) { if err = gfile.Mkdir(dirPath); err != nil { @@ -77,7 +76,7 @@ func (f *UploadFile) Save(dirPath string, randomlyRename ...bool) (filename stri // The parameter <randomlyRename> specifies whether randomly renames all the file names. func (fs UploadFiles) Save(dirPath string, randomlyRename ...bool) (filenames []string, err error) { if len(fs) == 0 { - return nil, errors.New("file array is empty, maybe you retrieve it from invalid field name or form enctype") + return nil, gerror.New("file array is empty, maybe you retrieve it from invalid field name or form enctype") } for _, f := range fs { if filename, err := f.Save(dirPath, randomlyRename...); err != nil { diff --git a/net/ghttp/ghttp_server_admin_process.go b/net/ghttp/ghttp_server_admin_process.go index 6cb27d05f3b..e6fa2e622e4 100644 --- a/net/ghttp/ghttp_server_admin_process.go +++ b/net/ghttp/ghttp_server_admin_process.go @@ -9,8 +9,8 @@ package ghttp import ( "bytes" "context" - "errors" "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/text/gstr" "os" @@ -52,7 +52,7 @@ var serverProcessStatus = gtype.NewInt() // The optional parameter <newExeFilePath> specifies the new binary file for creating process. func RestartAllServer(newExeFilePath ...string) error { if !gracefulEnabled { - return errors.New("graceful reload feature is disabled") + return gerror.New("graceful reload feature is disabled") } serverActionLocker.Lock() defer serverActionLocker.Unlock() @@ -85,9 +85,9 @@ func checkProcessStatus() error { if status > 0 { switch status { case adminActionRestarting: - return errors.New("server is restarting") + return gerror.New("server is restarting") case adminActionShuttingDown: - return errors.New("server is shutting down") + return gerror.New("server is shutting down") } } return nil @@ -98,7 +98,7 @@ func checkProcessStatus() error { func checkActionFrequency() error { interval := gtime.TimestampMilli() - serverActionLastTime.Val() if interval < adminActionIntervalLimit { - return errors.New(fmt.Sprintf("too frequent action, please retry in %d ms", adminActionIntervalLimit-interval)) + return gerror.Newf("too frequent action, please retry in %d ms", adminActionIntervalLimit-interval) } serverActionLastTime.Set(gtime.TimestampMilli()) return nil diff --git a/net/ghttp/ghttp_server_graceful.go b/net/ghttp/ghttp_server_graceful.go index e4ca2553cb9..2e6f82390f0 100644 --- a/net/ghttp/ghttp_server_graceful.go +++ b/net/ghttp/ghttp_server_graceful.go @@ -9,8 +9,8 @@ package ghttp import ( "context" "crypto/tls" - "errors" "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/os/gproc" "github.com/gogf/gf/os/gres" "github.com/gogf/gf/text/gstr" @@ -122,7 +122,7 @@ func (s *gracefulServer) ListenAndServeTLS(certFile, keyFile string, tlsConfig . } if err != nil { - return errors.New(fmt.Sprintf(`open cert file "%s","%s" failed: %s`, certFile, keyFile, err.Error())) + return gerror.Newf(`open cert file "%s","%s" failed: %s`, certFile, keyFile, err.Error()) } ln, err := s.getNetListener() if err != nil { diff --git a/net/ghttp/ghttp_server_router.go b/net/ghttp/ghttp_server_router.go index cc7d883b36d..6d31d5e788a 100644 --- a/net/ghttp/ghttp_server_router.go +++ b/net/ghttp/ghttp_server_router.go @@ -7,9 +7,9 @@ package ghttp import ( - "errors" "fmt" "github.com/gogf/gf/container/gtype" + "github.com/gogf/gf/errors/gerror" "strings" "github.com/gogf/gf/debug/gdebug" @@ -53,7 +53,7 @@ func (s *Server) parsePattern(pattern string) (domain, method, path string, err } } if path == "" { - err = errors.New("invalid pattern: URI should not be empty") + err = gerror.New("invalid pattern: URI should not be empty") } if path != "/" { path = strings.TrimRight(path, "/") diff --git a/net/ghttp/internal/client/client_request.go b/net/ghttp/internal/client/client_request.go index 2da8b6043dd..90b8970b953 100644 --- a/net/ghttp/internal/client/client_request.go +++ b/net/ghttp/internal/client/client_request.go @@ -9,8 +9,7 @@ package client import ( "bytes" "context" - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/internal/json" "github.com/gogf/gf/internal/utils" @@ -189,7 +188,7 @@ func (c *Client) prepareRequest(method, url string, data ...interface{}) (req *h if len(array[1]) > 6 && strings.Compare(array[1][0:6], "@file:") == 0 { path := array[1][6:] if !gfile.Exists(path) { - return nil, errors.New(fmt.Sprintf(`"%s" does not exist`, path)) + return nil, gerror.Newf(`"%s" does not exist`, path) } if file, err := writer.CreateFormFile(array[0], gfile.Basename(path)); err == nil { if f, err := os.Open(path); err == nil { diff --git a/net/gipv4/gipv4_ip.go b/net/gipv4/gipv4_ip.go index 46d2aab8e8f..29c113d2398 100644 --- a/net/gipv4/gipv4_ip.go +++ b/net/gipv4/gipv4_ip.go @@ -8,7 +8,7 @@ package gipv4 import ( - "errors" + "github.com/gogf/gf/errors/gerror" "net" "strconv" "strings" @@ -38,7 +38,7 @@ func GetIntranetIp() (ip string, err error) { return "", err } if len(ips) == 0 { - return "", errors.New("no intranet ip found") + return "", gerror.New("no intranet ip found") } return ips[0], nil } diff --git a/net/gtcp/gtcp_server.go b/net/gtcp/gtcp_server.go index f4ec7363c67..d8c4f22fdff 100644 --- a/net/gtcp/gtcp_server.go +++ b/net/gtcp/gtcp_server.go @@ -8,7 +8,7 @@ package gtcp import ( "crypto/tls" - "errors" + "github.com/gogf/gf/errors/gerror" "net" "sync" @@ -116,7 +116,7 @@ func (s *Server) Close() error { // Run starts running the TCP Server. func (s *Server) Run() (err error) { if s.handler == nil { - err = errors.New("start running failed: socket handler not defined") + err = gerror.New("start running failed: socket handler not defined") glog.Error(err) return } diff --git a/net/gudp/gudp_server.go b/net/gudp/gudp_server.go index db7dca6a476..4987787c9a3 100644 --- a/net/gudp/gudp_server.go +++ b/net/gudp/gudp_server.go @@ -7,7 +7,7 @@ package gudp import ( - "errors" + "github.com/gogf/gf/errors/gerror" "net" "github.com/gogf/gf/container/gmap" @@ -78,7 +78,7 @@ func (s *Server) Close() error { // Run starts listening UDP connection. func (s *Server) Run() error { if s.handler == nil { - err := errors.New("start running failed: socket handler not defined") + err := gerror.New("start running failed: socket handler not defined") glog.Error(err) return err } diff --git a/os/gcfg/gcfg_config.go b/os/gcfg/gcfg_config.go index d21f9197885..425a09df5c0 100644 --- a/os/gcfg/gcfg_config.go +++ b/os/gcfg/gcfg_config.go @@ -9,7 +9,6 @@ package gcfg import ( "bytes" "context" - "errors" "fmt" "github.com/gogf/gf/container/garray" "github.com/gogf/gf/container/gmap" @@ -143,7 +142,7 @@ func (c *Config) SetPath(path string) error { } else { buffer.WriteString(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" does not exist`, path)) } - err := errors.New(buffer.String()) + err := gerror.New(buffer.String()) if errorPrint() { glog.Error(err) } diff --git a/os/gcfg/gcfg_config_api.go b/os/gcfg/gcfg_config_api.go index aee7804a364..7e1f0625e98 100644 --- a/os/gcfg/gcfg_config_api.go +++ b/os/gcfg/gcfg_config_api.go @@ -7,7 +7,7 @@ package gcfg import ( - "errors" + "github.com/gogf/gf/errors/gerror" "time" "github.com/gogf/gf/encoding/gjson" @@ -295,7 +295,7 @@ func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[s if j := c.getJson(); j != nil { return j.GetStruct(pattern, pointer, mapping...) } - return errors.New("configuration not found") + return gerror.New("configuration not found") } // GetStructs converts any slice to given struct slice. @@ -303,7 +303,7 @@ func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[ if j := c.getJson(); j != nil { return j.GetStructs(pattern, pointer, mapping...) } - return errors.New("configuration not found") + return gerror.New("configuration not found") } // GetMapToMap retrieves the value by specified `pattern` and converts it to specified map variable. @@ -312,7 +312,7 @@ func (c *Config) GetMapToMap(pattern string, pointer interface{}, mapping ...map if j := c.getJson(); j != nil { return j.GetMapToMap(pattern, pointer, mapping...) } - return errors.New("configuration not found") + return gerror.New("configuration not found") } // GetMapToMaps retrieves the value by specified `pattern` and converts it to specified map slice @@ -322,7 +322,7 @@ func (c *Config) GetMapToMaps(pattern string, pointer interface{}, mapping ...ma if j := c.getJson(); j != nil { return j.GetMapToMaps(pattern, pointer, mapping...) } - return errors.New("configuration not found") + return gerror.New("configuration not found") } // GetMapToMapsDeep retrieves the value by specified `pattern` and converts it to specified map slice @@ -332,7 +332,7 @@ func (c *Config) GetMapToMapsDeep(pattern string, pointer interface{}, mapping . if j := c.getJson(); j != nil { return j.GetMapToMapsDeep(pattern, pointer, mapping...) } - return errors.New("configuration not found") + return gerror.New("configuration not found") } // Map converts current Json object to map[string]interface{}. It returns nil if fails. @@ -358,7 +358,7 @@ func (c *Config) Struct(pointer interface{}, mapping ...map[string]string) error if j := c.getJson(); j != nil { return j.Struct(pointer, mapping...) } - return errors.New("configuration not found") + return gerror.New("configuration not found") } // Structs converts current Json object to specified object slice. @@ -367,7 +367,7 @@ func (c *Config) Structs(pointer interface{}, mapping ...map[string]string) erro if j := c.getJson(); j != nil { return j.Structs(pointer, mapping...) } - return errors.New("configuration not found") + return gerror.New("configuration not found") } // MapToMap converts current Json object to specified map variable. @@ -376,7 +376,7 @@ func (c *Config) MapToMap(pointer interface{}, mapping ...map[string]string) err if j := c.getJson(); j != nil { return j.MapToMap(pointer, mapping...) } - return errors.New("configuration not found") + return gerror.New("configuration not found") } // MapToMaps converts current Json object to specified map variable slice. @@ -385,7 +385,7 @@ func (c *Config) MapToMaps(pointer interface{}, mapping ...map[string]string) er if j := c.getJson(); j != nil { return j.MapToMaps(pointer, mapping...) } - return errors.New("configuration not found") + return gerror.New("configuration not found") } // Clear removes all parsed configuration files content cache, diff --git a/os/gcmd/gcmd_handler.go b/os/gcmd/gcmd_handler.go index 2bcb7a90510..73d6c73c342 100644 --- a/os/gcmd/gcmd_handler.go +++ b/os/gcmd/gcmd_handler.go @@ -8,13 +8,13 @@ package gcmd import ( - "errors" + "github.com/gogf/gf/errors/gerror" ) // BindHandle registers callback function <f> with <cmd>. func BindHandle(cmd string, f func()) error { if _, ok := defaultCommandFuncMap[cmd]; ok { - return errors.New("duplicated handle for command:" + cmd) + return gerror.New("duplicated handle for command:" + cmd) } else { defaultCommandFuncMap[cmd] = f } @@ -37,7 +37,7 @@ func RunHandle(cmd string) error { if handle, ok := defaultCommandFuncMap[cmd]; ok { handle() } else { - return errors.New("no handle found for command:" + cmd) + return gerror.New("no handle found for command:" + cmd) } return nil } @@ -49,10 +49,10 @@ func AutoRun() error { if handle, ok := defaultCommandFuncMap[cmd]; ok { handle() } else { - return errors.New("no handle found for command:" + cmd) + return gerror.New("no handle found for command:" + cmd) } } else { - return errors.New("no command found") + return gerror.New("no command found") } return nil } diff --git a/os/gcmd/gcmd_parser.go b/os/gcmd/gcmd_parser.go index f18e5093e23..1ac003ae2ea 100644 --- a/os/gcmd/gcmd_parser.go +++ b/os/gcmd/gcmd_parser.go @@ -8,15 +8,13 @@ package gcmd import ( - "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/json" "os" "strings" "github.com/gogf/gf/text/gstr" - "errors" - "github.com/gogf/gf/container/gvar" "github.com/gogf/gf/text/gregex" @@ -96,7 +94,7 @@ func ParseWithArgs(args []string, supportedOptions map[string]bool, strict ...bo i++ continue } else if parser.strict { - return nil, errors.New(fmt.Sprintf(`invalid option '%s'`, args[i])) + return nil, gerror.Newf(`invalid option '%s'`, args[i]) } } } diff --git a/os/gcmd/gcmd_parser_handler.go b/os/gcmd/gcmd_parser_handler.go index a2ab79eee68..6e61712f49a 100644 --- a/os/gcmd/gcmd_parser_handler.go +++ b/os/gcmd/gcmd_parser_handler.go @@ -8,20 +8,20 @@ package gcmd import ( - "errors" + "github.com/gogf/gf/errors/gerror" ) // BindHandle registers callback function <f> with <cmd>. func (p *Parser) BindHandle(cmd string, f func()) error { if _, ok := p.commandFuncMap[cmd]; ok { - return errors.New("duplicated handle for command:" + cmd) + return gerror.New("duplicated handle for command:" + cmd) } else { p.commandFuncMap[cmd] = f } return nil } -// BindHandle registers callback function with map <m>. +// BindHandleMap registers callback function with map <m>. func (p *Parser) BindHandleMap(m map[string]func()) error { var err error for k, v := range m { @@ -37,7 +37,7 @@ func (p *Parser) RunHandle(cmd string) error { if handle, ok := p.commandFuncMap[cmd]; ok { handle() } else { - return errors.New("no handle found for command:" + cmd) + return gerror.New("no handle found for command:" + cmd) } return nil } @@ -49,10 +49,10 @@ func (p *Parser) AutoRun() error { if handle, ok := p.commandFuncMap[cmd]; ok { handle() } else { - return errors.New("no handle found for command:" + cmd) + return gerror.New("no handle found for command:" + cmd) } } else { - return errors.New("no command found") + return gerror.New("no command found") } return nil } diff --git a/os/gcron/gcron_cron.go b/os/gcron/gcron_cron.go index 28e2b85225d..0e148b97086 100644 --- a/os/gcron/gcron_cron.go +++ b/os/gcron/gcron_cron.go @@ -7,8 +7,7 @@ package gcron import ( - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "time" "github.com/gogf/gf/container/garray" @@ -63,7 +62,7 @@ func (c *Cron) GetLogLevel() int { func (c *Cron) Add(pattern string, job func(), name ...string) (*Entry, error) { if len(name) > 0 { if c.Search(name[0]) != nil { - return nil, errors.New(fmt.Sprintf(`cron job "%s" already exists`, name[0])) + return nil, gerror.Newf(`cron job "%s" already exists`, name[0]) } } return c.addEntry(pattern, job, false, name...) diff --git a/os/gcron/gcron_schedule.go b/os/gcron/gcron_schedule.go index 017875a1ef7..a960843e31e 100644 --- a/os/gcron/gcron_schedule.go +++ b/os/gcron/gcron_schedule.go @@ -7,8 +7,7 @@ package gcron import ( - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/os/gtime" "strconv" "strings" @@ -91,7 +90,7 @@ func newSchedule(pattern string) (*cronSchedule, error) { }, nil } } else { - return nil, errors.New(fmt.Sprintf(`invalid pattern: "%s"`, pattern)) + return nil, gerror.Newf(`invalid pattern: "%s"`, pattern) } } // Handle the common cron pattern, like: @@ -140,7 +139,7 @@ func newSchedule(pattern string) (*cronSchedule, error) { } return schedule, nil } else { - return nil, errors.New(fmt.Sprintf(`invalid pattern: "%s"`, pattern)) + return nil, gerror.Newf(`invalid pattern: "%s"`, pattern) } } @@ -157,7 +156,7 @@ func parseItem(item string, min int, max int, allowQuestionMark bool) (map[int]s intervalArray := strings.Split(item, "/") if len(intervalArray) == 2 { if i, err := strconv.Atoi(intervalArray[1]); err != nil { - return nil, errors.New(fmt.Sprintf(`invalid pattern item: "%s"`, item)) + return nil, gerror.Newf(`invalid pattern item: "%s"`, item) } else { interval = i } @@ -179,7 +178,7 @@ func parseItem(item string, min int, max int, allowQuestionMark bool) (map[int]s // Eg: */5 if rangeArray[0] != "*" { if i, err := parseItemValue(rangeArray[0], fieldType); err != nil { - return nil, errors.New(fmt.Sprintf(`invalid pattern item: "%s"`, item)) + return nil, gerror.Newf(`invalid pattern item: "%s"`, item) } else { rangeMin = i rangeMax = i @@ -187,7 +186,7 @@ func parseItem(item string, min int, max int, allowQuestionMark bool) (map[int]s } if len(rangeArray) == 2 { if i, err := parseItemValue(rangeArray[1], fieldType); err != nil { - return nil, errors.New(fmt.Sprintf(`invalid pattern item: "%s"`, item)) + return nil, gerror.Newf(`invalid pattern item: "%s"`, item) } else { rangeMax = i } @@ -221,7 +220,7 @@ func parseItemValue(value string, fieldType byte) (int, error) { } } } - return 0, errors.New(fmt.Sprintf(`invalid pattern value: "%s"`, value)) + return 0, gerror.Newf(`invalid pattern value: "%s"`, value) } // meet checks if the given time <t> meets the runnable point for the job. diff --git a/os/gfsnotify/gfsnotify.go b/os/gfsnotify/gfsnotify.go index 670370b33b7..0224b8ba0a4 100644 --- a/os/gfsnotify/gfsnotify.go +++ b/os/gfsnotify/gfsnotify.go @@ -9,9 +9,8 @@ package gfsnotify import ( "context" - "errors" - "fmt" "github.com/gogf/gf/container/gset" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "sync" "time" @@ -140,7 +139,7 @@ func RemoveCallback(callbackId int) error { callback = r.(*Callback) } if callback == nil { - return errors.New(fmt.Sprintf(`callback for id %d not found`, callbackId)) + return gerror.Newf(`callback for id %d not found`, callbackId) } w.RemoveCallback(callbackId) return nil diff --git a/os/gfsnotify/gfsnotify_watcher.go b/os/gfsnotify/gfsnotify_watcher.go index ab0f92eec5e..3219c26c805 100644 --- a/os/gfsnotify/gfsnotify_watcher.go +++ b/os/gfsnotify/gfsnotify_watcher.go @@ -8,8 +8,7 @@ package gfsnotify import ( "context" - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/container/glist" @@ -66,7 +65,7 @@ func (w *Watcher) AddOnce(name, path string, callbackFunc func(event *Event), re func (w *Watcher) addWithCallbackFunc(name, path string, callbackFunc func(event *Event), recursive ...bool) (callback *Callback, err error) { // Check and convert the given path to absolute path. if t := fileRealPath(path); t == "" { - return nil, errors.New(fmt.Sprintf(`"%s" does not exist`, path)) + return nil, gerror.Newf(`"%s" does not exist`, path) } else { path = t } diff --git a/os/glog/glog_logger_config.go b/os/glog/glog_logger_config.go index ac295c1c522..54322a7f8d9 100644 --- a/os/glog/glog_logger_config.go +++ b/os/glog/glog_logger_config.go @@ -7,8 +7,6 @@ package glog import ( - "errors" - "fmt" "io" "strings" "time" @@ -82,7 +80,7 @@ func (l *Logger) SetConfig(config Config) error { // SetConfigWithMap set configurations with map for the logger. func (l *Logger) SetConfigWithMap(m map[string]interface{}) error { if m == nil || len(m) == 0 { - return errors.New("configuration cannot be empty") + return gerror.New("configuration cannot be empty") } // The m now is a shallow copy of m. // A little tricky, isn't it? @@ -93,7 +91,7 @@ func (l *Logger) SetConfigWithMap(m map[string]interface{}) error { if level, ok := levelStringMap[strings.ToUpper(gconv.String(levelValue))]; ok { m[levelKey] = level } else { - return errors.New(fmt.Sprintf(`invalid level string: %v`, levelValue)) + return gerror.Newf(`invalid level string: %v`, levelValue) } } // Change string configuration to int value for file rotation size. @@ -101,7 +99,7 @@ func (l *Logger) SetConfigWithMap(m map[string]interface{}) error { if rotateSizeValue != nil { m[rotateSizeKey] = gfile.StrToSize(gconv.String(rotateSizeValue)) if m[rotateSizeKey] == -1 { - return errors.New(fmt.Sprintf(`invalid rotate size: %v`, rotateSizeValue)) + return gerror.Newf(`invalid rotate size: %v`, rotateSizeValue) } } if err := gconv.Struct(m, &l.config); err != nil { @@ -206,7 +204,7 @@ func (l *Logger) GetWriter() io.Writer { // SetPath sets the directory path for file logging. func (l *Logger) SetPath(path string) error { if path == "" { - return errors.New("logging path is empty") + return gerror.New("logging path is empty") } if !gfile.Exists(path) { if err := gfile.Mkdir(path); err != nil { diff --git a/os/glog/glog_logger_level.go b/os/glog/glog_logger_level.go index fe1cfdb4e3f..9e9ba02ca96 100644 --- a/os/glog/glog_logger_level.go +++ b/os/glog/glog_logger_level.go @@ -7,8 +7,7 @@ package glog import ( - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "strings" ) @@ -78,7 +77,7 @@ func (l *Logger) SetLevelStr(levelStr string) error { if level, ok := levelStringMap[strings.ToUpper(levelStr)]; ok { l.config.Level = level } else { - return errors.New(fmt.Sprintf(`invalid level string: %s`, levelStr)) + return gerror.Newf(`invalid level string: %s`, levelStr) } return nil } diff --git a/os/gproc/gproc_comm.go b/os/gproc/gproc_comm.go index ca9911c8314..fc013eec961 100644 --- a/os/gproc/gproc_comm.go +++ b/os/gproc/gproc_comm.go @@ -7,9 +7,9 @@ package gproc import ( - "errors" "fmt" "github.com/gogf/gf/container/gmap" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/net/gtcp" "github.com/gogf/gf/os/gfile" "github.com/gogf/gf/util/gconv" @@ -65,7 +65,7 @@ func getConnByPid(pid int) (*gtcp.PoolConn, error) { return nil, err } } - return nil, errors.New(fmt.Sprintf("could not find port for pid: %d", pid)) + return nil, gerror.Newf("could not find port for pid: %d", pid) } // getPortByPid returns the listening port for specified pid. diff --git a/os/gproc/gproc_comm_send.go b/os/gproc/gproc_comm_send.go index b53c5132e53..7946c9a8177 100644 --- a/os/gproc/gproc_comm_send.go +++ b/os/gproc/gproc_comm_send.go @@ -7,7 +7,7 @@ package gproc import ( - "errors" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/json" "github.com/gogf/gf/net/gtcp" "io" @@ -46,7 +46,7 @@ func Send(pid int, data []byte, group ...string) error { err = json.UnmarshalUseNumber(result, response) if err == nil { if response.Code != 1 { - err = errors.New(response.Message) + err = gerror.New(response.Message) } } } diff --git a/os/gproc/gproc_process.go b/os/gproc/gproc_process.go index eb386345e26..51fa28d7048 100644 --- a/os/gproc/gproc_process.go +++ b/os/gproc/gproc_process.go @@ -8,8 +8,8 @@ package gproc import ( "context" - "errors" "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "os" "os/exec" @@ -101,7 +101,7 @@ func (p *Process) Send(data []byte) error { if p.Process != nil { return Send(p.Process.Pid, data) } - return errors.New("invalid process") + return gerror.New("invalid process") } // Release releases any resources associated with the Process p, diff --git a/os/grpool/grpool.go b/os/grpool/grpool.go index 136484367f0..707eb130454 100644 --- a/os/grpool/grpool.go +++ b/os/grpool/grpool.go @@ -8,8 +8,7 @@ package grpool import ( - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/container/glist" "github.com/gogf/gf/container/gtype" @@ -70,7 +69,7 @@ func Jobs() int { // The job will be executed asynchronously. func (p *Pool) Add(f func()) error { for p.closed.Val() { - return errors.New("pool closed") + return gerror.New("pool closed") } p.list.PushFront(f) // Check whether fork new goroutine or not. @@ -99,7 +98,7 @@ func (p *Pool) AddWithRecover(userFunc func(), recoverFunc ...func(err error)) e defer func() { if err := recover(); err != nil { if len(recoverFunc) > 0 && recoverFunc[0] != nil { - recoverFunc[0](errors.New(fmt.Sprintf(`%v`, err))) + recoverFunc[0](gerror.Newf(`%v`, err)) } } }() diff --git a/os/gsession/gsession_session.go b/os/gsession/gsession_session.go index 85d4d199539..2067d5b35d2 100644 --- a/os/gsession/gsession_session.go +++ b/os/gsession/gsession_session.go @@ -8,7 +8,7 @@ package gsession import ( "context" - "errors" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "time" @@ -182,7 +182,7 @@ func (s *Session) Id() string { // It returns error if it is called after session starts. func (s *Session) SetId(id string) error { if s.start { - return errors.New("session already started") + return gerror.New("session already started") } s.id = id return nil @@ -192,7 +192,7 @@ func (s *Session) SetId(id string) error { // It returns error if it is called after session starts. func (s *Session) SetIdFunc(f func(ttl time.Duration) string) error { if s.start { - return errors.New("session already started") + return gerror.New("session already started") } s.idFunc = f return nil diff --git a/os/gspath/gspath.go b/os/gspath/gspath.go index ca3c7c290fd..e543006290f 100644 --- a/os/gspath/gspath.go +++ b/os/gspath/gspath.go @@ -13,8 +13,7 @@ package gspath import ( "context" - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "os" "sort" @@ -104,7 +103,7 @@ func (sp *SPath) Set(path string) (realPath string, err error) { } } if realPath == "" { - return realPath, errors.New(fmt.Sprintf(`path "%s" does not exist`, path)) + return realPath, gerror.Newf(`path "%s" does not exist`, path) } // The set path must be a directory. if gfile.IsDir(realPath) { @@ -124,7 +123,7 @@ func (sp *SPath) Set(path string) (realPath string, err error) { sp.addMonitorByPath(realPath) return realPath, nil } else { - return "", errors.New(path + " should be a folder") + return "", gerror.New(path + " should be a folder") } } @@ -139,7 +138,7 @@ func (sp *SPath) Add(path string) (realPath string, err error) { } } if realPath == "" { - return realPath, errors.New(fmt.Sprintf(`path "%s" does not exist`, path)) + return realPath, gerror.Newf(`path "%s" does not exist`, path) } // The added path must be a directory. if gfile.IsDir(realPath) { @@ -153,7 +152,7 @@ func (sp *SPath) Add(path string) (realPath string, err error) { } return realPath, nil } else { - return "", errors.New(path + " should be a folder") + return "", gerror.New(path + " should be a folder") } } diff --git a/os/gview/gview_config.go b/os/gview/gview_config.go index 4e68da42124..e596b07bf73 100644 --- a/os/gview/gview_config.go +++ b/os/gview/gview_config.go @@ -8,8 +8,7 @@ package gview import ( "context" - "errors" - "fmt" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/i18n/gi18n" "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/os/gfile" @@ -75,7 +74,7 @@ func (view *View) SetConfig(config Config) error { // SetConfigWithMap set configurations with map for the view. func (view *View) SetConfigWithMap(m map[string]interface{}) error { if m == nil || len(m) == 0 { - return errors.New("configuration cannot be empty") + return gerror.New("configuration cannot be empty") } // The m now is a shallow copy of m. // Any changes to m does not affect the original one. @@ -124,7 +123,7 @@ func (view *View) SetPath(path string) error { } // Path not exist. if realPath == "" { - err := errors.New(fmt.Sprintf(`[gview] SetPath failed: path "%s" does not exist`, path)) + err := gerror.Newf(`[gview] SetPath failed: path "%s" does not exist`, path) if errorPrint() { glog.Error(err) } @@ -132,7 +131,7 @@ func (view *View) SetPath(path string) error { } // Should be a directory. if !isDir { - err := errors.New(fmt.Sprintf(`[gview] SetPath failed: path "%s" should be directory type`, path)) + err := gerror.Newf(`[gview] SetPath failed: path "%s" should be directory type`, path) if errorPrint() { glog.Error(err) } @@ -178,7 +177,7 @@ func (view *View) AddPath(path string) error { } // Path not exist. if realPath == "" { - err := errors.New(fmt.Sprintf(`[gview] AddPath failed: path "%s" does not exist`, path)) + err := gerror.Newf(`[gview] AddPath failed: path "%s" does not exist`, path) if errorPrint() { glog.Error(err) } @@ -186,7 +185,7 @@ func (view *View) AddPath(path string) error { } // realPath should be type of folder. if !isDir { - err := errors.New(fmt.Sprintf(`[gview] AddPath failed: path "%s" should be directory type`, path)) + err := gerror.Newf(`[gview] AddPath failed: path "%s" should be directory type`, path) if errorPrint() { glog.Error(err) } diff --git a/os/gview/gview_parse.go b/os/gview/gview_parse.go index e7675413a69..ef2e242b999 100644 --- a/os/gview/gview_parse.go +++ b/os/gview/gview_parse.go @@ -9,7 +9,6 @@ package gview import ( "bytes" "context" - "errors" "fmt" "github.com/gogf/gf/encoding/ghash" "github.com/gogf/gf/errors/gerror" @@ -377,7 +376,7 @@ func (view *View) searchFile(file string) (path string, folder string, resource if errorPrint() { glog.Error(buffer.String()) } - err = errors.New(fmt.Sprintf(`template file "%s" not found`, file)) + err = gerror.Newf(`template file "%s" not found`, file) } return } diff --git a/util/gconv/gconv_z_unit_struct_marshal_unmarshal_test.go b/util/gconv/gconv_z_unit_struct_marshal_unmarshal_test.go index 9fc08294447..b7c0941f5af 100644 --- a/util/gconv/gconv_z_unit_struct_marshal_unmarshal_test.go +++ b/util/gconv/gconv_z_unit_struct_marshal_unmarshal_test.go @@ -7,9 +7,9 @@ package gconv_test import ( - "errors" "github.com/gogf/gf/crypto/gcrc32" "github.com/gogf/gf/encoding/gbinary" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/test/gtest" @@ -83,16 +83,16 @@ func (p *Pkg) Marshal() []byte { func (p *Pkg) UnmarshalValue(v interface{}) error { b := gconv.Bytes(v) if len(b) < 6 { - return errors.New("invalid package length") + return gerror.New("invalid package length") } p.Length = gbinary.DecodeToUint16(b[:2]) if len(b) < int(p.Length) { - return errors.New("invalid data length") + return gerror.New("invalid data length") } p.Crc32 = gbinary.DecodeToUint32(b[2:6]) p.Data = b[6:] if gcrc32.Encrypt(p.Data) != p.Crc32 { - return errors.New("crc32 validation failed") + return gerror.New("crc32 validation failed") } return nil } diff --git a/util/gvalid/gvalid_validator_check_value.go b/util/gvalid/gvalid_validator_check_value.go index a72cee3b3b0..791a7d08074 100644 --- a/util/gvalid/gvalid_validator_check_value.go +++ b/util/gvalid/gvalid_validator_check_value.go @@ -7,7 +7,7 @@ package gvalid import ( - "errors" + "github.com/gogf/gf/errors/gerror" "strconv" "strings" "time" @@ -175,7 +175,7 @@ func (v *Validator) doCheckBuildInRules( "max-length", "size": if msg := v.checkLength(valueStr, ruleKey, rulePattern, customMsgMap); msg != "" { - return match, errors.New(msg) + return match, gerror.New(msg) } else { match = true } @@ -186,7 +186,7 @@ func (v *Validator) doCheckBuildInRules( "max", "between": if msg := v.checkRange(valueStr, ruleKey, rulePattern, customMsgMap); msg != "" { - return match, errors.New(msg) + return match, gerror.New(msg) } else { match = true } @@ -222,7 +222,7 @@ func (v *Validator) doCheckBuildInRules( var msg string msg = v.getErrorMessageByRule(ruleKey, customMsgMap) msg = strings.Replace(msg, ":format", rulePattern, -1) - return match, errors.New(msg) + return match, gerror.New(msg) } // Values of two fields should be equal as string. @@ -237,7 +237,7 @@ func (v *Validator) doCheckBuildInRules( var msg string msg = v.getErrorMessageByRule(ruleKey, customMsgMap) msg = strings.Replace(msg, ":field", rulePattern, -1) - return match, errors.New(msg) + return match, gerror.New(msg) } // Values of two fields should not be equal as string. @@ -253,7 +253,7 @@ func (v *Validator) doCheckBuildInRules( var msg string msg = v.getErrorMessageByRule(ruleKey, customMsgMap) msg = strings.Replace(msg, ":field", rulePattern, -1) - return match, errors.New(msg) + return match, gerror.New(msg) } // Field value should be in range of. @@ -436,7 +436,7 @@ func (v *Validator) doCheckBuildInRules( match = gregex.IsMatchString(`^([0-9A-Fa-f]{2}[\-:]){5}[0-9A-Fa-f]{2}$`, valueStr) default: - return match, errors.New("Invalid rule name: " + ruleKey) + return match, gerror.New("Invalid rule name: " + ruleKey) } return match, nil }