Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanvc committed Jan 11, 2024
1 parent 635d5c7 commit 49ecb08
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
20 changes: 11 additions & 9 deletions base/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,23 @@ func (se *statusError) Error() string {
return se.s.GetMsg()
}

func (se *statusError) EvoStatus() *Status {
return se.s
}

func Convert(err error) *Status {
s, ok := FromError(err)
if ok {
return s
}
return New(codes.Unknown, "UnknownStatus").SetMsg(err.Error())
s, _ := FromError(err)
return s
}

func FromError(err error) (*Status, bool) {
if err == nil {
return nil, true
}
var realErr *statusError
if ok := errors.As(err, &realErr); ok {
return realErr.s, true
type evostatus interface{ EvoStatus() *Status }
var es evostatus
if ok := errors.As(err, &es); ok {
return es.EvoStatus(), true
}
return nil, false
return New(codes.Unknown, "").SetMsg(err.Error()), false
}
7 changes: 3 additions & 4 deletions examples/metricapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"github.com/ethanvc/evo/base"
"github.com/ethanvc/evo/evohttp"
Expand Down Expand Up @@ -87,15 +88,13 @@ func (controller *userController) QueryUser(c context.Context, req *QueryUserReq
func (controller *userController) queryUserFromCache(c context.Context, req *QueryUserReq) (resp *UserDto, err error) {
// for request resolved network, have to record time cost and response content.
c = plog.WithLogContext(c, nil)
defer func() {
plog.RequestLog(c, err, req, resp)
}()
defer func() { plog.RequestLog(c, err, req, resp) }()
c, cancel := context.WithTimeoutCause(c, time.Millisecond*100,
base.New(codes.DeadlineExceeded, "GetFromRedisTimeout").Err())
defer cancel()
cmd := controller.redisCli.Get(c, fmt.Sprintf("a_%d", req.Uid))
if cmd.Err() != nil {
return nil, cmd.Err()
return nil, errors.Join(base.New(codes.Internal, "UnknownRedisGetErr").Err(), cmd.Err())
}
return
}

0 comments on commit 49ecb08

Please sign in to comment.