Use github.com/pkg/errors
authorMikael Berthe <mikael@lilotux.net>
Sun, 30 Apr 2017 22:58:12 +0200
changeset 162 68df3a01e1a7
parent 161 6786f169b59f
child 163 53fd74d3bf21
Use github.com/pkg/errors
.travis.yml
account.go
api.go
app.go
login.go
madon.go
media.go
status.go
streams.go
timelines.go
--- a/.travis.yml	Sun Apr 30 22:22:12 2017 +0200
+++ b/.travis.yml	Sun Apr 30 22:58:12 2017 +0200
@@ -11,7 +11,7 @@
   only:
   - master
 install:
-- go get github.com/urfave/cli
+- go get github.com/pkg/errors
 - go get github.com/stretchr/testify/assert
 - go get github.com/sendgrid/rest
 - go get github.com/gorilla/websocket
--- a/account.go	Sun Apr 30 22:22:12 2017 +0200
+++ b/account.go	Sun Apr 30 22:58:12 2017 +0200
@@ -17,6 +17,7 @@
 	"strconv"
 	"strings"
 
+	"github.com/pkg/errors"
 	"github.com/sendgrid/rest"
 )
 
@@ -390,7 +391,7 @@
 	// Prepare the request
 	req, err := mc.prepareRequest(endPoint, rest.Patch, params)
 	if err != nil {
-		return nil, fmt.Errorf("prepareRequest failed: %s", err.Error())
+		return nil, errors.Wrap(err, "prepareRequest failed")
 	}
 	req.Headers["Content-Type"] = w.FormDataContentType()
 	req.Body = formBuf.Bytes()
@@ -398,7 +399,7 @@
 	// Make API call
 	r, err := restAPI(req)
 	if err != nil {
-		return nil, fmt.Errorf("account update failed: %s", err.Error())
+		return nil, errors.Wrap(err, "account update failed")
 	}
 
 	// Check for error reply
@@ -406,14 +407,14 @@
 	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
 		// The empty object is not an error
 		if errorResult.Text != "" {
-			return nil, fmt.Errorf("%s", errorResult.Text)
+			return nil, errors.New(errorResult.Text)
 		}
 	}
 
 	// Not an error reply; let's unmarshal the data
 	var account Account
 	if err := json.Unmarshal([]byte(r.Body), &account); err != nil {
-		return nil, fmt.Errorf("cannot decode API response: %s", err.Error())
+		return nil, errors.Wrap(err, "cannot decode API response")
 	}
 	return &account, nil
 }
--- a/api.go	Sun Apr 30 22:22:12 2017 +0200
+++ b/api.go	Sun Apr 30 22:58:12 2017 +0200
@@ -17,6 +17,7 @@
 	"strconv"
 	"strings"
 
+	"github.com/pkg/errors"
 	"github.com/sendgrid/rest"
 )
 
@@ -167,7 +168,7 @@
 // will be set (if they exist) in the structure.
 func (mc *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, limitOptions *LimitParams, links *apiLinks, data interface{}) error {
 	if mc == nil {
-		return fmt.Errorf("use of uninitialized madon client")
+		return errors.New("use of uninitialized madon client")
 	}
 
 	if limitOptions != nil {
@@ -194,13 +195,13 @@
 	// Make API call
 	r, err := restAPI(req)
 	if err != nil {
-		return fmt.Errorf("API query (%s) failed: %s", endPoint, err.Error())
+		return errors.Wrapf(err, "API query (%s) failed", endPoint)
 	}
 
 	if links != nil {
 		pLinks, err := parseLink(r.Headers["Link"])
 		if err != nil {
-			return fmt.Errorf("cannot decode header links (%s): %s", method, err.Error())
+			return errors.Wrapf(err, "cannot decode header links (%s)", method)
 		}
 		if pLinks != nil {
 			*links = *pLinks
@@ -212,14 +213,14 @@
 	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
 		// The empty object is not an error
 		if errorResult.Text != "" {
-			return fmt.Errorf("%s", errorResult.Text)
+			return errors.New(errorResult.Text)
 		}
 	}
 
 	// Not an error reply; let's unmarshal the data
 	err = json.Unmarshal([]byte(r.Body), &data)
 	if err != nil {
-		return fmt.Errorf("cannot decode API response (%s): %s", method, err.Error())
+		return errors.Wrapf(err, "cannot decode API response (%s)", method)
 	}
 	return nil
 }
--- a/app.go	Sun Apr 30 22:22:12 2017 +0200
+++ b/app.go	Sun Apr 30 22:58:12 2017 +0200
@@ -8,10 +8,10 @@
 package madon
 
 import (
-	"errors"
 	"net/url"
 	"strings"
 
+	"github.com/pkg/errors"
 	"github.com/sendgrid/rest"
 )
 
--- a/login.go	Sun Apr 30 22:22:12 2017 +0200
+++ b/login.go	Sun Apr 30 22:58:12 2017 +0200
@@ -8,9 +8,9 @@
 
 import (
 	"encoding/json"
-	"fmt"
 	"strings"
 
+	"github.com/pkg/errors"
 	"github.com/sendgrid/rest"
 )
 
@@ -29,10 +29,10 @@
 	}
 
 	if username == "" {
-		return fmt.Errorf("missing username")
+		return errors.New("missing username")
 	}
 	if password == "" {
-		return fmt.Errorf("missing password")
+		return errors.New("missing password")
 	}
 
 	hdrs := make(map[string]string)
@@ -65,7 +65,7 @@
 
 	err = json.Unmarshal([]byte(r.Body), &resp)
 	if err != nil {
-		return fmt.Errorf("cannot unmarshal server response: %s", err.Error())
+		return errors.Wrap(err, "cannot unmarshal server response")
 	}
 
 	mc.UserToken = &resp
--- a/madon.go	Sun Apr 30 22:22:12 2017 +0200
+++ b/madon.go	Sun Apr 30 22:58:12 2017 +0200
@@ -8,7 +8,7 @@
 package madon
 
 import (
-	"errors"
+	"github.com/pkg/errors"
 )
 
 // LimitParams contains common limit/paging options for the Mastodon REST API
--- a/media.go	Sun Apr 30 22:22:12 2017 +0200
+++ b/media.go	Sun Apr 30 22:58:12 2017 +0200
@@ -9,12 +9,12 @@
 import (
 	"bytes"
 	"encoding/json"
-	"fmt"
 	"io"
 	"mime/multipart"
 	"os"
 	"path/filepath"
 
+	"github.com/pkg/errors"
 	"github.com/sendgrid/rest"
 )
 
@@ -28,24 +28,24 @@
 
 	f, err := os.Open(filePath)
 	if err != nil {
-		return nil, fmt.Errorf("cannot read file: %s", err.Error())
+		return nil, errors.Wrap(err, "cannot read file")
 	}
 	defer f.Close()
 
 	w := multipart.NewWriter(&b)
 	formWriter, err := w.CreateFormFile("file", filepath.Base(filePath))
 	if err != nil {
-		return nil, fmt.Errorf("media upload: cannot create form: %s", err.Error())
+		return nil, errors.Wrap(err, "media upload")
 	}
 	if _, err = io.Copy(formWriter, f); err != nil {
-		return nil, fmt.Errorf("media upload: cannot create form: %s", err.Error())
+		return nil, errors.Wrap(err, "media upload")
 	}
 
 	w.Close()
 
 	req, err := mc.prepareRequest("media", rest.Post, nil)
 	if err != nil {
-		return nil, fmt.Errorf("media prepareRequest failed: %s", err.Error())
+		return nil, errors.Wrap(err, "media prepareRequest failed")
 	}
 	req.Headers["Content-Type"] = w.FormDataContentType()
 	req.Body = b.Bytes()
@@ -53,7 +53,7 @@
 	// Make API call
 	r, err := restAPI(req)
 	if err != nil {
-		return nil, fmt.Errorf("media upload failed: %s", err.Error())
+		return nil, errors.Wrap(err, "media upload failed")
 	}
 
 	// Check for error reply
@@ -61,7 +61,7 @@
 	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
 		// The empty object is not an error
 		if errorResult.Text != "" {
-			return nil, fmt.Errorf("%s", errorResult.Text)
+			return nil, errors.New(errorResult.Text)
 		}
 	}
 
@@ -69,7 +69,7 @@
 	var attachment Attachment
 	err = json.Unmarshal([]byte(r.Body), &attachment)
 	if err != nil {
-		return nil, fmt.Errorf("cannot decode API response (media): %s", err.Error())
+		return nil, errors.Wrap(err, "cannot decode API response (media)")
 	}
 	return &attachment, nil
 }
--- a/status.go	Sun Apr 30 22:22:12 2017 +0200
+++ b/status.go	Sun Apr 30 22:58:12 2017 +0200
@@ -10,6 +10,7 @@
 	"fmt"
 	"strconv"
 
+	"github.com/pkg/errors"
 	"github.com/sendgrid/rest"
 )
 
@@ -97,7 +98,7 @@
 			return ErrInvalidParameter
 		}
 		if len(opts.MediaIDs) > 4 {
-			return fmt.Errorf("too many (>4) media IDs")
+			return errors.New("too many (>4) media IDs")
 		}
 	case "delete":
 		method = rest.Delete
--- a/streams.go	Sun Apr 30 22:22:12 2017 +0200
+++ b/streams.go	Sun Apr 30 22:58:12 2017 +0200
@@ -8,12 +8,11 @@
 
 import (
 	"encoding/json"
-	"errors"
-	"fmt"
 	"net/url"
 	"strings"
 
 	"github.com/gorilla/websocket"
+	"github.com/pkg/errors"
 )
 
 // StreamEvent contains a single event from the streaming API
@@ -49,7 +48,7 @@
 	// Build streaming websocket URL
 	u, err := url.Parse("ws" + mc.APIBase[4:] + "/streaming/")
 	if err != nil {
-		return nil, errors.New("cannot create Websocket URL: " + err.Error())
+		return nil, errors.Wrap(err, "cannot create Websocket URL")
 	}
 
 	urlParams := url.Values{}
@@ -92,7 +91,7 @@
 			if strings.Contains(err.Error(), "close 1000 (normal)") {
 				break // Connection properly closed
 			}
-			e := fmt.Errorf("read error: %v", err)
+			e := errors.Wrap(err, "read error")
 			events <- StreamEvent{Event: "error", Error: e}
 			break
 		}
@@ -104,13 +103,13 @@
 		case "update":
 			strPayload, ok := msg.Payload.(string)
 			if !ok {
-				e := fmt.Errorf("could not decode status: payload isn't a string")
+				e := errors.New("could not decode status: payload isn't a string")
 				events <- StreamEvent{Event: "error", Error: e}
 				continue
 			}
 			var s Status
 			if err := json.Unmarshal([]byte(strPayload), &s); err != nil {
-				e := fmt.Errorf("could not decode status: %v", err)
+				e := errors.Wrap(err, "could not decode status")
 				events <- StreamEvent{Event: "error", Error: e}
 				continue
 			}
@@ -118,13 +117,13 @@
 		case "notification":
 			strPayload, ok := msg.Payload.(string)
 			if !ok {
-				e := fmt.Errorf("could not decode notification: payload isn't a string")
+				e := errors.New("could not decode notification: payload isn't a string")
 				events <- StreamEvent{Event: "error", Error: e}
 				continue
 			}
 			var notif Notification
 			if err := json.Unmarshal([]byte(strPayload), &notif); err != nil {
-				e := fmt.Errorf("could not decode notification: %v", err)
+				e := errors.Wrap(err, "could not decode notification")
 				events <- StreamEvent{Event: "error", Error: e}
 				continue
 			}
@@ -132,13 +131,13 @@
 		case "delete":
 			floatPayload, ok := msg.Payload.(float64)
 			if !ok {
-				e := fmt.Errorf("could not decode deletion: payload isn't a number")
+				e := errors.New("could not decode deletion: payload isn't a number")
 				events <- StreamEvent{Event: "error", Error: e}
 				continue
 			}
 			obj = int64(floatPayload) // statusID
 		default:
-			e := fmt.Errorf("unhandled event '%s'", msg.Event)
+			e := errors.Errorf("unhandled event '%s'", msg.Event)
 			events <- StreamEvent{Event: "error", Error: e}
 			continue
 		}
--- a/timelines.go	Sun Apr 30 22:22:12 2017 +0200
+++ b/timelines.go	Sun Apr 30 22:58:12 2017 +0200
@@ -7,8 +7,9 @@
 package madon
 
 import (
-	"fmt"
 	"strings"
+
+	"github.com/pkg/errors"
 )
 
 // GetTimelines returns a timeline (a list of statuses
@@ -28,11 +29,11 @@
 	case strings.HasPrefix(timeline, ":"), strings.HasPrefix(timeline, "#"):
 		hashtag := timeline[1:]
 		if hashtag == "" {
-			return nil, fmt.Errorf("timelines API: empty hashtag")
+			return nil, errors.New("timelines API: empty hashtag")
 		}
 		endPoint = "timelines/tag/" + hashtag
 	default:
-		return nil, fmt.Errorf("GetTimelines: bad timelines argument")
+		return nil, errors.New("GetTimelines: bad timelines argument")
 	}
 
 	params := make(apiCallParams)