@@ -97,66 +97,78 @@ func (c *HttpClientImplementation) DoRequest(req *http.Request, result interface
9797 res , err := c .HttpClient .Do (req )
9898 if err != nil {
9999 c .Logger .Error ("Cannot send request: %v" , err .Error ())
100+ var statusCode int
101+
102+ if res != nil {
103+ statusCode = res .StatusCode
104+ } else if strings .Contains (err .Error (), "timeout" ) {
105+ statusCode = 408
106+ } else {
107+ statusCode = 0
108+ }
109+
100110 return & Error {
101111 Message : fmt .Sprintf ("Error when request via HttpClient, Cannot send request with error: %s" , err .Error ()),
102- StatusCode : res . StatusCode ,
112+ StatusCode : statusCode ,
103113 RawError : err ,
104114 }
105115 }
106116
107- defer res .Body .Close ()
117+ if res != nil {
118+ defer res .Body .Close ()
108119
109- c .Logger .Info ("================== END ==================" )
110- c .Logger .Info ("Request completed in %v " , time .Since (start ))
120+ c .Logger .Info ("================== END ==================" )
121+ c .Logger .Info ("Request completed in %v " , time .Since (start ))
111122
112- resBody , err := ioutil .ReadAll (res .Body )
113- if err != nil {
114- c .Logger .Error ("Request failed: %v" , err )
115- return & Error {
116- Message : "Cannot read response body: " + err .Error (),
117- StatusCode : res .StatusCode ,
123+ resBody , err := ioutil .ReadAll (res .Body )
124+ if err != nil {
125+ c .Logger .Error ("Request failed: %v" , err )
126+ return & Error {
127+ Message : "Cannot read response body: " + err .Error (),
128+ StatusCode : res .StatusCode ,
129+ RawError : err ,
130+ }
118131 }
119- }
120132
121- rawResponse := newHTTPResponse (res , resBody )
122- c .Logger .Debug ("=============== Response ===============" )
123- // Loop through headers to perform log
124- logHttpHeaders (c .Logger , rawResponse .Header , false )
125- c .Logger .Debug ("Response Body: %v" , string (rawResponse .RawBody ))
133+ rawResponse := newHTTPResponse (res , resBody )
134+ c .Logger .Debug ("=============== Response ===============" )
135+ // Loop through headers to perform log
136+ logHttpHeaders (c .Logger , rawResponse .Header , false )
137+ c .Logger .Debug ("Response Body: %v" , string (rawResponse .RawBody ))
138+
139+ if result != nil {
140+ if err = json .Unmarshal (resBody , & result ); err != nil {
141+ return & Error {
142+ Message : fmt .Sprintf ("Invalid body response, parse error during API request to Midtrans with message: %s" , err .Error ()),
143+ StatusCode : res .StatusCode ,
144+ RawError : err ,
145+ RawApiResponse : rawResponse ,
146+ }
147+ }
148+ }
126149
127- if result != nil {
128- if err = json .Unmarshal (resBody , & result ); err != nil {
129- return & Error {
130- Message : fmt .Sprintf ("Invalid body response, parse error during API request to Midtrans with message: %s" , err .Error ()),
131- StatusCode : res .StatusCode ,
132- RawError : err ,
133- RawApiResponse : rawResponse ,
150+ // Check status_code from Midtrans response body
151+ if found , data := HasOwnProperty ("status_code" , resBody ); found {
152+ statusCode , _ := strconv .Atoi (data ["status_code" ].(string ))
153+ if statusCode >= 401 && statusCode != 407 {
154+ return & Error {
155+ Message : fmt .Sprintf ("Midtrans API is returning API error. HTTP status code: %s API response: %s" , strconv .Itoa (statusCode ), string (resBody )),
156+ StatusCode : statusCode ,
157+ RawApiResponse : rawResponse ,
158+ }
134159 }
135160 }
136- }
137161
138- // Check status_code from Midtrans response body
139- if found , data := HasOwnProperty ("status_code" , resBody ); found {
140- statusCode , _ := strconv .Atoi (data ["status_code" ].(string ))
141- if statusCode >= 401 && statusCode != 407 {
162+ // Check StatusCode from Midtrans HTTP response api StatusCode
163+ if res .StatusCode >= 400 {
142164 return & Error {
143- Message : fmt .Sprintf ("Midtrans API is returning API error. HTTP status code: %s API response: %s" , strconv .Itoa (statusCode ), string (resBody )),
144- StatusCode : statusCode ,
165+ Message : fmt .Sprintf ("Midtrans API is returning API error. HTTP status code: %s API response: %s" , strconv .Itoa (res . StatusCode ), string (resBody )),
166+ StatusCode : res . StatusCode ,
145167 RawApiResponse : rawResponse ,
168+ RawError : err ,
146169 }
147170 }
148171 }
149-
150- // Check StatusCode from Midtrans HTTP response api StatusCode
151- if res .StatusCode >= 400 {
152- return & Error {
153- Message : fmt .Sprintf ("Midtrans API is returning API error. HTTP status code: %s API response: %s" , strconv .Itoa (res .StatusCode ), string (resBody )),
154- StatusCode : res .StatusCode ,
155- RawApiResponse : rawResponse ,
156- RawError : err ,
157- }
158- }
159-
160172 return nil
161173}
162174
0 commit comments