From 1382e5700355bf48150b447508bb23624aeaeee4 Mon Sep 17 00:00:00 2001 From: Sinh Date: Wed, 24 Aug 2022 14:17:45 +0700 Subject: [PATCH 1/2] define tnc error code --- partnerapi/tnc/const.go | 4 ++++ partnerapi/tnc/error.go | 22 ++++++++++++++++++++++ partnerapi/tnc/model_response.go | 14 ++++---------- partnerapi/tnc/tnc.go | 22 +++++++++++----------- 4 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 partnerapi/tnc/error.go diff --git a/partnerapi/tnc/const.go b/partnerapi/tnc/const.go index fd3bbf6..fd6a860 100644 --- a/partnerapi/tnc/const.go +++ b/partnerapi/tnc/const.go @@ -37,6 +37,10 @@ const ( baseURLProd = "https://ext-api.tnclog.vn" ) +const ( + ErrCodeExistPartnerCode = "exist_partner_code" +) + var ( baseURLENVMapping = map[ENV]string{ EnvProd: baseURLProd, diff --git a/partnerapi/tnc/error.go b/partnerapi/tnc/error.go new file mode 100644 index 0000000..ec41b58 --- /dev/null +++ b/partnerapi/tnc/error.go @@ -0,0 +1,22 @@ +package tnc + +import ( + "fmt" +) + +// Error ... +type Error struct { + Code string `json:"code"` + Message string `json:"errorMessage"` +} + +// Error ... +func (e Error) Error() string { + return fmt.Sprintf("tnc_err: code %s, messsage %s", e.Code, e.Message) +} + +// IsErrExistPartnerCode ... +func IsErrExistPartnerCode(err error) bool { + e, ok := err.(Error) + return ok && e.Code == ErrCodeExistPartnerCode +} diff --git a/partnerapi/tnc/model_response.go b/partnerapi/tnc/model_response.go index a13dec4..5d3843d 100644 --- a/partnerapi/tnc/model_response.go +++ b/partnerapi/tnc/model_response.go @@ -2,16 +2,10 @@ package tnc // OutboundRequestRes ... type OutboundRequestRes struct { - OrID int `json:"orId"` - OrCode string `json:"orCode"` - PartnerORCode string `json:"partnerORCode"` - Error *ErrRes `json:"error"` -} - -// ErrRes ... -type ErrRes struct { - Code string `json:"code"` - ErrorMessage string `json:"errorMessage"` + OrID int `json:"orId"` + OrCode string `json:"orCode"` + PartnerORCode string `json:"partnerORCode"` + Error *Error `json:"error"` } type authRes struct { diff --git a/partnerapi/tnc/tnc.go b/partnerapi/tnc/tnc.go index a9a5971..17e83fd 100644 --- a/partnerapi/tnc/tnc.go +++ b/partnerapi/tnc/tnc.go @@ -66,7 +66,7 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque } var ( r model.CommunicationHttpResponse - errRes ErrRes + errRes Error dataRes []OutboundRequestRes ) if err = pjson.Unmarshal(msg.Data, &r); err != nil { @@ -80,7 +80,7 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque if err = r.ParseResponseData(&errRes); err != nil { return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: parse_response_err: %v", err) } - return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) + return nil, errRes } if err = r.ParseResponseData(&dataRes); err != nil { return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: parse_response_data: %v", err) @@ -91,7 +91,7 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque item := &dataRes[0] e := item.Error if e != nil { - return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: failed, code %s - message %s", e.Code, e.ErrorMessage) + return nil, errRes } return item, err @@ -119,7 +119,7 @@ func (c *Client) UpdateOutboundRequestLogisticInfo(p UpdateORLogisticInfoPayload } var ( r model.CommunicationHttpResponse - errRes ErrRes + errRes Error ) if err = pjson.Unmarshal(msg.Data, &r); err != nil { return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: parse_data %v", err) @@ -132,7 +132,7 @@ func (c *Client) UpdateOutboundRequestLogisticInfo(p UpdateORLogisticInfoPayload if err = r.ParseResponseData(&errRes); err != nil { return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: parse_response_err: %v", err) } - return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) + return errRes } return nil } @@ -158,7 +158,7 @@ func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, er } var ( r model.CommunicationHttpResponse - errRes ErrRes + errRes Error outboundRequest OutboundRequestInfo ) if err = pjson.Unmarshal(msg.Data, &r); err != nil { @@ -172,7 +172,7 @@ func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, er if err = r.ParseResponseData(&errRes); err != nil { return nil, fmt.Errorf("tnc.Client.GetOutboundRequestByID: parse_response_err: %v", err) } - return nil, fmt.Errorf("tnc.Client.GetOutboundRequestByID: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) + return nil, errRes } if err = r.ParseResponseData(&outboundRequest); err != nil { return nil, fmt.Errorf("tnc.Client.GetOutboundRequestByID: parse_response_data: %v", err) @@ -203,7 +203,7 @@ func (c *Client) CancelOutboundRequest(requestID int, note string) error { } var ( r model.CommunicationHttpResponse - errRes ErrRes + errRes Error ) if err = pjson.Unmarshal(msg.Data, &r); err != nil { return fmt.Errorf("tnc.Client.CancelOutboundRequest: parse_data %v", err) @@ -216,7 +216,7 @@ func (c *Client) CancelOutboundRequest(requestID int, note string) error { if err = r.ParseResponseData(&errRes); err != nil { return fmt.Errorf("tnc.Client.CancelOutboundRequest: parse_response_err: %v", err) } - return fmt.Errorf("tnc.Client.CancelOutboundRequest: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) + return errRes } return nil } @@ -252,7 +252,7 @@ func (c *Client) auth() (*authRes, error) { } var ( r model.CommunicationHttpResponse - errRes ErrRes + errRes Error data authRes ) if err = pjson.Unmarshal(msg.Data, &r); err != nil { @@ -266,7 +266,7 @@ func (c *Client) auth() (*authRes, error) { if err = r.ParseResponseData(&errRes); err != nil { return nil, fmt.Errorf("tnc.Client.auth: parse_response_err: %v", err) } - return nil, fmt.Errorf("tnc.Client.auth: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) + return nil, errRes } if err = r.ParseResponseData(&data); err != nil { return nil, fmt.Errorf("tnc.Client.auth: parse_response_data: %v", err) From 4f0da1a456666fd7a4cfc02bc7158175d490e174 Mon Sep 17 00:00:00 2001 From: Sinh Date: Sun, 28 Aug 2022 14:18:46 +0700 Subject: [PATCH 2/2] update nats module --- constant/natsio.go | 9 --------- go.mod | 7 +++---- go.sum | 7 ++----- partnerapi/globalcare/globale_care.go | 6 +++--- partnerapi/tnc/tnc.go | 5 ++--- 5 files changed, 10 insertions(+), 24 deletions(-) delete mode 100644 constant/natsio.go diff --git a/constant/natsio.go b/constant/natsio.go deleted file mode 100644 index d730e74..0000000 --- a/constant/natsio.go +++ /dev/null @@ -1,9 +0,0 @@ -package constant - -const natsCommunicationPrefix = "communication_" - -const ( - NatsCommunicationSubjectRequestHTTP = natsCommunicationPrefix + "request_http" - NatsCommunicationSubjectResponseHTTP = natsCommunicationPrefix + "response_http" - NatsCommunicationSubjectWebhookTNC = natsCommunicationPrefix + "webhook_tnc" -) diff --git a/go.mod b/go.mod index aed1d01..f602b02 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,9 @@ go 1.17 require ( github.com/Selly-Modules/logger v0.0.1 - github.com/Selly-Modules/natsio v1.0.1-0.20220818104747-572120c26a1d + github.com/Selly-Modules/natsio v1.0.1 + github.com/nats-io/nats.go v1.13.0 + github.com/thoas/go-funk v0.9.1 ) require ( @@ -14,14 +16,11 @@ require ( github.com/elastic/go-windows v1.0.0 // indirect github.com/jcchavezs/porto v0.1.0 // indirect github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect - github.com/logrusorgru/aurora v2.0.3+incompatible // indirect - github.com/nats-io/nats.go v1.13.0 // indirect github.com/nats-io/nkeys v0.3.0 // indirect github.com/nats-io/nuid v1.0.1 // indirect github.com/pkg/errors v0.8.1 // indirect github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0 // indirect github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect - github.com/thoas/go-funk v0.9.1 // indirect go.elastic.co/apm v1.13.1 // indirect go.elastic.co/apm/module/apmzap v1.13.1 // indirect go.elastic.co/fastjson v1.1.0 // indirect diff --git a/go.sum b/go.sum index bf59b4e..f9468d2 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,7 @@ github.com/Selly-Modules/logger v0.0.1 h1:dwLLtW53FfVBlklhdtFRB63eP0ofIh0IUQ/Gjgi0r5k= github.com/Selly-Modules/logger v0.0.1/go.mod h1:RWhSQ3F01an8KD00VjzRBZOMcE5eV2Cy0/l4ZkeieyU= -github.com/Selly-Modules/natsio v1.0.0 h1:2GHm7upYn0CmqKPwfg2AXNmaAHuL7V9l36M4BgOmQ4M= -github.com/Selly-Modules/natsio v1.0.0/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ= -github.com/Selly-Modules/natsio v1.0.1-0.20220818104747-572120c26a1d h1:NnRejDZPFqvtphr2rtfwi0rGx+oN8yD8RwttViVddjY= -github.com/Selly-Modules/natsio v1.0.1-0.20220818104747-572120c26a1d/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ= +github.com/Selly-Modules/natsio v1.0.1 h1:0HoP00hP0Fz5h03p23Al3aARZwg+JSKpOrBo5Oh7pwY= +github.com/Selly-Modules/natsio v1.0.1/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ= github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= @@ -42,7 +40,6 @@ github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= diff --git a/partnerapi/globalcare/globale_care.go b/partnerapi/globalcare/globale_care.go index 56ed5fd..8d53362 100644 --- a/partnerapi/globalcare/globale_care.go +++ b/partnerapi/globalcare/globale_care.go @@ -8,10 +8,10 @@ import ( "github.com/Selly-Modules/logger" "github.com/Selly-Modules/natsio" "github.com/Selly-Modules/natsio/model" + "github.com/Selly-Modules/natsio/subject" "github.com/nats-io/nats.go" "github.com/thoas/go-funk" - "github.com/Selly-Modules/3pl/constant" "github.com/Selly-Modules/3pl/util/base64" "github.com/Selly-Modules/3pl/util/pjson" ) @@ -70,7 +70,7 @@ func (c *Client) CreateOrder(p CreateOrderPayload) (*CommonResponse, error) { Data: pjson.ToJSONString(body), }, } - msg, err := c.requestNats(constant.NatsCommunicationSubjectRequestHTTP, natsPayload) + msg, err := c.requestNats(subject.Communication.RequestHTTP, natsPayload) if err != nil { logger.Error("globalcare.Client.CreateOrder", logger.LogData{ "err": err.Error(), @@ -99,7 +99,7 @@ func (c *Client) GetOrder(orderCode string) (*CommonResponse, error) { Method: http.MethodGet, }, } - msg, err := c.requestNats(constant.NatsCommunicationSubjectRequestHTTP, natsPayload) + msg, err := c.requestNats(subject.Communication.RequestHTTP, natsPayload) if err != nil { logger.Error("globalcare.Client.GetOrder", logger.LogData{ "err": err.Error(), diff --git a/partnerapi/tnc/tnc.go b/partnerapi/tnc/tnc.go index 17e83fd..de2c7e0 100644 --- a/partnerapi/tnc/tnc.go +++ b/partnerapi/tnc/tnc.go @@ -9,9 +9,9 @@ import ( "github.com/Selly-Modules/logger" "github.com/Selly-Modules/natsio" "github.com/Selly-Modules/natsio/model" + "github.com/Selly-Modules/natsio/subject" "github.com/nats-io/nats.go" - "github.com/Selly-Modules/3pl/constant" "github.com/Selly-Modules/3pl/util/pjson" ) @@ -288,9 +288,8 @@ func (c *Client) getRequestHeader() map[string]string { } func (c *Client) requestHttpViaNats(data model.CommunicationRequestHttp) (*nats.Msg, error) { - s := constant.NatsCommunicationSubjectRequestHTTP b := pjson.ToBytes(data) - return c.natsClient.Request(s, b) + return c.natsClient.Request(subject.Communication.RequestHTTP, b) } func (c *Client) getBaseURL() string {