-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb72a89
commit bbb1c1f
Showing
1 changed file
with
41 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,43 @@ | ||
package consts | ||
|
||
import "errors" | ||
|
||
var ErrNotAuthentication = errors.New("not authentication") | ||
var ErrTimeout = errors.New("calling time exceeded the threshold") | ||
var ErrInvalidKey = errors.New("invalid key") | ||
var ErrUnInterface = errors.New("invalid interface") | ||
var ErrUnavailableInterface = errors.New("unavailable interface") | ||
var ErrCall = errors.New("call failed") | ||
var ErrSignature = errors.New("signature invalid") | ||
var ErrRole = errors.New("没有权限") | ||
var ErrMargin = errors.New("获取余额失败") | ||
import ( | ||
"errors" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type Errno struct { | ||
err error | ||
code codes.Code | ||
} | ||
|
||
// GRPCStatus 实现 GRPCStatus 方法 | ||
func (en *Errno) GRPCStatus() *status.Status { | ||
return status.New(en.code, en.err.Error()) | ||
} | ||
|
||
// 实现 Error 方法 | ||
func (en *Errno) Error() string { | ||
return en.err.Error() | ||
} | ||
|
||
// NewErrno 创建自定义错误 | ||
func NewErrno(code codes.Code, err error) *Errno { | ||
return &Errno{ | ||
err: err, | ||
code: code, | ||
} | ||
} | ||
|
||
// 定义常量错误 | ||
var ( | ||
ErrNotAuthentication = NewErrno(codes.Unauthenticated, errors.New("not authentication")) | ||
ErrTimeout = NewErrno(codes.DeadlineExceeded, errors.New("calling time exceeded the threshold")) | ||
ErrInvalidKey = NewErrno(codes.InvalidArgument, errors.New("invalid key")) | ||
ErrUnInterface = NewErrno(codes.InvalidArgument, errors.New("invalid interface")) | ||
ErrUnavailableInterface = NewErrno(codes.Unavailable, errors.New("unavailable interface")) | ||
ErrCall = NewErrno(codes.Code(1001), errors.New("call failed")) | ||
ErrSignature = NewErrno(codes.InvalidArgument, errors.New("signature invalid")) | ||
ErrRole = NewErrno(codes.PermissionDenied, errors.New("没有权限")) | ||
ErrMargin = NewErrno(codes.Code(1002), errors.New("获取余额失败")) | ||
) |