Skip to content

Commit 0ddacdd

Browse files
committed
add error code for components
1 parent f72d991 commit 0ddacdd

File tree

88 files changed

+434
-263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+434
-263
lines changed

container/garray/garray_normal_any.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (a *Array) Set(index int, value interface{}) error {
123123
a.mu.Lock()
124124
defer a.mu.Unlock()
125125
if index < 0 || index >= len(a.array) {
126-
return gerror.Newf("index %d out of array range %d", index, len(a.array))
126+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
127127
}
128128
a.array[index] = value
129129
return nil
@@ -176,7 +176,7 @@ func (a *Array) InsertBefore(index int, value interface{}) error {
176176
a.mu.Lock()
177177
defer a.mu.Unlock()
178178
if index < 0 || index >= len(a.array) {
179-
return gerror.Newf("index %d out of array range %d", index, len(a.array))
179+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
180180
}
181181
rear := append([]interface{}{}, a.array[index:]...)
182182
a.array = append(a.array[0:index], value)
@@ -189,7 +189,7 @@ func (a *Array) InsertAfter(index int, value interface{}) error {
189189
a.mu.Lock()
190190
defer a.mu.Unlock()
191191
if index < 0 || index >= len(a.array) {
192-
return gerror.Newf("index %d out of array range %d", index, len(a.array))
192+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
193193
}
194194
rear := append([]interface{}{}, a.array[index+1:]...)
195195
a.array = append(a.array[0:index+1], value)
@@ -545,7 +545,7 @@ func (a *Array) Fill(startIndex int, num int, value interface{}) error {
545545
a.mu.Lock()
546546
defer a.mu.Unlock()
547547
if startIndex < 0 || startIndex > len(a.array) {
548-
return gerror.Newf("index %d out of array range %d", startIndex, len(a.array))
548+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", startIndex, len(a.array))
549549
}
550550
for i := startIndex; i < startIndex+num; i++ {
551551
if i > len(a.array)-1 {

container/garray/garray_normal_int.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (a *IntArray) Set(index int, value int) error {
104104
a.mu.Lock()
105105
defer a.mu.Unlock()
106106
if index < 0 || index >= len(a.array) {
107-
return gerror.Newf("index %d out of array range %d", index, len(a.array))
107+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
108108
}
109109
a.array[index] = value
110110
return nil
@@ -175,7 +175,7 @@ func (a *IntArray) InsertBefore(index int, value int) error {
175175
a.mu.Lock()
176176
defer a.mu.Unlock()
177177
if index < 0 || index >= len(a.array) {
178-
return gerror.Newf("index %d out of array range %d", index, len(a.array))
178+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
179179
}
180180
rear := append([]int{}, a.array[index:]...)
181181
a.array = append(a.array[0:index], value)
@@ -188,7 +188,7 @@ func (a *IntArray) InsertAfter(index int, value int) error {
188188
a.mu.Lock()
189189
defer a.mu.Unlock()
190190
if index < 0 || index >= len(a.array) {
191-
return gerror.Newf("index %d out of array range %d", index, len(a.array))
191+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
192192
}
193193
rear := append([]int{}, a.array[index+1:]...)
194194
a.array = append(a.array[0:index+1], value)
@@ -559,7 +559,7 @@ func (a *IntArray) Fill(startIndex int, num int, value int) error {
559559
a.mu.Lock()
560560
defer a.mu.Unlock()
561561
if startIndex < 0 || startIndex > len(a.array) {
562-
return gerror.Newf("index %d out of array range %d", startIndex, len(a.array))
562+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", startIndex, len(a.array))
563563
}
564564
for i := startIndex; i < startIndex+num; i++ {
565565
if i > len(a.array)-1 {

container/garray/garray_normal_str.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (a *StrArray) Set(index int, value string) error {
9090
a.mu.Lock()
9191
defer a.mu.Unlock()
9292
if index < 0 || index >= len(a.array) {
93-
return gerror.Newf("index %d out of array range %d", index, len(a.array))
93+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
9494
}
9595
a.array[index] = value
9696
return nil
@@ -162,7 +162,7 @@ func (a *StrArray) InsertBefore(index int, value string) error {
162162
a.mu.Lock()
163163
defer a.mu.Unlock()
164164
if index < 0 || index >= len(a.array) {
165-
return gerror.Newf("index %d out of array range %d", index, len(a.array))
165+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
166166
}
167167
rear := append([]string{}, a.array[index:]...)
168168
a.array = append(a.array[0:index], value)
@@ -175,7 +175,7 @@ func (a *StrArray) InsertAfter(index int, value string) error {
175175
a.mu.Lock()
176176
defer a.mu.Unlock()
177177
if index < 0 || index >= len(a.array) {
178-
return gerror.Newf("index %d out of array range %d", index, len(a.array))
178+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
179179
}
180180
rear := append([]string{}, a.array[index+1:]...)
181181
a.array = append(a.array[0:index+1], value)
@@ -562,7 +562,7 @@ func (a *StrArray) Fill(startIndex int, num int, value string) error {
562562
a.mu.Lock()
563563
defer a.mu.Unlock()
564564
if startIndex < 0 || startIndex > len(a.array) {
565-
return gerror.Newf("index %d out of array range %d", startIndex, len(a.array))
565+
return gerror.NewCodef(gerror.CodeInvalidParameter, "index %d out of array range %d", startIndex, len(a.array))
566566
}
567567
for i := startIndex; i < startIndex+num; i++ {
568568
if i > len(a.array)-1 {

container/gpool/gpool.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func New(ttl time.Duration, newFunc NewFunc, expireFunc ...ExpireFunc) *Pool {
6666
// Put puts an item to pool.
6767
func (p *Pool) Put(value interface{}) error {
6868
if p.closed.Val() {
69-
return gerror.New("pool is closed")
69+
return gerror.NewCode(gerror.CodeInvalidOperation, "pool is closed")
7070
}
7171
item := &poolItem{
7272
value: value,
@@ -117,7 +117,7 @@ func (p *Pool) Get() (interface{}, error) {
117117
if p.NewFunc != nil {
118118
return p.NewFunc()
119119
}
120-
return nil, gerror.New("pool is empty")
120+
return nil, gerror.NewCode(gerror.CodeInvalidOperation, "pool is empty")
121121
}
122122

123123
// Size returns the count of available items of pool.

crypto/gaes/gaes.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error) {
6363
}
6464
blockSize := block.BlockSize()
6565
if len(cipherText) < blockSize {
66-
return nil, gerror.New("cipherText too short")
66+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "cipherText too short")
6767
}
6868
ivValue := ([]byte)(nil)
6969
if len(iv) > 0 {
@@ -72,7 +72,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error) {
7272
ivValue = []byte(IVDefaultValue)
7373
}
7474
if len(cipherText)%blockSize != 0 {
75-
return nil, gerror.New("cipherText is not a multiple of the block size")
75+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "cipherText is not a multiple of the block size")
7676
}
7777
blockModel := cipher.NewCBCDecrypter(block, ivValue)
7878
plainText := make([]byte, len(cipherText))
@@ -93,22 +93,22 @@ func PKCS5Padding(src []byte, blockSize int) []byte {
9393
func PKCS5UnPadding(src []byte, blockSize int) ([]byte, error) {
9494
length := len(src)
9595
if blockSize <= 0 {
96-
return nil, gerror.New("invalid blocklen")
96+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "invalid blocklen")
9797
}
9898

9999
if length%blockSize != 0 || length == 0 {
100-
return nil, gerror.New("invalid data len")
100+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "invalid data len")
101101
}
102102

103103
unpadding := int(src[length-1])
104104
if unpadding > blockSize || unpadding == 0 {
105-
return nil, gerror.New("invalid padding")
105+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "invalid padding")
106106
}
107107

108108
padding := src[length-unpadding:]
109109
for i := 0; i < unpadding; i++ {
110110
if padding[i] != byte(unpadding) {
111-
return nil, gerror.New("invalid padding")
111+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "invalid padding")
112112
}
113113
}
114114

@@ -146,7 +146,7 @@ func DecryptCFB(cipherText []byte, key []byte, unPadding int, iv ...[]byte) ([]b
146146
return nil, err
147147
}
148148
if len(cipherText) < aes.BlockSize {
149-
return nil, gerror.New("cipherText too short")
149+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "cipherText too short")
150150
}
151151
ivValue := ([]byte)(nil)
152152
if len(iv) > 0 {

crypto/gdes/gdes.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func DecryptECB(cipherText []byte, key []byte, padding int) ([]byte, error) {
6666
// The length of the <key> should be either 16 or 24 bytes.
6767
func EncryptECBTriple(plainText []byte, key []byte, padding int) ([]byte, error) {
6868
if len(key) != 16 && len(key) != 24 {
69-
return nil, gerror.New("key length error")
69+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "key length error")
7070
}
7171

7272
text, err := Padding(plainText, padding)
@@ -100,7 +100,7 @@ func EncryptECBTriple(plainText []byte, key []byte, padding int) ([]byte, error)
100100
// The length of the <key> should be either 16 or 24 bytes.
101101
func DecryptECBTriple(cipherText []byte, key []byte, padding int) ([]byte, error) {
102102
if len(key) != 16 && len(key) != 24 {
103-
return nil, gerror.New("key length error")
103+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "key length error")
104104
}
105105

106106
var newKey []byte
@@ -138,7 +138,7 @@ func EncryptCBC(plainText []byte, key []byte, iv []byte, padding int) ([]byte, e
138138
}
139139

140140
if len(iv) != block.BlockSize() {
141-
return nil, gerror.New("iv length invalid")
141+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "iv length invalid")
142142
}
143143

144144
text, err := Padding(plainText, padding)
@@ -161,7 +161,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv []byte, padding int) ([]byte,
161161
}
162162

163163
if len(iv) != block.BlockSize() {
164-
return nil, gerror.New("iv length invalid")
164+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "iv length invalid")
165165
}
166166

167167
text := make([]byte, len(cipherText))
@@ -179,7 +179,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv []byte, padding int) ([]byte,
179179
// EncryptCBCTriple encrypts <plainText> using TripleDES and CBC mode.
180180
func EncryptCBCTriple(plainText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
181181
if len(key) != 16 && len(key) != 24 {
182-
return nil, gerror.New("key length invalid")
182+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "key length invalid")
183183
}
184184

185185
var newKey []byte
@@ -196,7 +196,7 @@ func EncryptCBCTriple(plainText []byte, key []byte, iv []byte, padding int) ([]b
196196
}
197197

198198
if len(iv) != block.BlockSize() {
199-
return nil, gerror.New("iv length invalid")
199+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "iv length invalid")
200200
}
201201

202202
text, err := Padding(plainText, padding)
@@ -214,7 +214,7 @@ func EncryptCBCTriple(plainText []byte, key []byte, iv []byte, padding int) ([]b
214214
// DecryptCBCTriple decrypts <cipherText> using TripleDES and CBC mode.
215215
func DecryptCBCTriple(cipherText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
216216
if len(key) != 16 && len(key) != 24 {
217-
return nil, gerror.New("key length invalid")
217+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "key length invalid")
218218
}
219219

220220
var newKey []byte
@@ -231,7 +231,7 @@ func DecryptCBCTriple(cipherText []byte, key []byte, iv []byte, padding int) ([]
231231
}
232232

233233
if len(iv) != block.BlockSize() {
234-
return nil, gerror.New("iv length invalid")
234+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "iv length invalid")
235235
}
236236

237237
text := make([]byte, len(cipherText))
@@ -262,12 +262,12 @@ func Padding(text []byte, padding int) ([]byte, error) {
262262
switch padding {
263263
case NOPADDING:
264264
if len(text)%8 != 0 {
265-
return nil, gerror.New("text length invalid")
265+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "text length invalid")
266266
}
267267
case PKCS5PADDING:
268268
return PaddingPKCS5(text, 8), nil
269269
default:
270-
return nil, gerror.New("padding type error")
270+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "padding type error")
271271
}
272272

273273
return text, nil
@@ -277,12 +277,12 @@ func UnPadding(text []byte, padding int) ([]byte, error) {
277277
switch padding {
278278
case NOPADDING:
279279
if len(text)%8 != 0 {
280-
return nil, gerror.New("text length invalid")
280+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "text length invalid")
281281
}
282282
case PKCS5PADDING:
283283
return UnPaddingPKCS5(text), nil
284284
default:
285-
return nil, gerror.New("padding type error")
285+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "padding type error")
286286
}
287287
return text, nil
288288
}

database/gdb/gdb.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package gdb
1010
import (
1111
"context"
1212
"database/sql"
13-
"fmt"
1413
"time"
1514

1615
"github.com/gogf/gf/errors/gerror"
@@ -333,7 +332,10 @@ func New(group ...string) (db DB, err error) {
333332
defer configs.RUnlock()
334333

335334
if len(configs.config) < 1 {
336-
return nil, gerror.New("database configuration is empty, please set the database configuration before using")
335+
return nil, gerror.NewCode(
336+
gerror.CodeInvalidConfiguration,
337+
"database configuration is empty, please set the database configuration before using",
338+
)
337339
}
338340
if _, ok := configs.config[groupName]; ok {
339341
if node, err := getConfigNodeByGroup(groupName, true); err == nil {
@@ -352,7 +354,8 @@ func New(group ...string) (db DB, err error) {
352354
}
353355
return c.db, nil
354356
} else {
355-
return nil, gerror.Newf(
357+
return nil, gerror.NewCodef(
358+
gerror.CodeInvalidConfiguration,
356359
`cannot find database driver for specified database type "%s", did you misspell type name "%s" or forget importing the database driver?`,
357360
node.Type, node.Type,
358361
)
@@ -361,7 +364,8 @@ func New(group ...string) (db DB, err error) {
361364
return nil, err
362365
}
363366
} else {
364-
return nil, gerror.Newf(
367+
return nil, gerror.NewCodef(
368+
gerror.CodeInvalidConfiguration,
365369
`database configuration node "%s" is not found, did you misspell group name "%s" or miss the database configuration?`,
366370
groupName, groupName,
367371
)
@@ -404,7 +408,7 @@ func getConfigNodeByGroup(group string, master bool) (*ConfigNode, error) {
404408
}
405409
}
406410
if len(masterList) < 1 {
407-
return nil, gerror.New("at least one master node configuration's need to make sense")
411+
return nil, gerror.NewCode(gerror.CodeInvalidConfiguration, "at least one master node configuration's need to make sense")
408412
}
409413
if len(slaveList) < 1 {
410414
slaveList = masterList
@@ -415,7 +419,7 @@ func getConfigNodeByGroup(group string, master bool) (*ConfigNode, error) {
415419
return getConfigNodeByWeight(slaveList), nil
416420
}
417421
} else {
418-
return nil, gerror.New(fmt.Sprintf("empty database configuration for item name '%s'", group))
422+
return nil, gerror.NewCodef(gerror.CodeInvalidConfiguration, "empty database configuration for item name '%s'", group)
419423
}
420424
}
421425

database/gdb/gdb_core.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (c *Core) GetCtxTimeout(timeoutType int, ctx context.Context) (context.Cont
8989
return context.WithTimeout(ctx, c.db.GetConfig().PrepareTimeout)
9090
}
9191
default:
92-
panic(gerror.Newf("invalid context timeout type: %d", timeoutType))
92+
panic(gerror.NewCodef(gerror.CodeInvalidParameter, "invalid context timeout type: %d", timeoutType))
9393
}
9494
return ctx, func() {}
9595
}
@@ -553,7 +553,7 @@ func (c *Core) DoUpdate(ctx context.Context, link Link, table string, data inter
553553
updates = gconv.String(data)
554554
}
555555
if len(updates) == 0 {
556-
return nil, gerror.New("data cannot be empty")
556+
return nil, gerror.NewCode(gerror.CodeMissingParameter, "data cannot be empty")
557557
}
558558
if len(params) > 0 {
559559
args = append(params, args...)

database/gdb/gdb_core_underlying.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (c *Core) DoExec(ctx context.Context, link Link, sql string, args ...interf
136136
func (c *Core) DoCommit(ctx context.Context, link Link, sql string, args []interface{}) (newSql string, newArgs []interface{}, err error) {
137137
if c.db.GetConfig().CtxStrict {
138138
if v := ctx.Value(ctxStrictKeyName); v == nil {
139-
return sql, args, gerror.New(ctxStrictErrorStr)
139+
return sql, args, gerror.NewCode(gerror.CodeMissingParameter, ctxStrictErrorStr)
140140
}
141141
}
142142
return sql, args, nil
@@ -181,7 +181,7 @@ func (c *Core) DoPrepare(ctx context.Context, link Link, sql string) (*Stmt, err
181181

182182
if c.db.GetConfig().CtxStrict {
183183
if v := ctx.Value(ctxStrictKeyName); v == nil {
184-
return nil, gerror.New(ctxStrictErrorStr)
184+
return nil, gerror.NewCode(gerror.CodeMissingParameter, ctxStrictErrorStr)
185185
}
186186
}
187187

database/gdb/gdb_driver_mssql.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (d *DriverMssql) TableFields(ctx context.Context, table string, schema ...s
214214
charL, charR := d.GetChars()
215215
table = gstr.Trim(table, charL+charR)
216216
if gstr.Contains(table, " ") {
217-
return nil, gerror.New("function TableFields supports only single table operations")
217+
return nil, gerror.NewCode(gerror.CodeInvalidParameter, "function TableFields supports only single table operations")
218218
}
219219
useSchema := d.db.GetSchema()
220220
if len(schema) > 0 && schema[0] != "" {
@@ -292,10 +292,10 @@ ORDER BY a.id,a.colorder`,
292292
func (d *DriverMssql) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
293293
switch option.InsertOption {
294294
case insertOptionSave:
295-
return nil, gerror.New(`Save operation is not supported by mssql driver`)
295+
return nil, gerror.NewCode(gerror.CodeNotSupported, `Save operation is not supported by mssql driver`)
296296

297297
case insertOptionReplace:
298-
return nil, gerror.New(`Replace operation is not supported by mssql driver`)
298+
return nil, gerror.NewCode(gerror.CodeNotSupported, `Replace operation is not supported by mssql driver`)
299299

300300
default:
301301
return d.Core.DoInsert(ctx, link, table, list, option)

0 commit comments

Comments
 (0)