Skip to content

Commit db0bd6a

Browse files
committed
improve error handling
1 parent 84c340f commit db0bd6a

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

couchdb-exporter.go

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,10 @@ func init() {
4646
}
4747

4848
func main() {
49-
flag.Parse()
50-
// Convinces goflags that we have called Parse() to avoid noisy logs.
51-
// Necessary due to https://github.com/golang/glog/commit/65d674618f712aa808a7d0104131b9206fc3d5ad
52-
// and us using another flags package.
53-
goflag.CommandLine.Parse([]string{})
54-
goflag.Lookup("logtostderr").Value.Set(strconv.FormatBool(*&glogadapt.Logging.ToStderr))
55-
goflag.Lookup("alsologtostderr").Value.Set(strconv.FormatBool(*&glogadapt.Logging.AlsoToStderr))
56-
goflag.Lookup("v").Value.Set(glogadapt.Logging.Verbosity.String())
57-
goflag.Lookup("stderrthreshold").Value.Set(glogadapt.Logging.StderrThreshold.String())
58-
goflag.Lookup("log_dir").Value.Set(glogadapt.Logging.LogDir)
49+
err := initFlags()
50+
if err != nil {
51+
glog.Fatal(err)
52+
}
5953

6054
var databases []string
6155
if *&exporterConfig.databases != "" {
@@ -76,15 +70,50 @@ func main() {
7670

7771
http.Handle(*&exporterConfig.metricsEndpoint, promhttp.Handler())
7872
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
79-
fmt.Fprint(w, "OK")
73+
_, err = fmt.Fprint(w, "OK")
74+
if err != nil {
75+
glog.Error(err)
76+
}
8077
})
8178
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
8279
http.Error(w, fmt.Sprintf("Please GET %s", *&exporterConfig.metricsEndpoint), http.StatusNotFound)
8380
})
8481

8582
glog.Infof("Starting exporter at '%s' to read from CouchDB at '%s'", *&exporterConfig.listenAddress, *&exporterConfig.couchdbURI)
86-
err := http.ListenAndServe(*&exporterConfig.listenAddress, nil)
83+
err = http.ListenAndServe(*&exporterConfig.listenAddress, nil)
8784
if err != nil {
8885
glog.Fatal(err)
8986
}
9087
}
88+
89+
func initFlags() error {
90+
flag.Parse()
91+
// Convinces goflags that we have called Parse() to avoid noisy logs.
92+
// Necessary due to https://github.com/golang/glog/commit/65d674618f712aa808a7d0104131b9206fc3d5ad
93+
// and us using another flags package.
94+
err := goflag.CommandLine.Parse([]string{})
95+
if err != nil {
96+
return err
97+
}
98+
err = goflag.Lookup("logtostderr").Value.Set(strconv.FormatBool(*&glogadapt.Logging.ToStderr))
99+
if err != nil {
100+
return err
101+
}
102+
err = goflag.Lookup("alsologtostderr").Value.Set(strconv.FormatBool(*&glogadapt.Logging.AlsoToStderr))
103+
if err != nil {
104+
return err
105+
}
106+
err = goflag.Lookup("v").Value.Set(glogadapt.Logging.Verbosity.String())
107+
if err != nil {
108+
return err
109+
}
110+
err = goflag.Lookup("stderrthreshold").Value.Set(glogadapt.Logging.StderrThreshold.String())
111+
if err != nil {
112+
return err
113+
}
114+
err = goflag.Lookup("log_dir").Value.Set(glogadapt.Logging.LogDir)
115+
if err != nil {
116+
return err
117+
}
118+
return nil
119+
}

lib/couchdb-client.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,24 @@ func (c *CouchdbClient) Request(method string, uri string, body io.Reader) (resp
331331
if err != nil {
332332
return nil, err
333333
}
334+
if resp != nil {
335+
defer func() {
336+
if cerr := resp.Body.Close(); cerr != nil {
337+
err = cerr
338+
}
339+
}()
340+
}
334341

335342
respData, err = ioutil.ReadAll(resp.Body)
336-
resp.Body.Close()
337343
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
338344
if err != nil {
339345
respData = []byte(err.Error())
340346
}
341347
return nil, fmt.Errorf("status %s (%d): %s", resp.Status, resp.StatusCode, respData)
342348
}
349+
if err != nil {
350+
return nil, err
351+
}
343352

344353
return respData, nil
345354
}

0 commit comments

Comments
 (0)