Skip to content

Commit

Permalink
unify error package to gerror
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Jun 26, 2021
1 parent b958689 commit d109706
Show file tree
Hide file tree
Showing 41 changed files with 151 additions and 169 deletions.
10 changes: 5 additions & 5 deletions container/garray/garray_normal_any.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions container/garray/garray_normal_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package garray

import (
"bytes"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/internal/json"
"math"
"sort"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions container/garray/garray_normal_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions container/gpool/gpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package gpool

import (
"errors"
"github.com/gogf/gf/errors/gerror"
"time"

"github.com/gogf/gf/container/glist"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions crypto/gaes/gaes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"errors"
"github.com/gogf/gf/errors/gerror"
)

var (
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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")
}
}

Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 13 additions & 13 deletions crypto/gdes/gdes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"bytes"
"crypto/cipher"
"crypto/des"
"errors"
"github.com/gogf/gf/errors/gerror"
)

const (
Expand Down Expand Up @@ -66,7 +66,7 @@ func DecryptECB(cipherText []byte, key []byte, padding int) ([]byte, error) {
// The length of the <key> 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)
Expand Down Expand Up @@ -100,7 +100,7 @@ func EncryptECBTriple(plainText []byte, key []byte, padding int) ([]byte, error)
// The length of the <key> 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
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -179,7 +179,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv []byte, padding int) ([]byte,
// EncryptCBCTriple encrypts <plainText> 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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions database/gredis/gredis_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...)
}
Expand Down Expand Up @@ -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))
}
Expand Down
Loading

0 comments on commit d109706

Please sign in to comment.