From da1df7eecf1f90936b4b667f647b7f1d67baa86b Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Sat, 15 Oct 2016 09:09:17 -0400 Subject: [PATCH] fixed documentation for NewClient, added ErrNoInstanceGiven --- CHANGELOG.md | 3 +++ gerrit.go | 22 +++++++++++++++------- gerrit_test.go | 7 +++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00a00f6..e055acc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ first. For more complete details see instead of a hard coded value when comparing response codes. * Updated AccountInfo.AccountID to be omitted of empty (such as when used in ApprovalInfo). +* Fixed documentation for NewClient and moved fmt.Errorf call from + inside the function to a `ErrNoInstanceGiven` variable so it's + easier to compare against. ### 0.1.0 diff --git a/gerrit.go b/gerrit.go index 2b762a4..73b2a58 100644 --- a/gerrit.go +++ b/gerrit.go @@ -3,6 +3,7 @@ package gerrit import ( "bytes" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -51,18 +52,25 @@ type Response struct { *http.Response } -// NewClient returns a new Gerrit API client. -// gerritInstance has to be the HTTP endpoint of the Gerrit instance. -// If a nil httpClient is provided, http.DefaultClient will be used. -func NewClient(gerritURL string, httpClient *http.Client) (*Client, error) { +var ( + // ErrNoInstanceGiven is returned by NewClient in the event the + // gerritURL argument was blank. + ErrNoInstanceGiven = errors.New("No Gerrit instance given.") +) + +// NewClient returns a new Gerrit API client. The gerritURL argument has to be the +// HTTP endpoint of the Gerrit instance, http://localhost:8080/ for example. If a nil +// httpClient is provided, http.DefaultClient will be used. +func NewClient(endpoint string, httpClient *http.Client) (*Client, error) { if httpClient == nil { httpClient = http.DefaultClient } - if len(gerritURL) == 0 { - return nil, fmt.Errorf("No Gerrit instance given.") + if len(endpoint) == 0 { + return nil, ErrNoInstanceGiven } - baseURL, err := url.Parse(gerritURL) + + baseURL, err := url.Parse(endpoint) if err != nil { return nil, err } diff --git a/gerrit_test.go b/gerrit_test.go index 7d70350..b87f7c9 100644 --- a/gerrit_test.go +++ b/gerrit_test.go @@ -288,3 +288,10 @@ func TestRemoveMagicPrefixLineDoesNothingWithoutPrefix(t *testing.T) { } } } + +func TestErrNoInstanceGiven(t *testing.T) { + _, err := gerrit.NewClient("", nil) + if err != gerrit.ErrNoInstanceGiven { + t.Error("Expected `ErrNoInstanceGiven`") + } +}