From d0b8e93b80c086eb8ce0bd5fa0d6d12d2bc7b62b Mon Sep 17 00:00:00 2001 From: lab Date: Wed, 10 Nov 2021 02:33:37 +0800 Subject: [PATCH] feat: wrap err with original error --- go.mod | 2 +- go.sum | 3 ++- pkg/mp/auth/auth.go | 22 +++++++++++----------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 7b21060..227377c 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/golang/protobuf v1.5.2 github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0 - github.com/rs/zerolog v1.15.0 + github.com/pkg/errors v0.9.1 google.golang.org/genproto v0.0.0-20211104193956-4c6863e31247 google.golang.org/grpc v1.42.0 google.golang.org/protobuf v1.27.1 diff --git a/go.sum b/go.sum index a833569..06a72e4 100644 --- a/go.sum +++ b/go.sum @@ -209,6 +209,8 @@ github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= @@ -216,7 +218,6 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= -github.com/rs/zerolog v1.15.0 h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= diff --git a/pkg/mp/auth/auth.go b/pkg/mp/auth/auth.go index 93d0b1b..90651ec 100644 --- a/pkg/mp/auth/auth.go +++ b/pkg/mp/auth/auth.go @@ -2,9 +2,9 @@ package auth import ( "context" - "errors" "time" + "github.com/pkg/errors" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -64,7 +64,7 @@ func (srv Service) GetAuthCodeURL(ctx context.Context, req *pb.GetCodeURLRequest func (srv Service) ExchangeToken(ctx context.Context, req *pb.ExchangeTokenRequest) (*pb.ExchangeTokenResponse, error) { resp, err := srv.client.ExchangeToken(ctx, req.Code) if err != nil { - return nil, status.Errorf(codes.Internal, "exchange token from weixin failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "exchange token from weixin failed").Error()) } var token Token @@ -76,7 +76,7 @@ func (srv Service) ExchangeToken(ctx context.Context, req *pb.ExchangeTokenReque } token.Assign(resp) if err := srv.db.Save(&token).Error; err != nil { - return nil, status.Errorf(codes.Internal, "save token to database failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "save token to database failed").Error()) } go srv.PublishEvent(ctx, "auth.token.exchanged", &token) @@ -96,13 +96,13 @@ func (srv Service) RefreshToken(ctx context.Context, req *pb.RefreshTokenRequest resp, err := srv.client.RefreshToken(ctx, token.RefreshToken) if err != nil { - return nil, status.Errorf(codes.Internal, "refresh refreshed token from weixin failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "refresh refreshed token from weixin failed").Error()) } token.Assign(resp) if err := srv.db.Save(&token).Error; err != nil { - return nil, status.Errorf(codes.Internal, "save refreshed token to database failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "save refreshed token to database failed").Error()) } go srv.PublishEvent(ctx, "auth.token.refreshed", &token) @@ -127,12 +127,12 @@ func (srv Service) GetUserinfo(ctx context.Context, req *pb.GetUserinfoRequest) resp, err := srv.client.GetUserinfo(ctx, token.AccessToken, req.OpenId, mpauth.Lang(req.Lang.String())) if err != nil { - return nil, status.Errorf(codes.Internal, "get userinfo token from weixin failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "get userinfo token from weixin failed").Error()) } userinfo = Userinfo{Userinfo: *resp} if err := srv.db.Create(&userinfo).Error; err != nil { - return nil, status.Errorf(codes.Internal, "save userinfo to database failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "save userinfo to database failed").Error()) } go srv.PublishEvent(ctx, "auth.userinfo.created", &userinfo) @@ -160,12 +160,12 @@ func (srv Service) SyncUserinfo(ctx context.Context, req *pb.SyncUserinfoRequest resp, err := srv.client.GetUserinfo(ctx, token.AccessToken, req.OpenId, mpauth.Lang(req.Lang.String())) if err != nil { - return nil, status.Errorf(codes.Internal, "get userinfo token from weixin failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "get userinfo token from weixin failed").Error()) } userinfo = Userinfo{Userinfo: *resp} if err := srv.db.Save(&userinfo).Error; err != nil { - return nil, status.Errorf(codes.Internal, "sync userinfo and save to database failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "sync userinfo and save to database failed").Error()) } go srv.PublishEvent(ctx, "auth.userinfo.synchronized", &userinfo) @@ -188,13 +188,13 @@ func (srv Service) GetClientCredential(ctx context.Context, _ *emptypb.Empty) (* if errors.Is(err, gorm.ErrRecordNotFound) || cred.Expired() { resp, err := srv.client.GetClientCredential(ctx) if err != nil { - return nil, status.Errorf(codes.Internal, "get client credential token from weixin failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "get client credential token from weixin failed").Error()) } cred = ClientCredential{ ClientCredential: *resp, } if err := srv.db.Create(&cred).Error; err != nil { - return nil, status.Errorf(codes.Internal, "save client credential to database failed") + return nil, status.Errorf(codes.Internal, errors.Wrap(err, "save client credential to database failed").Error()) } go srv.PublishEvent(ctx, "auth.clientcredential.created", &cred)