Wrapped errors with xerrors
This commit is contained in:
parent
b90fe59765
commit
fa7aab250c
1
go.mod
1
go.mod
@ -4,4 +4,5 @@ require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.2.2
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
|
||||
)
|
||||
|
2
go.sum
2
go.sum
@ -4,3 +4,5 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
21
messenger.go
21
messenger.go
@ -10,6 +10,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -192,11 +194,11 @@ func (m *Messenger) ProfileByID(id int64, profileFields []string) (Profile, erro
|
||||
qr := QueryResponse{}
|
||||
err = json.Unmarshal(content, &qr)
|
||||
if qr.Error != nil {
|
||||
err = fmt.Errorf("facebook error: %s", qr.Error.Message)
|
||||
return p, xerrors.Errorf("facebook error: %w", qr.Error)
|
||||
}
|
||||
}
|
||||
|
||||
return p, err
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// GreetingSetting sends settings for greeting
|
||||
@ -279,13 +281,14 @@ func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err := json.Unmarshal(body, &rec)
|
||||
if err != nil {
|
||||
fmt.Println("could not decode response:", err)
|
||||
err = xerrors.Errorf("could not decode response: %w", err)
|
||||
fmt.Println(err)
|
||||
fmt.Fprintln(w, `{status: 'not ok'}`)
|
||||
return
|
||||
}
|
||||
|
||||
if rec.Object != "page" {
|
||||
fmt.Println("Object is not page, undefined behaviour. Got", rec.Object)
|
||||
fmt.Println("object is not page, undefined behaviour. Got", rec.Object)
|
||||
}
|
||||
|
||||
if m.verify {
|
||||
@ -304,22 +307,22 @@ func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
|
||||
// checkIntegrity checks the integrity of the requests received
|
||||
func (m *Messenger) checkIntegrity(r *http.Request) error {
|
||||
if m.appSecret == "" {
|
||||
return fmt.Errorf("missing app secret")
|
||||
return xerrors.New("missing app secret")
|
||||
}
|
||||
|
||||
sigHeader := "X-Hub-Signature"
|
||||
sig := strings.SplitN(r.Header.Get(sigHeader), "=", 2)
|
||||
if len(sig) == 1 {
|
||||
if sig[0] == "" {
|
||||
return fmt.Errorf("missing %s header", sigHeader)
|
||||
return xerrors.Errorf("missing %s header", sigHeader)
|
||||
}
|
||||
return fmt.Errorf("malformed %s header: %v", sigHeader, strings.Join(sig, "="))
|
||||
return xerrors.Errorf("malformed %s header: %v", sigHeader, strings.Join(sig, "="))
|
||||
}
|
||||
|
||||
checkSHA1 := func(body []byte, hash string) error {
|
||||
mac := hmac.New(sha1.New, []byte(m.appSecret))
|
||||
if mac.Write(body); fmt.Sprintf("%x", mac.Sum(nil)) != hash {
|
||||
return fmt.Errorf("invalid signature: %s", hash)
|
||||
return xerrors.Errorf("invalid signature: %s", hash)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -333,7 +336,7 @@ func (m *Messenger) checkIntegrity(r *http.Request) error {
|
||||
case "sha1":
|
||||
return checkSHA1(body, sigHash)
|
||||
default:
|
||||
return fmt.Errorf("unknown %s header encoding, expected sha1: %s", sigHeader, sig[0])
|
||||
return xerrors.Errorf("unknown %s header encoding, expected sha1: %s", sigHeader, sig[0])
|
||||
}
|
||||
}
|
||||
|
||||
|
12
response.go
12
response.go
@ -12,6 +12,8 @@ import (
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// AttachmentType is attachment type.
|
||||
@ -72,17 +74,21 @@ type QueryError struct {
|
||||
FBTraceID string `json:"fbtrace_id"`
|
||||
}
|
||||
|
||||
// QueryError implements error
|
||||
func (e QueryError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func checkFacebookError(r io.Reader) error {
|
||||
var err error
|
||||
|
||||
qr := QueryResponse{}
|
||||
err = json.NewDecoder(r).Decode(&qr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("json unmarshal error: %s", err)
|
||||
return xerrors.Errorf("json unmarshal error: %w", err)
|
||||
}
|
||||
if qr.Error != nil {
|
||||
err = fmt.Errorf("facebook error: %s", qr.Error.Message)
|
||||
return err
|
||||
return xerrors.Errorf("facebook error: %w", qr.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user