1
0
mirror of synced 2024-11-22 12:56:06 +03:00

Correctly handle Facebook errors (#14)

* Sending back the error message of Facebook as an error for some function calls

* Adding error handling for other function calls

* Private function to parse Facebook's response

* Handling errors for ProfileByID function
This commit is contained in:
Depado 2016-10-07 15:28:27 +02:00 committed by Harrison Shoebridge
parent 79275a89fd
commit 6970a1b09a
3 changed files with 69 additions and 7 deletions

View File

@ -36,7 +36,7 @@ type Delivery struct {
Seq int `json:"seq"` Seq int `json:"seq"`
} }
// Delivery represents a the event fired when a message is read by the // Read represents a the event fired when a message is read by the
// recipient. // recipient.
type Read struct { type Read struct {
// RawWatermark is the timestamp before which all messages have been read // RawWatermark is the timestamp before which all messages have been read

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"time" "time"
) )
@ -120,9 +121,28 @@ func (m *Messenger) ProfileByID(id int64) (Profile, error) {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil {
return p, err
}
defer resp.Body.Close() defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&p) content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return p, err
}
err = json.Unmarshal(content, &p)
if err != nil {
return p, err
}
if p == *new(Profile) {
qr := QueryResponse{}
err = json.Unmarshal(content, &qr)
if qr.Error != nil {
err = fmt.Errorf("Facebook error : %s", qr.Error.Message)
}
}
return p, err return p, err
} }
@ -152,9 +172,12 @@ func (m *Messenger) GreetingSetting(text string) error {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() defer resp.Body.Close()
return err return checkFacebookError(resp.Body)
} }
// CallToActionsSetting sends settings for Get Started or Persist Menu // CallToActionsSetting sends settings for Get Started or Persist Menu
@ -181,9 +204,12 @@ func (m *Messenger) CallToActionsSetting(state string, actions []CallToActionsIt
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() defer resp.Body.Close()
return err return checkFacebookError(resp.Body)
} }
// handle is the internal HTTP handler for the webhooks. // handle is the internal HTTP handler for the webhooks.

View File

@ -16,6 +16,33 @@ const (
SendMessageURL = "https://graph.facebook.com/v2.6/me/messages" SendMessageURL = "https://graph.facebook.com/v2.6/me/messages"
) )
// QueryResponse is the response sent back by Facebook when setting up things
// like greetings or call-to-actions
type QueryResponse struct {
Error *QueryError `json:"error,omitempty"`
Result string `json:"result,omitempty"`
}
// QueryError is representing an error sent back by Facebook
type QueryError struct {
Message string `json:"message"`
Type string `json:"type"`
Code int `json:"code"`
FBTraceID string `json:"fbtrace_id"`
}
func checkFacebookError(r io.Reader) error {
var err error
qr := QueryResponse{}
err = json.NewDecoder(r).Decode(&qr)
if qr.Error != nil {
err = fmt.Errorf("Facebook error : %s", qr.Error.Message)
}
return err
}
// Response is used for responding to events with messages. // Response is used for responding to events with messages.
type Response struct { type Response struct {
token string token string
@ -136,9 +163,12 @@ func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButto
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() defer resp.Body.Close()
return err return checkFacebookError(resp.Body)
} }
// GenericTemplate is a message which allows for structural elements to be sent // GenericTemplate is a message which allows for structural elements to be sent
@ -173,9 +203,12 @@ func (r *Response) GenericTemplate(elements *[]StructuredMessageElement) error {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() defer resp.Body.Close()
return err return checkFacebookError(resp.Body)
} }
// SenderAction sends a info about sender action // SenderAction sends a info about sender action
@ -201,9 +234,12 @@ func (r *Response) SenderAction(action string) error {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() defer resp.Body.Close()
return err return checkFacebookError(resp.Body)
} }
// SendMessage is the information sent in an API request to Facebook. // SendMessage is the information sent in an API request to Facebook.