Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit b9c9209

Browse files
author
Bjørn
authored
Never exit due to HTTP request error (#37)
This change removes special handling of collection errors and thus eliminating exits due to those. In case of any error due to collection we log the details and move on to the next available organization. Closes #36
1 parent 6e52a3d commit b9c9209

File tree

2 files changed

+6
-116
lines changed

2 files changed

+6
-116
lines changed

main.go

+6-39
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package main
22

33
import (
44
"fmt"
5-
"io"
65
"net/http"
7-
"net/url"
86
"os"
97
"os/signal"
108
"strconv"
@@ -146,18 +144,13 @@ func runAPIPolling(done chan error, url, token string, organizationIDs []string,
146144
var gaugeResults []gaugeResult
147145
for _, organization := range organizations {
148146
log.Debugf("Collecting for organization '%s'", organization.Name)
149-
var results []gaugeResult
150-
err := poll(organization, func(organization org) error {
151-
var err error
152-
results, err = collect(&client, organization)
153-
if err != nil {
154-
return err
155-
}
156-
return nil
157-
})
147+
results, err := collect(&client, organization)
158148
if err != nil {
159-
done <- errors.WithMessagef(err, "organization %s (%s)", organization.Name, organization.ID)
160-
return
149+
log.With("error", errors.Cause(err)).
150+
With("organzationName", organization.Name).
151+
With("organzationId", organization.ID).
152+
Errorf("Collection failed for organization '%s': %v", organization.Name, err)
153+
continue
161154
}
162155
gaugeResults = append(gaugeResults, results...)
163156
}
@@ -171,32 +164,6 @@ func runAPIPolling(done chan error, url, token string, organizationIDs []string,
171164
}
172165
}
173166

174-
// poll polles the collector for new data. In case of errors it decides whether
175-
// to keep on polling or stop b y returning an error.
176-
func poll(organization org, collector func(org) error) error {
177-
err := collector(organization)
178-
if err != nil {
179-
err = errors.Cause(err)
180-
httpErr, ok := err.(*url.Error)
181-
if ok {
182-
if httpErr.Timeout() {
183-
log.Errorf("Collection failed for organization '%s' due timeout", organization.Name)
184-
return nil
185-
}
186-
if httpErr.Err == io.ErrUnexpectedEOF {
187-
log.Errorf("Collection failed for organization '%s' due to unexpected EOF", organization.Name)
188-
return nil
189-
}
190-
}
191-
if err == io.ErrUnexpectedEOF {
192-
log.Errorf("Collection failed for organization '%s' due to unexpected EOF", organization.Name)
193-
return nil
194-
}
195-
return err
196-
}
197-
return nil
198-
}
199-
200167
func organizationNames(orgs []org) []string {
201168
var names []string
202169
for _, org := range orgs {

main_test.go

-77
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package main
22

33
import (
4-
"errors"
5-
"io"
64
"net/http"
75
"net/http/httptest"
8-
"net/url"
96
"reflect"
107
"sort"
118
"testing"
@@ -184,77 +181,3 @@ func TestRunAPIPolling_issuesTimeout(t *testing.T) {
184181
// success path if timeout errors are suppressed
185182
}
186183
}
187-
188-
func TestPoll(t *testing.T) {
189-
tt := []struct {
190-
name string
191-
collectorErr error
192-
output error
193-
}{
194-
{
195-
name: "no error",
196-
collectorErr: nil,
197-
output: nil,
198-
},
199-
{
200-
name: "custom error",
201-
collectorErr: errors.New("custom error"),
202-
output: errors.New("custom error"),
203-
},
204-
{
205-
name: "unexpected EOF",
206-
collectorErr: io.ErrUnexpectedEOF,
207-
output: nil,
208-
},
209-
{
210-
name: "http timeout",
211-
collectorErr: &url.Error{
212-
Op: "POST",
213-
URL: "/url",
214-
Err: &timeoutError{},
215-
},
216-
output: nil,
217-
},
218-
{
219-
name: "http unexpected EOF",
220-
collectorErr: &url.Error{
221-
Op: "POST",
222-
URL: "/url",
223-
Err: io.ErrUnexpectedEOF,
224-
},
225-
output: nil,
226-
},
227-
}
228-
for _, tc := range tt {
229-
t.Run(tc.name, func(t *testing.T) {
230-
o := org{
231-
ID: tc.name,
232-
}
233-
err := poll(o, func(org) error {
234-
return tc.collectorErr
235-
})
236-
if tc.output != nil {
237-
if err == nil {
238-
t.Fatalf("expected output error but got nil")
239-
}
240-
if tc.output.Error() != err.Error() {
241-
t.Fatalf("expected error '%s' but got '%s'", tc.output.Error(), err.Error())
242-
}
243-
return
244-
}
245-
if err != nil {
246-
t.Fatalf("unexpected output error '%s'", err)
247-
}
248-
})
249-
}
250-
}
251-
252-
type timeoutError struct{}
253-
254-
func (err *timeoutError) Timeout() bool {
255-
return true
256-
}
257-
258-
func (err *timeoutError) Error() string {
259-
return "timeout error"
260-
}

0 commit comments

Comments
 (0)