Improve error handling and logging in HTTP request execution
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 26s
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 26s
This commit is contained in:
22
client.go
22
client.go
@ -204,29 +204,45 @@ func (c *Client) IsAlive() bool {
|
|||||||
func (c *Client) execute(method string, url string, params map[string]string, j bool) (*Response, error) {
|
func (c *Client) execute(method string, url string, params map[string]string, j bool) (*Response, error) {
|
||||||
// defer timeTrack("[TIMETRK] Executing HTTP Request")
|
// defer timeTrack("[TIMETRK] Executing HTTP Request")
|
||||||
var resp *resty.Response
|
var resp *resty.Response
|
||||||
|
var err error
|
||||||
|
// c.logger.Debug("executing http request", "method", method, "url", url, "params", params)
|
||||||
|
|
||||||
// GET Requests
|
// GET Requests
|
||||||
if method == GET {
|
if method == GET {
|
||||||
resp, _ = c.httpClient.
|
resp, err = c.httpClient.
|
||||||
R().
|
R().
|
||||||
SetQueryParams(params).
|
SetQueryParams(params).
|
||||||
Get(url)
|
Get(url)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Error("error while executing GET request", "request", "execute", "method", method, "url", url, "error", err.Error())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c.logger.Debug("executed GET request", "method", method, "url", url, "params", params)
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST Requests
|
// POST Requests
|
||||||
if method == POST {
|
if method == POST {
|
||||||
if j { // POST > JSON Body
|
if j { // POST > JSON Body
|
||||||
resp, _ = c.httpClient.
|
resp, err = c.httpClient.
|
||||||
R().
|
R().
|
||||||
SetBody(params).
|
SetBody(params).
|
||||||
Post(url)
|
Post(url)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Error("error while executing POST request", "request", "execute", "method", method, "url", url, "error", err.Error())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
} else { // POST > Form Data
|
} else { // POST > Form Data
|
||||||
resp, _ = c.httpClient.
|
resp, err = c.httpClient.
|
||||||
R().
|
R().
|
||||||
SetFormData(params).
|
SetFormData(params).
|
||||||
Post(url)
|
Post(url)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Error("error while executing POST request", "request", "execute", "method", method, "url", url, "error", err.Error())
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.logger.Debug("executed POST request", "method", method, "url", url, "params", params)
|
||||||
|
}
|
||||||
|
|
||||||
if resp.IsSuccess() {
|
if resp.IsSuccess() {
|
||||||
resBytes, err := io.ReadAll(resp.Body)
|
resBytes, err := io.ReadAll(resp.Body)
|
||||||
|
Reference in New Issue
Block a user