Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
handle all errors, use pkg/errors to add context
Browse files Browse the repository at this point in the history
  • Loading branch information
jaffee committed Sep 20, 2017
1 parent b6ad2fa commit a5a011a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ package pilosa
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -46,6 +45,7 @@ import (

"github.com/golang/protobuf/proto"
"github.com/pilosa/go-pilosa/internal"
"github.com/pkg/errors"
)

const maxHosts = 10
Expand Down Expand Up @@ -114,7 +114,10 @@ func (c *Client) Query(query PQLQuery, options *QueryOptions) (*QueryResponse, e
if options == nil {
options = &QueryOptions{}
}
data := makeRequestData(query.serialize(), options)
data, err := makeRequestData(query.serialize(), options)
if err != nil {
return nil, errors.Wrap(err, "making request data")
}
path := fmt.Sprintf("/index/%s/query", query.Index().name)
_, buf, err := c.httpRequest("POST", path, data, protobufHeaders, rawResponse)
if err != nil {
Expand Down Expand Up @@ -428,9 +431,12 @@ func (c *Client) fetchFragmentNodes(indexName string, slice uint64) ([]fragmentN
}

func (c *Client) importNode(request *internal.ImportRequest) error {
data, _ := proto.Marshal(request)
data, err := proto.Marshal(request)
if err != nil {
return errors.Wrap(err, "marshaling to protobuf")
}
// request.Marshal never returns an error
_, _, err := c.httpRequest("POST", "/import", data, protobufHeaders, noResponse)
_, _, err = c.httpRequest("POST", "/import", data, protobufHeaders, noResponse)
if err != nil {
return err
}
Expand Down Expand Up @@ -582,16 +588,18 @@ func newHTTPClient(options *ClientOptions) *http.Client {
}
}

func makeRequestData(query string, options *QueryOptions) []byte {
func makeRequestData(query string, options *QueryOptions) ([]byte, error) {
request := &internal.QueryRequest{
Query: query,
ColumnAttrs: options.Columns,
ExcludeAttrs: options.ExcludeAttrs,
ExcludeBits: options.ExcludeBits,
}
r, _ := proto.Marshal(request)
// request.Marshal never returns an error
return r
r, err := proto.Marshal(request)
if err != nil {
return nil, errors.Wrap(err, "marshaling request to protobuf")
}
return r, nil
}

func matchError(msg string) error {
Expand Down
10 changes: 7 additions & 3 deletions uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
package pilosa

import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"

"github.com/pkg/errors"
)

var addressRegexp = regexp.MustCompile("^(([+a-z]+):\\/\\/)?([0-9a-z.-]+)?(:([0-9]+))?$")
Expand Down Expand Up @@ -135,12 +136,15 @@ func parseAddress(address string) (uri *URI, err error) {
}
var port = 10101
if m[5] != "" {
port, _ = strconv.Atoi(m[5])
port, err = strconv.Atoi(m[5])
if err != nil {
return nil, errors.Wrap(err, "converting port string to int")
}
}
uri = &URI{
scheme: scheme,
host: host,
port: uint16(port),
}
return
return uri, nil
}

0 comments on commit a5a011a

Please sign in to comment.