Skip to content

Commit

Permalink
Wrap errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mtibben committed Sep 10, 2021
1 parent a644175 commit a8903ca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (p *Client) Post(query string, response interface{}, options ...Option) err
func (p *Client) RawPost(query string, options ...Option) (*Response, error) {
r, err := p.newRequest(query, options...)
if err != nil {
return nil, fmt.Errorf("build: %s", err.Error())
return nil, fmt.Errorf("build: %w", err)
}

w := httptest.NewRecorder()
Expand All @@ -97,7 +97,7 @@ func (p *Client) RawPost(query string, options ...Option) (*Response, error) {
respDataRaw := &Response{}
err = json.Unmarshal(w.Body.Bytes(), &respDataRaw)
if err != nil {
return nil, fmt.Errorf("decode: %s", err.Error())
return nil, fmt.Errorf("decode: %w", err)
}

return respDataRaw, nil
Expand All @@ -123,7 +123,7 @@ func (p *Client) newRequest(query string, options ...Option) (*http.Request, err
case "application/json":
requestBody, err := json.Marshal(bd)
if err != nil {
return nil, fmt.Errorf("encode: %s", err.Error())
return nil, fmt.Errorf("encode: %w", err)
}
bd.HTTP.Body = ioutil.NopCloser(bytes.NewBuffer(requestBody))
default:
Expand All @@ -141,7 +141,7 @@ func unpack(data interface{}, into interface{}) error {
ZeroFields: true,
})
if err != nil {
return fmt.Errorf("mapstructure: %s", err.Error())
return fmt.Errorf("mapstructure: %w", err)
}

return d.Decode(data)
Expand Down
18 changes: 9 additions & 9 deletions client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,37 @@ func (p *Client) WebsocketOnce(query string, resp interface{}, options ...Option
func (p *Client) WebsocketWithPayload(query string, initPayload map[string]interface{}, options ...Option) *Subscription {
r, err := p.newRequest(query, options...)
if err != nil {
return errorSubscription(fmt.Errorf("request: %s", err.Error()))
return errorSubscription(fmt.Errorf("request: %w", err))
}

requestBody, err := ioutil.ReadAll(r.Body)
if err != nil {
return errorSubscription(fmt.Errorf("parse body: %s", err.Error()))
return errorSubscription(fmt.Errorf("parse body: %w", err))
}

srv := httptest.NewServer(p.h)
host := strings.Replace(srv.URL, "http://", "ws://", -1)
c, _, err := websocket.DefaultDialer.Dial(host+r.URL.Path, r.Header)

if err != nil {
return errorSubscription(fmt.Errorf("dial: %s", err.Error()))
return errorSubscription(fmt.Errorf("dial: %w", err))
}

initMessage := operationMessage{Type: connectionInitMsg}
if initPayload != nil {
initMessage.Payload, err = json.Marshal(initPayload)
if err != nil {
return errorSubscription(fmt.Errorf("parse payload: %s", err.Error()))
return errorSubscription(fmt.Errorf("parse payload: %w", err))
}
}

if err = c.WriteJSON(initMessage); err != nil {
return errorSubscription(fmt.Errorf("init: %s", err.Error()))
return errorSubscription(fmt.Errorf("init: %w", err))
}

var ack operationMessage
if err = c.ReadJSON(&ack); err != nil {
return errorSubscription(fmt.Errorf("ack: %s", err.Error()))
return errorSubscription(fmt.Errorf("ack: %w", err))
}

if ack.Type != connectionAckMsg {
Expand All @@ -92,15 +92,15 @@ func (p *Client) WebsocketWithPayload(query string, initPayload map[string]inter

var ka operationMessage
if err = c.ReadJSON(&ka); err != nil {
return errorSubscription(fmt.Errorf("ack: %s", err.Error()))
return errorSubscription(fmt.Errorf("ack: %w", err))
}

if ka.Type != connectionKaMsg {
return errorSubscription(fmt.Errorf("expected ack message, got %#v", ack))
}

if err = c.WriteJSON(operationMessage{Type: startMsg, ID: "1", Payload: requestBody}); err != nil {
return errorSubscription(fmt.Errorf("start: %s", err.Error()))
return errorSubscription(fmt.Errorf("start: %w", err))
}

return &Subscription{
Expand All @@ -125,7 +125,7 @@ func (p *Client) WebsocketWithPayload(query string, initPayload map[string]inter
var respDataRaw Response
err = json.Unmarshal(op.Payload, &respDataRaw)
if err != nil {
return fmt.Errorf("decode: %s", err.Error())
return fmt.Errorf("decode: %w", err)
}

// we want to unpack even if there is an error, so we can see partial responses
Expand Down
2 changes: 1 addition & 1 deletion internal/rewrite/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r *Rewriter) getFile(filename string) string {
if _, ok := r.files[filename]; !ok {
b, err := ioutil.ReadFile(filename)
if err != nil {
panic(fmt.Errorf("unable to load file, already exists: %s", err.Error()))
panic(fmt.Errorf("unable to load file, already exists: %w", err))
}

r.files[filename] = string(b)
Expand Down

0 comments on commit a8903ca

Please sign in to comment.