HostSeries no longer panics #246

Merged
merged 1 commit into from Oct 14, 2016

Conversation

Projects
None yet
3 participants
Contributor

kat-co commented Oct 14, 2016

This is to help address lp:1633495.

HostSeries unecessarily panics. Any function that panics should be prepended with the keyword "Must". I have introduced a function which does just this in case callers want to continue to panic.

Also removed unecessary global variable referencing an unexported function.

series/series.go
})
+ return series, err
@rogpeppe

rogpeppe Oct 14, 2016

Owner

This doesn't seem quite right. If HostSeries returns an error the first time, it'll return "", nil the second time AFAICS.

@kat-co

kat-co Oct 14, 2016

Contributor

Whoops, good catch! I've taken your suggestion and just set series to UnknownSeries for now.

series/series.go
+ return series, err
+}
+
+// MustHostSeries calls HostSeries and panics if there is an error.
@rogpeppe

rogpeppe Oct 14, 2016

Owner

I wouldn't bother with this function unless you find yourself needing it a lot.

@kat-co

kat-co Oct 14, 2016

Contributor

There are several places in Juju that currently rely on this behavior panicing. I don't plan on tackling that issue other than re-pointing those areas to this function.

series/series.go
+// HostSeries returns the series of the machine the current process is
+// running on.
+//
+// This is not a concurrent-safe operation.
series/series.go
+//
+// This is not a concurrent-safe operation.
+func HostSeries() (string, error) {
+ series, err := readSeries()
@rogpeppe

rogpeppe Oct 14, 2016

Owner

or just:

return readSeries()

the trace doesn't add much IMHO when there's only one error path.

@kat-co

kat-co Oct 14, 2016

Contributor

I prefer the errors.Trace so that it gives the path through the code when the error is output.

series/series.go
-
- seriesOnce sync.Once
- series string // filled in by the first call to hostSeries
+ logger = loggo.GetLogger("juju.juju.series")
@rogpeppe

rogpeppe Oct 14, 2016

Owner

Perhaps move this definition to the only file that uses it (supportedseries.go)
and perhaps add a TODO to remove the globals there?

@kat-co

kat-co Oct 14, 2016

Contributor

Done

seriesOnce.Do(func() {
- var err error
series, err = readSeries()
@rogpeppe

rogpeppe Oct 14, 2016

Owner

I'd suggest you declare err inside the function.

series/supportedseries.go
"github.com/juju/utils/os"
)
+var (
+ // TODO(katco): Remove this global
@rogpeppe

rogpeppe Oct 14, 2016

Owner

bug number? :)

series/series.go
seriesOnce sync.Once
- series string // filled in by the first call to hostSeries
+ // These are filled in by the first call to hostSeries
+ series string
@rogpeppe

rogpeppe Oct 14, 2016

Owner

// TODO ...

?

LGTM modulo a few minor suggestions, thanks!

HostSeries no longer panics
This is to help address lp:1633495.

HostSeries unecessarily panics. Any function that panics should be prepended with the keyword "Must". I have introduced a function which does just this in case callers want to continue to panic.

Also removed unecessary global variable referencing an unexported function.
Contributor

kat-co commented Oct 14, 2016

$$merge$$

Contributor

jujubot commented Oct 14, 2016

Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju-utils

@jujubot jujubot merged commit f0010f5 into juju:master Oct 14, 2016

@kat-co kat-co deleted the kat-co:fixes-1633495-dont-panic-bad-series branch Oct 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment