1
0
mirror of synced 2024-11-22 04:46:05 +03:00

Wrapped errors with xerrors

This commit is contained in:
Sergey Parshukov 2019-07-31 00:38:58 +03:00
parent b90fe59765
commit fa7aab250c
No known key found for this signature in database
GPG Key ID: 9D0FB8D5E702923A
4 changed files with 24 additions and 12 deletions

1
go.mod
View File

@ -4,4 +4,5 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2 github.com/stretchr/testify v1.2.2
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
) )

2
go.sum
View File

@ -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/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 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 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=

View File

@ -10,6 +10,8 @@ import (
"net/http" "net/http"
"strings" "strings"
"time" "time"
"golang.org/x/xerrors"
) )
const ( const (
@ -192,11 +194,11 @@ func (m *Messenger) ProfileByID(id int64, profileFields []string) (Profile, erro
qr := QueryResponse{} qr := QueryResponse{}
err = json.Unmarshal(content, &qr) err = json.Unmarshal(content, &qr)
if qr.Error != nil { 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 // GreetingSetting sends settings for greeting
@ -279,13 +281,14 @@ func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
err := json.Unmarshal(body, &rec) err := json.Unmarshal(body, &rec)
if err != nil { 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'}`) fmt.Fprintln(w, `{status: 'not ok'}`)
return return
} }
if rec.Object != "page" { 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 { 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 // checkIntegrity checks the integrity of the requests received
func (m *Messenger) checkIntegrity(r *http.Request) error { func (m *Messenger) checkIntegrity(r *http.Request) error {
if m.appSecret == "" { if m.appSecret == "" {
return fmt.Errorf("missing app secret") return xerrors.New("missing app secret")
} }
sigHeader := "X-Hub-Signature" sigHeader := "X-Hub-Signature"
sig := strings.SplitN(r.Header.Get(sigHeader), "=", 2) sig := strings.SplitN(r.Header.Get(sigHeader), "=", 2)
if len(sig) == 1 { if len(sig) == 1 {
if sig[0] == "" { 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 { checkSHA1 := func(body []byte, hash string) error {
mac := hmac.New(sha1.New, []byte(m.appSecret)) mac := hmac.New(sha1.New, []byte(m.appSecret))
if mac.Write(body); fmt.Sprintf("%x", mac.Sum(nil)) != hash { 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 return nil
} }
@ -333,7 +336,7 @@ func (m *Messenger) checkIntegrity(r *http.Request) error {
case "sha1": case "sha1":
return checkSHA1(body, sigHash) return checkSHA1(body, sigHash)
default: 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])
} }
} }

View File

@ -12,6 +12,8 @@ import (
"net/http" "net/http"
"net/textproto" "net/textproto"
"strings" "strings"
"golang.org/x/xerrors"
) )
// AttachmentType is attachment type. // AttachmentType is attachment type.
@ -72,17 +74,21 @@ type QueryError struct {
FBTraceID string `json:"fbtrace_id"` FBTraceID string `json:"fbtrace_id"`
} }
// QueryError implements error
func (e QueryError) Error() string {
return e.Message
}
func checkFacebookError(r io.Reader) error { func checkFacebookError(r io.Reader) error {
var err error var err error
qr := QueryResponse{} qr := QueryResponse{}
err = json.NewDecoder(r).Decode(&qr) err = json.NewDecoder(r).Decode(&qr)
if err != nil { if err != nil {
return fmt.Errorf("json unmarshal error: %s", err) return xerrors.Errorf("json unmarshal error: %w", err)
} }
if qr.Error != nil { if qr.Error != nil {
err = fmt.Errorf("facebook error: %s", qr.Error.Message) return xerrors.Errorf("facebook error: %w", qr.Error)
return err
} }
return nil return nil