作者 yangfu

fix: 微调

@@ -9,6 +9,7 @@ require ( @@ -9,6 +9,7 @@ require (
9 github.com/go-pg/pg/v10 v10.0.0-beta.2 9 github.com/go-pg/pg/v10 v10.0.0-beta.2
10 github.com/tiptok/gocomm v1.0.2 10 github.com/tiptok/gocomm v1.0.2
11 github.com/tal-tech/go-zero v1.0.11 11 github.com/tal-tech/go-zero v1.0.11
  12 + github.com/linmadan/egglib-go v0.0.0-20191217144343-ca4539f95bf9
12 ) 13 )
13 14
14 replace github.com/tiptok/gocomm v1.0.2 => F:\go\src\learn_project\gocomm 15 replace github.com/tiptok/gocomm v1.0.2 => F:\go\src\learn_project\gocomm
@@ -3,6 +3,7 @@ package user @@ -3,6 +3,7 @@ package user
3 import ( 3 import (
4 "crypto/sha1" 4 "crypto/sha1"
5 "fmt" 5 "fmt"
  6 + "github.com/linmadan/egglib-go/core/application"
6 "github.com/tiptok/gocomm/common" 7 "github.com/tiptok/gocomm/common"
7 "github.com/tiptok/gocomm/pkg/log" 8 "github.com/tiptok/gocomm/pkg/log"
8 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory" 9 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
@@ -20,12 +21,12 @@ func (svr *UserService) CreateUser(header *protocol.RequestHeader, request *prot @@ -20,12 +21,12 @@ func (svr *UserService) CreateUser(header *protocol.RequestHeader, request *prot
20 ) 21 )
21 rsp = &protocolx.CreateUserResponse{} 22 rsp = &protocolx.CreateUserResponse{}
22 if err = request.ValidateCommand(); err != nil { 23 if err = request.ValidateCommand(); err != nil {
23 - err = protocol.NewCustomMessage(2, err.Error()) 24 + err = application.ThrowError(application.ARG_ERROR, err.Error())
24 return 25 return
25 } 26 }
26 if err = transactionContext.StartTransaction(); err != nil { 27 if err = transactionContext.StartTransaction(); err != nil {
27 - log.Error(err)  
28 - return nil, err 28 + err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  29 + return
29 } 30 }
30 defer func() { 31 defer func() {
31 transactionContext.RollbackTransaction() 32 transactionContext.RollbackTransaction()
@@ -49,12 +50,16 @@ func (svr *UserService) CreateUser(header *protocol.RequestHeader, request *prot @@ -49,12 +50,16 @@ func (svr *UserService) CreateUser(header *protocol.RequestHeader, request *prot
49 if len(newUser.Passwd) == 0 { 50 if len(newUser.Passwd) == 0 {
50 newUser.Passwd = fmt.Sprintf("%x", sha1.Sum([]byte("123456"))) 51 newUser.Passwd = fmt.Sprintf("%x", sha1.Sum([]byte("123456")))
51 } 52 }
52 - if m, err := UserRepository.Save(newUser); err != nil {  
53 - return nil, err 53 + if m, e := UserRepository.Save(newUser); e != nil {
  54 + err = application.ThrowError(application.INTERNAL_SERVER_ERROR, e.Error())
  55 + return
54 } else { 56 } else {
55 rsp = m 57 rsp = m
56 } 58 }
57 - err = transactionContext.CommitTransaction() 59 + if err = transactionContext.CommitTransaction(); err != nil {
  60 + err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  61 + return
  62 + }
58 return 63 return
59 } 64 }
60 65
@@ -64,7 +69,7 @@ func (svr *UserService) UpdateUser(header *protocol.RequestHeader, request *prot @@ -64,7 +69,7 @@ func (svr *UserService) UpdateUser(header *protocol.RequestHeader, request *prot
64 ) 69 )
65 rsp = &protocolx.UpdateUserResponse{} 70 rsp = &protocolx.UpdateUserResponse{}
66 if err = request.ValidateCommand(); err != nil { 71 if err = request.ValidateCommand(); err != nil {
67 - err = protocol.NewCustomMessage(2, err.Error()) 72 + err = application.ThrowError(application.ARG_ERROR, err.Error())
68 return 73 return
69 } 74 }
70 if err = transactionContext.StartTransaction(); err != nil { 75 if err = transactionContext.StartTransaction(); err != nil {
@@ -88,12 +93,17 @@ func (svr *UserService) UpdateUser(header *protocol.RequestHeader, request *prot @@ -88,12 +93,17 @@ func (svr *UserService) UpdateUser(header *protocol.RequestHeader, request *prot
88 } 93 }
89 } 94 }
90 if err = user.Update(common.ObjectToMap(request)); err != nil { 95 if err = user.Update(common.ObjectToMap(request)); err != nil {
  96 + err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
91 return 97 return
92 } 98 }
93 if user, err = UserRepository.Save(user); err != nil { 99 if user, err = UserRepository.Save(user); err != nil {
  100 + err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  101 + return
  102 + }
  103 + if err = transactionContext.CommitTransaction(); err != nil {
  104 + err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
94 return 105 return
95 } 106 }
96 - err = transactionContext.CommitTransaction()  
97 return 107 return
98 } 108 }
99 109
@@ -103,12 +113,12 @@ func (svr *UserService) GetUser(header *protocol.RequestHeader, request *protoco @@ -103,12 +113,12 @@ func (svr *UserService) GetUser(header *protocol.RequestHeader, request *protoco
103 ) 113 )
104 rsp = &protocolx.GetUserResponse{} 114 rsp = &protocolx.GetUserResponse{}
105 if err = request.ValidateCommand(); err != nil { 115 if err = request.ValidateCommand(); err != nil {
106 - err = protocol.NewCustomMessage(2, err.Error()) 116 + err = application.ThrowError(application.ARG_ERROR, err.Error())
107 return 117 return
108 } 118 }
109 if err = transactionContext.StartTransaction(); err != nil { 119 if err = transactionContext.StartTransaction(); err != nil {
110 - log.Error(err)  
111 - return nil, err 120 + err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  121 + return
112 } 122 }
113 defer func() { 123 defer func() {
114 transactionContext.RollbackTransaction() 124 transactionContext.RollbackTransaction()
@@ -117,10 +127,14 @@ func (svr *UserService) GetUser(header *protocol.RequestHeader, request *protoco @@ -117,10 +127,14 @@ func (svr *UserService) GetUser(header *protocol.RequestHeader, request *protoco
117 var UserRepository, _ = factory.CreateUserRepository(transactionContext) 127 var UserRepository, _ = factory.CreateUserRepository(transactionContext)
118 var user *domain.User 128 var user *domain.User
119 if user, err = UserRepository.FindOne(common.ObjectToMap(request)); err != nil { 129 if user, err = UserRepository.FindOne(common.ObjectToMap(request)); err != nil {
  130 + err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
120 return 131 return
121 } 132 }
122 rsp = user 133 rsp = user
123 - err = transactionContext.CommitTransaction() 134 + if err = transactionContext.CommitTransaction(); err != nil {
  135 + err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  136 + return
  137 + }
124 return 138 return
125 } 139 }
126 140
@@ -130,12 +144,12 @@ func (svr *UserService) DeleteUser(header *protocol.RequestHeader, request *prot @@ -130,12 +144,12 @@ func (svr *UserService) DeleteUser(header *protocol.RequestHeader, request *prot
130 ) 144 )
131 rsp = &protocolx.DeleteUserResponse{} 145 rsp = &protocolx.DeleteUserResponse{}
132 if err = request.ValidateCommand(); err != nil { 146 if err = request.ValidateCommand(); err != nil {
133 - err = protocol.NewCustomMessage(2, err.Error()) 147 + err = application.ThrowError(application.ARG_ERROR, err.Error())
134 return 148 return
135 } 149 }
136 if err = transactionContext.StartTransaction(); err != nil { 150 if err = transactionContext.StartTransaction(); err != nil {
137 - log.Error(err)  
138 - return nil, err 151 + err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  152 + return
139 } 153 }
140 defer func() { 154 defer func() {
141 transactionContext.RollbackTransaction() 155 transactionContext.RollbackTransaction()
@@ -144,13 +158,18 @@ func (svr *UserService) DeleteUser(header *protocol.RequestHeader, request *prot @@ -144,13 +158,18 @@ func (svr *UserService) DeleteUser(header *protocol.RequestHeader, request *prot
144 var UserRepository, _ = factory.CreateUserRepository(transactionContext) 158 var UserRepository, _ = factory.CreateUserRepository(transactionContext)
145 var user *domain.User 159 var user *domain.User
146 if user, err = UserRepository.FindOne(common.ObjectToMap(request)); err != nil { 160 if user, err = UserRepository.FindOne(common.ObjectToMap(request)); err != nil {
  161 + err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
147 return 162 return
148 } 163 }
149 if user, err = UserRepository.Remove(user); err != nil { 164 if user, err = UserRepository.Remove(user); err != nil {
  165 + err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
150 return 166 return
151 } 167 }
152 rsp = user 168 rsp = user
153 - err = transactionContext.CommitTransaction() 169 + if err = transactionContext.CommitTransaction(); err != nil {
  170 + err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  171 + return
  172 + }
154 return 173 return
155 } 174 }
156 175
@@ -160,7 +179,8 @@ func (svr *UserService) ListUser(header *protocol.RequestHeader, request *protoc @@ -160,7 +179,8 @@ func (svr *UserService) ListUser(header *protocol.RequestHeader, request *protoc
160 ) 179 )
161 rsp = &protocolx.ListUserResponse{} 180 rsp = &protocolx.ListUserResponse{}
162 if err = request.ValidateCommand(); err != nil { 181 if err = request.ValidateCommand(); err != nil {
163 - err = protocol.NewCustomMessage(2, err.Error()) 182 + err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  183 + return
164 } 184 }
165 if err = transactionContext.StartTransaction(); err != nil { 185 if err = transactionContext.StartTransaction(); err != nil {
166 log.Error(err) 186 log.Error(err)
@@ -174,13 +194,17 @@ func (svr *UserService) ListUser(header *protocol.RequestHeader, request *protoc @@ -174,13 +194,17 @@ func (svr *UserService) ListUser(header *protocol.RequestHeader, request *protoc
174 var user []*domain.User 194 var user []*domain.User
175 var total int64 195 var total int64
176 if total, user, err = UserRepository.Find(common.ObjectToMap(request)); err != nil { 196 if total, user, err = UserRepository.Find(common.ObjectToMap(request)); err != nil {
  197 + err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
177 return 198 return
178 } 199 }
179 rsp = map[string]interface{}{ 200 rsp = map[string]interface{}{
180 "total": total, 201 "total": total,
181 "list": user, 202 "list": user,
182 } 203 }
183 - err = transactionContext.CommitTransaction() 204 + if err = transactionContext.CommitTransaction(); err != nil {
  205 + err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  206 + return
  207 + }
184 return 208 return
185 } 209 }
186 210
@@ -2,6 +2,7 @@ package protocol @@ -2,6 +2,7 @@ package protocol
2 2
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
  5 + "github.com/linmadan/egglib-go/core/application"
5 ) 6 )
6 7
7 //CustomErrParse 解析自定义错误结构体 8 //CustomErrParse 解析自定义错误结构体
@@ -72,6 +73,13 @@ func NewResponseMessageData(data interface{}, err error) *ResponseMessage { @@ -72,6 +73,13 @@ func NewResponseMessageData(data interface{}, err error) *ResponseMessage {
72 msg.Data = data 73 msg.Data = data
73 return msg 74 return msg
74 } 75 }
  76 + if v, ok := err.(*application.ServiceError); ok {
  77 + msg = &ResponseMessage{
  78 + ErrorCode{v.Code, v.Message},
  79 + data,
  80 + }
  81 + return msg
  82 + }
75 return NewMesage(1) 83 return NewMesage(1)
76 } 84 }
77 85