Skip to content

Commit

Permalink
fix #232: use %w to wrap error
Browse files Browse the repository at this point in the history
  • Loading branch information
huandu committed Jan 16, 2024
1 parent a8accd0 commit 1591be2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Change Log

## v2.7.1

- `[FIX]` [#232](https://github.com/huandu/facebook/issues/232) Fix incorrect error wrapping. Thanks, [@AnatolyRugalev](https://github.com/AnatolyRugalev).

## v2.7.0

- `[NEW]` [#211](https://github.com/huandu/facebook/pull/211) Make session on app configurable. Thank [Spazzy757](https://github.com/Spazzy757) for your PR.
- `[NEW]` [#211](https://github.com/huandu/facebook/pull/211) Make session on app configurable. Thank [@Spazzy757](https://github.com/Spazzy757) for your PR.

## v2.6.0

Expand Down
12 changes: 6 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ func (app *App) ParseSignedRequest(signedRequest string) (res Result, err error)
sig, e1 := base64.RawURLEncoding.DecodeString(strs[0])

if e1 != nil {
err = fmt.Errorf("facebook: fail to decode signed request sig with error %v", e1)
err = fmt.Errorf("facebook: fail to decode signed request sig with error %w", e1)
return
}

payload, e2 := base64.RawURLEncoding.DecodeString(strs[1])

if e2 != nil {
err = fmt.Errorf("facebook: fail to decode signed request payload with error is %v", e2)
err = fmt.Errorf("facebook: fail to decode signed request payload with error is %w", e2)
return
}

err = json.Unmarshal(payload, &res)

if err != nil {
err = fmt.Errorf("facebook: signed request payload is not a valid json string with error %v", err)
err = fmt.Errorf("facebook: signed request payload is not a valid json string with error %w", err)
return
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func (app *App) ParseCodeInfo(code, machineID string) (token string, expires int
})

if err != nil {
err = fmt.Errorf("facebook: fail to parse facebook response with error %v", err)
err = fmt.Errorf("facebook: fail to parse facebook response with error %w", err)
return
}

Expand Down Expand Up @@ -190,7 +190,7 @@ func (app *App) ExchangeToken(accessToken string) (token string, expires int, er
})

if err != nil {
err = fmt.Errorf("fail to parse facebook response with error %v", err)
err = fmt.Errorf("fail to parse facebook response with error %w", err)
return
}

Expand Down Expand Up @@ -230,7 +230,7 @@ func (app *App) GetCode(accessToken string) (code string, err error) {
})

if err != nil {
err = fmt.Errorf("facebook: fail to get code from facebook with error %v", err)
err = fmt.Errorf("facebook: fail to get code from facebook with error %w", err)
return
}

Expand Down
50 changes: 26 additions & 24 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,19 @@ func getValueField(value reflect.Value, fields []string) reflect.Value {
//
// Examples:
//
// type Foo struct {
// // "id" must exist in response. note the leading comma.
// Id string `facebook:",required"`
// type Foo struct {
// // "id" must exist in response. note the leading comma.
// Id string `facebook:",required"`
//
// // use "name" as field name in response.
// TheName string `facebook:"name"`
// // use "name" as field name in response.
// TheName string `facebook:"name"`
//
// // the "json" key also works as expected.
// Key string `json:"my_key"`
// // the "json" key also works as expected.
// Key string `json:"my_key"`
//
// // if both "facebook" and "json" key are set, the "facebook" key is used.
// Value string `facebook:"value" json:"shadowed"`
// }
// // if both "facebook" and "json" key are set, the "facebook" key is used.
// Value string `facebook:"value" json:"shadowed"`
// }
//
// To change default behavior, set a struct tag `facebook:",required"` to fields
// should not be missing.
Expand Down Expand Up @@ -388,12 +388,13 @@ func (res Result) DecodeField(field string, v interface{}) error {
// Err returns an error if Result is a Graph API error.
//
// The returned error can be converted to Error by type assertion.
// err := res.Err()
// if err != nil {
// if e, ok := err.(*Error); ok {
// // read more details in e.Message, e.Code and e.Type
// }
// }
//
// err := res.Err()
// if err != nil {
// if e, ok := err.(*Error); ok {
// // read more details in e.Message, e.Code and e.Type
// }
// }
//
// For more information about Graph API Errors, see
// https://developers.facebook.com/docs/reference/api/errors/
Expand All @@ -420,13 +421,14 @@ func (res Result) Err() error {
//
// Facebook uses following JSON structure to response paging information.
// If "data" doesn't present in Result, Paging will return error.
// {
// "data": [...],
// "paging": {
// "previous": "https://graph.facebook.com/...",
// "next": "https://graph.facebook.com/..."
// }
// }
//
// {
// "data": [...],
// "paging": {
// "previous": "https://graph.facebook.com/...",
// "next": "https://graph.facebook.com/..."
// }
// }
func (res Result) Paging(session *Session) (*PagingResult, error) {
return newPagingResult(session, res)
}
Expand Down Expand Up @@ -614,7 +616,7 @@ func decodeField(val reflect.Value, field reflect.Value, fullName string) error
data, err := json.Marshal(val.Interface())

if err != nil {
return fmt.Errorf("facebook: fail to marshal value for field '%v' with error %v", fullName, err)
return fmt.Errorf("facebook: fail to marshal value for field '%v' with error %w", fullName, err)
}

return unmarshaler.UnmarshalJSON(data)
Expand Down
8 changes: 4 additions & 4 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func (session *Session) sendPostRequest(uri string, params Params, res interface
mime, err := params.Encode(buf)

if err != nil {
return nil, fmt.Errorf("facebook: cannot encode POST params; %v", err)
return nil, fmt.Errorf("facebook: cannot encode POST params; %w", err)
}

request, err := http.NewRequest("POST", uri, buf)
Expand All @@ -502,7 +502,7 @@ func (session *Session) sendOauthRequest(uri string, params Params) (Result, err
mime, err := params.Encode(buf)

if err != nil {
return nil, fmt.Errorf("facebook: cannot encode POST params; %v", err)
return nil, fmt.Errorf("facebook: cannot encode POST params; %w", err)
}

request, err := http.NewRequest("POST", urlStr, buf)
Expand Down Expand Up @@ -562,7 +562,7 @@ func (session *Session) sendRequest(request *http.Request) (response *http.Respo
}

if err != nil {
err = fmt.Errorf("facebook: cannot reach facebook server; %v", err)
err = fmt.Errorf("facebook: cannot reach facebook server; %w", err)
return
}

Expand All @@ -571,7 +571,7 @@ func (session *Session) sendRequest(request *http.Request) (response *http.Respo
response.Body.Close()

if err != nil {
err = fmt.Errorf("facebook: cannot read facebook response; %v", err)
err = fmt.Errorf("facebook: cannot read facebook response; %w", err)
}

data = buf.Bytes()
Expand Down

0 comments on commit 1591be2

Please sign in to comment.