Add error handling to general example
This commit is contained in:
8
chat.go
8
chat.go
@ -74,8 +74,8 @@ func getNewMessages(k *Keybase, c chan<- ChatAPI, execOptions []string) {
|
||||
for scanner.Scan() {
|
||||
var jsonData ChatAPI
|
||||
json.Unmarshal([]byte(scanner.Text()), &jsonData)
|
||||
if len([]byte(jsonData.ErrorRaw)) > 0 {
|
||||
var errorListen = string(jsonData.ErrorRaw)
|
||||
if jsonData.ErrorRaw != nil {
|
||||
var errorListen = string(*jsonData.ErrorRaw)
|
||||
jsonData.ErrorListen = &errorListen
|
||||
}
|
||||
c <- jsonData
|
||||
@ -155,9 +155,9 @@ func chatAPIOut(k *Keybase, c ChatAPI) (ChatAPI, error) {
|
||||
if err := json.Unmarshal(cmdOut, &r); err != nil {
|
||||
return ChatAPI{}, err
|
||||
}
|
||||
if len([]byte(r.ErrorRaw)) > 0 {
|
||||
if r.ErrorRaw != nil {
|
||||
var errorRead Error
|
||||
json.Unmarshal([]byte(r.ErrorRaw), &errorRead)
|
||||
json.Unmarshal([]byte(*r.ErrorRaw), &errorRead)
|
||||
r.ErrorRead = &errorRead
|
||||
return r, errors.New(r.ErrorRead.Message)
|
||||
}
|
||||
|
||||
5
docs.go
5
docs.go
@ -48,6 +48,11 @@ in @mkbot#test1:
|
||||
}
|
||||
|
||||
func handler(m keybase.ChatAPI) {
|
||||
if m.ErrorListen != nil {
|
||||
fmt.Printf("Error: %s\n", *m.ErrorListen)
|
||||
return
|
||||
}
|
||||
|
||||
msgType := m.Msg.Content.Type
|
||||
msgID := m.Msg.ID
|
||||
deviceName := m.Msg.Sender.DeviceName
|
||||
|
||||
30
types.go
30
types.go
@ -19,21 +19,21 @@ type RunOptions struct {
|
||||
|
||||
// ChatAPI holds information about a message received by the `keybase chat api-listen` command
|
||||
type ChatAPI struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
Msg *msg `json:"msg,omitempty"`
|
||||
Method string `json:"method,omitempty"`
|
||||
Params *params `json:"params,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
ID int `json:"id,omitempty"`
|
||||
Ratelimits []rateLimits `json:"ratelimits,omitempty"`
|
||||
Notification *notification `json:"notification,omitempty"`
|
||||
Result *result `json:"result,omitempty"`
|
||||
Pagination *pagination `json:"pagination,omitempty"`
|
||||
ErrorRaw json.RawMessage `json:"error,omitempty"` // Raw JSON string containit any errors returned
|
||||
ErrorRead *Error `json:"-"` // Errors returned by any outgoing chat functions such as Read(), Edit(), etc
|
||||
ErrorListen *string `json:"-"` // Errors returned by the api-listen command (used in the Run() function)
|
||||
keybase Keybase // Some methods will need this, so I'm passing it but keeping it unexported
|
||||
Type string `json:"type,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
Msg *msg `json:"msg,omitempty"`
|
||||
Method string `json:"method,omitempty"`
|
||||
Params *params `json:"params,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
ID int `json:"id,omitempty"`
|
||||
Ratelimits []rateLimits `json:"ratelimits,omitempty"`
|
||||
Notification *notification `json:"notification,omitempty"`
|
||||
Result *result `json:"result,omitempty"`
|
||||
Pagination *pagination `json:"pagination,omitempty"`
|
||||
ErrorRaw *json.RawMessage `json:"error,omitempty"` // Raw JSON string containing any errors returned
|
||||
ErrorRead *Error `json:"-"` // Errors returned by any outgoing chat functions such as Read(), Edit(), etc
|
||||
ErrorListen *string `json:"-"` // Errors returned by the api-listen command (used in the Run() function)
|
||||
keybase Keybase // Some methods will need this, so I'm passing it but keeping it unexported
|
||||
}
|
||||
|
||||
type sender struct {
|
||||
|
||||
Reference in New Issue
Block a user