Skip to content

Commit fa7aab2

Browse files
committed
Wrapped errors with xerrors
1 parent b90fe59 commit fa7aab2

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ require (
44
github.com/davecgh/go-spew v1.1.1 // indirect
55
github.com/pmezard/go-difflib v1.0.0 // indirect
66
github.com/stretchr/testify v1.2.2
7+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
78
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
66
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
7+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
8+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

messenger.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"net/http"
1111
"strings"
1212
"time"
13+
14+
"golang.org/x/xerrors"
1315
)
1416

1517
const (
@@ -192,11 +194,11 @@ func (m *Messenger) ProfileByID(id int64, profileFields []string) (Profile, erro
192194
qr := QueryResponse{}
193195
err = json.Unmarshal(content, &qr)
194196
if qr.Error != nil {
195-
err = fmt.Errorf("facebook error: %s", qr.Error.Message)
197+
return p, xerrors.Errorf("facebook error: %w", qr.Error)
196198
}
197199
}
198200

199-
return p, err
201+
return p, nil
200202
}
201203

202204
// GreetingSetting sends settings for greeting
@@ -279,13 +281,14 @@ func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
279281

280282
err := json.Unmarshal(body, &rec)
281283
if err != nil {
282-
fmt.Println("could not decode response:", err)
284+
err = xerrors.Errorf("could not decode response: %w", err)
285+
fmt.Println(err)
283286
fmt.Fprintln(w, `{status: 'not ok'}`)
284287
return
285288
}
286289

287290
if rec.Object != "page" {
288-
fmt.Println("Object is not page, undefined behaviour. Got", rec.Object)
291+
fmt.Println("object is not page, undefined behaviour. Got", rec.Object)
289292
}
290293

291294
if m.verify {
@@ -304,22 +307,22 @@ func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
304307
// checkIntegrity checks the integrity of the requests received
305308
func (m *Messenger) checkIntegrity(r *http.Request) error {
306309
if m.appSecret == "" {
307-
return fmt.Errorf("missing app secret")
310+
return xerrors.New("missing app secret")
308311
}
309312

310313
sigHeader := "X-Hub-Signature"
311314
sig := strings.SplitN(r.Header.Get(sigHeader), "=", 2)
312315
if len(sig) == 1 {
313316
if sig[0] == "" {
314-
return fmt.Errorf("missing %s header", sigHeader)
317+
return xerrors.Errorf("missing %s header", sigHeader)
315318
}
316-
return fmt.Errorf("malformed %s header: %v", sigHeader, strings.Join(sig, "="))
319+
return xerrors.Errorf("malformed %s header: %v", sigHeader, strings.Join(sig, "="))
317320
}
318321

319322
checkSHA1 := func(body []byte, hash string) error {
320323
mac := hmac.New(sha1.New, []byte(m.appSecret))
321324
if mac.Write(body); fmt.Sprintf("%x", mac.Sum(nil)) != hash {
322-
return fmt.Errorf("invalid signature: %s", hash)
325+
return xerrors.Errorf("invalid signature: %s", hash)
323326
}
324327
return nil
325328
}
@@ -333,7 +336,7 @@ func (m *Messenger) checkIntegrity(r *http.Request) error {
333336
case "sha1":
334337
return checkSHA1(body, sigHash)
335338
default:
336-
return fmt.Errorf("unknown %s header encoding, expected sha1: %s", sigHeader, sig[0])
339+
return xerrors.Errorf("unknown %s header encoding, expected sha1: %s", sigHeader, sig[0])
337340
}
338341
}
339342

response.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"net/http"
1313
"net/textproto"
1414
"strings"
15+
16+
"golang.org/x/xerrors"
1517
)
1618

1719
// AttachmentType is attachment type.
@@ -72,17 +74,21 @@ type QueryError struct {
7274
FBTraceID string `json:"fbtrace_id"`
7375
}
7476

77+
// QueryError implements error
78+
func (e QueryError) Error() string {
79+
return e.Message
80+
}
81+
7582
func checkFacebookError(r io.Reader) error {
7683
var err error
7784

7885
qr := QueryResponse{}
7986
err = json.NewDecoder(r).Decode(&qr)
8087
if err != nil {
81-
return fmt.Errorf("json unmarshal error: %s", err)
88+
return xerrors.Errorf("json unmarshal error: %w", err)
8289
}
8390
if qr.Error != nil {
84-
err = fmt.Errorf("facebook error: %s", qr.Error.Message)
85-
return err
91+
return xerrors.Errorf("facebook error: %w", qr.Error)
8692
}
8793

8894
return nil

0 commit comments

Comments
 (0)