-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
49 lines (40 loc) · 1.21 KB
/
errors.go
File metadata and controls
49 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package emir
import "sync"
var errorPool sync.Pool
// AcquireBasicError returns an error instance from context pool
// The returned instance might be dirty
// You should set all fields before using
//
// The returned error instancec may be passed to ReleaseBasicError when it is no longer needed
// It is forbidden accessing to the released error instance
func AcquireBasicError() *BasicError {
v := errorPool.Get()
if v == nil {
return new(BasicError)
}
return v.(*BasicError)
}
// ReleaseBasicError returns acquired error instance to the error pool
//
// It is forbidden accessing to the released error instance
func ReleaseBasicError(err *BasicError) {
err.ErrorCode = nil
errorPool.Put(err)
}
func (err *BasicError) Error() string {
return err.ErrorMessage
}
// NewBasicError returns an error instance that carries status, error message, and code
//
// Returned error should be released after used
// If you are using a custom error handler, you should release BasicError instances
func NewBasicError(status int, errorMessage string, errorCode ...interface{}) error {
err := &BasicError{
StatusCode: status,
ErrorMessage: errorMessage,
}
if len(errorCode) != 0 {
err.ErrorCode = errorCode[0]
}
return err
}