Skip to content

Commit

Permalink
fixed documentation for NewClient, added ErrNoInstanceGiven
Browse files Browse the repository at this point in the history
  • Loading branch information
opalmer committed Oct 15, 2016
1 parent 7e459ad commit da1df7e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 15 additions & 7 deletions gerrit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gerrit
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions gerrit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`")
}
}

0 comments on commit da1df7e

Please sign in to comment.