Skip to content

Commit

Permalink
Improved error handling of connection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
koen-serry committed Jan 2, 2024
1 parent 4ccfe4e commit ff7e2dd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
15 changes: 12 additions & 3 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,10 @@ func (c *Client) DocumentFeed(
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", c.UserAgent)

res, _ := c.HTTPClient.Do(req)
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode == 200 {
payload, _ := ioutil.ReadAll(res.Body)
var updates MandateUpdates
Expand Down Expand Up @@ -365,7 +368,10 @@ func (c *Client) DownloadPdf(ctx context.Context, mndtId string, downloadFile st
req.Header.Add("Authorization", c.apiToken)

absPath, _ := filepath.Abs(downloadFile)
res, _ := c.HTTPClient.Do(req)
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode == 200 {
payload, _ := ioutil.ReadAll(res.Body)

Expand Down Expand Up @@ -403,7 +409,10 @@ func (c *Client) DocumentDetail(ctx context.Context, mndtId string, force bool)
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Add("Authorization", c.apiToken)

res, _ := c.HTTPClient.Do(req)
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode == 200 {
payload, _ := ioutil.ReadAll(res.Body)

Expand Down
20 changes: 16 additions & 4 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ func (c *Client) InvoiceAdd(ctx context.Context, invoiceRequest *NewInvoiceReque
}
}

res, _ := c.HTTPClient.Do(req)
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode == 200 {
payload, _ := ioutil.ReadAll(res.Body)
c.Debug.Println("TwikeyInvoice: ", string(payload))
Expand Down Expand Up @@ -280,7 +283,10 @@ func (c *Client) InvoiceDetail(ctx context.Context, invoiceIdOrNumber string, si
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Add("Authorization", c.apiToken)

res, _ := c.HTTPClient.Do(req)
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode == 200 {
payload, _ := ioutil.ReadAll(res.Body)

Expand Down Expand Up @@ -327,7 +333,10 @@ func (c *Client) InvoiceAction(ctx context.Context, invoiceIdOrNumber string, ac
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Add("Authorization", c.apiToken)

res, _ := c.HTTPClient.Do(req)
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode == 204 {
return nil
}
Expand All @@ -354,7 +363,10 @@ func (c *Client) InvoicePayment(ctx context.Context, invoiceIdOrNumber string, m
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Add("Authorization", c.apiToken)

res, _ := c.HTTPClient.Do(req)
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode == 204 {
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions paylink.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ func (c *Client) PaylinkFeed(ctx context.Context, callback func(paylink *Paylink
req.Header.Add("Authorization", c.apiToken)
req.Header.Set("User-Agent", c.UserAgent)

res, _ := c.HTTPClient.Do(req)

res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode == 200 {
payload, _ := ioutil.ReadAll(res.Body)
_ = res.Body.Close()
Expand Down
6 changes: 4 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ func (c *Client) logout() {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", c.apiToken)

res, _ := c.HTTPClient.Do(req)
if res.StatusCode != 200 {
res, err := c.HTTPClient.Do(req)
if err != nil {
c.Debug.Println("Error in logout from Twikey:", err)
} else if res.StatusCode != 200 {
c.Debug.Println("Error in logout from Twikey:", res.StatusCode)
}
}
12 changes: 8 additions & 4 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ func (c *Client) TransactionFeed(ctx context.Context, callback func(transaction
req.Header.Add("Authorization", c.apiToken)
req.Header.Set("User-Agent", c.UserAgent)

res, _ := c.HTTPClient.Do(req)

res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode == 200 {
payload, _ := ioutil.ReadAll(res.Body)
_ = res.Body.Close()
Expand Down Expand Up @@ -193,8 +195,10 @@ func (c *Client) TransactionCollect(ctx context.Context, template string, prenot
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", c.apiToken)
req.Header.Set("User-Agent", c.UserAgent)
res, _ := c.HTTPClient.Do(req)

res, err := c.HTTPClient.Do(req)
if err != nil {
return "", err
}
if res.StatusCode == 200 {
payload, _ := ioutil.ReadAll(res.Body)
_ = res.Body.Close()
Expand Down

0 comments on commit ff7e2dd

Please sign in to comment.