Skip to content

Commit

Permalink
MINOR: Add error checks where missing (#64)
Browse files Browse the repository at this point in the history
Some function calls were lacking error checking. This properly checks
these errors.
  • Loading branch information
ShimmerGlass authored Aug 24, 2020
1 parent 86295eb commit 83f8b09
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 16 deletions.
7 changes: 6 additions & 1 deletion haproxy/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ func (h *haConfig) FilePath(content []byte) (string, error) {
if err != nil {
return "", err
}
defer f.Close()
defer func() {
err := f.Close()
if err != nil {
log.Errorf("error closing file %s: %s", path, err)
}
}()

_, err = f.Write(content)
if err != nil {
Expand Down
14 changes: 12 additions & 2 deletions haproxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ func newHaConfig(baseDir string, sd *lib.Shutdown) (*haConfig, error) {
if err != nil {
return nil, err
}
defer cfgFile.Close()
defer func() {
err := cfgFile.Close()
if err != nil {
log.Errorf("error closing config file %s: %s", cfg.HAProxy, err)
}
}()

dataplanePass = createRandomString()

Expand All @@ -130,7 +135,12 @@ func newHaConfig(baseDir string, sd *lib.Shutdown) (*haConfig, error) {
sd.Done()
return nil, err
}
defer spoeCfgFile.Close()
defer func() {
err := spoeCfgFile.Close()
if err != nil {
log.Errorf("error closing spoe config file %s: %s", cfg.SPOE, err)
}
}()
_, err = spoeCfgFile.WriteString(spoeConfTmpl)
if err != nil {
sd.Done()
Expand Down
7 changes: 6 additions & 1 deletion haproxy/dataplane/dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ func (c *Dataplane) makeReq(method, url string, reqData, resData interface{}) er
if err != nil {
return errors.Wrapf(err, "error calling %s %s", method, url)
}
defer res.Body.Close()
defer func() {
err := res.Body.Close()
if err != nil {
log.Errorf("error calling %s %s: %s", method, url, err)
}
}()

if res.StatusCode >= http.StatusBadRequest {
body, _ := ioutil.ReadAll(res.Body)
Expand Down
15 changes: 12 additions & 3 deletions haproxy/haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,14 @@ func (h *HAProxy) startLogger() error {
server := syslog.NewServer()
server.SetFormat(syslog.RFC5424)
server.SetHandler(handler)
server.ListenUnixgram(h.haConfig.LogsSock)
server.Boot()
err := server.ListenUnixgram(h.haConfig.LogsSock)
if err != nil {
return fmt.Errorf("error starting syslog logger: %s", err)
}
err = server.Boot()
if err != nil {
return fmt.Errorf("error starting syslog logger: %s", err)
}

go func(channel syslog.LogPartsChannel) {
for logParts := range channel {
Expand Down Expand Up @@ -183,7 +189,10 @@ func (h *HAProxy) startStats() error {
http.Handle("/metrics", promhttp.Handler())

log.Infof("Starting stats server at %s", h.opts.StatsListenAddr)
http.ListenAndServe(h.opts.StatsListenAddr, nil)
err := http.ListenAndServe(h.opts.StatsListenAddr, nil)
if err != nil {
log.Errorf("error starting stats server: %s", err)
}
}()

return nil
Expand Down
12 changes: 9 additions & 3 deletions haproxy/haproxy_cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ import (
func runCommand(sd *lib.Shutdown, cmdPath string, args ...string) (*exec.Cmd, error) {
_, file := path.Split(cmdPath)
cmd := exec.Command(cmdPath, args...)
halog.Cmd("haproxy", cmd)
err := halog.Cmd("haproxy", cmd)
if err != nil {
return nil, err
}

sd.Add(1)
err := cmd.Start()
err = cmd.Start()
if err != nil {
sd.Done()
return nil, errors.Wrapf(err, "error starting %s", file)
Expand All @@ -46,7 +49,10 @@ func runCommand(sd *lib.Shutdown, cmdPath string, args ...string) (*exec.Cmd, er
return
}
log.Infof("killing %s with sig %d", file, syscall.SIGTERM)
syscall.Kill(cmd.Process.Pid, syscall.SIGTERM)
err := syscall.Kill(cmd.Process.Pid, syscall.SIGTERM)
if err != nil {
log.Errorf("could not kill %s: %s", file, err)
}
}()

return cmd, nil
Expand Down
3 changes: 2 additions & 1 deletion haproxy/haproxy_cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func Test_runCommand_ok(t *testing.T) {
sd := lib.NewShutdown()
cmd, err := runCommand(sd, "ls", ".")
require.NoError(t, err)
cmd.Wait()
err = cmd.Wait()
require.NoError(t, err)
}

func Test_runCommand_nok_wrong_path(t *testing.T) {
Expand Down
3 changes: 0 additions & 3 deletions haproxy/haproxy_cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) {
if err != nil {
return nil, err
}
if haCmd.Process == nil {
return nil, fmt.Errorf("%s was not started", cfg.HAProxyPath)
}

cmd, err := runCommand(sd,
cfg.DataplanePath,
Expand Down
6 changes: 4 additions & 2 deletions haproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func TestService(t *testing.T) {

body, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
res.Body.Close()
err = res.Body.Close()
require.NoError(t, err)
require.Equal(t, "hello connect", string(body))
}

Expand Down Expand Up @@ -131,6 +132,7 @@ func TestPreparedQuery(t *testing.T) {

body, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
res.Body.Close()
err = res.Body.Close()
require.NoError(t, err)
require.Equal(t, "hello connect prepared query", string(body))
}

0 comments on commit 83f8b09

Please sign in to comment.