Skip to content

Commit

Permalink
fix: 优化了错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
universero committed Nov 12, 2024
1 parent eb72a89 commit bbb1c1f
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions biz/infrastructure/consts/errors.go
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("获取余额失败"))
)

0 comments on commit bbb1c1f

Please sign in to comment.