Skip to content

Commit

Permalink
Check RocketChat response for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
krakowski committed Oct 30, 2020
1 parent 5db474f commit 40a25cb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions api/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ type AttachmentField struct {
Title string `json:"title" yaml:"title"`
Value string `json:"value" yaml:"value"`
}

type MessageResponse struct {
Channel string `json:"channel"`
Success bool `json:"success"`
}
13 changes: 11 additions & 2 deletions api/message_post.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package rocket

import "errors"

const (
postMessagePath = "api/v1/chat.postMessage"
)

func (message *MessageService) Post(payload MessagePayload) error {
func (message *MessageService) Post(payload MessagePayload) (*MessageResponse, error) {

var response MessageResponse
_, err := message.resty.R().
SetBody(payload).
SetResult(&response).
Post(postMessagePath)

return err
if !response.Success {
return nil, errors.New("Posting message failed")
}

return &response, err
}
5 changes: 5 additions & 0 deletions api/rocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type ClientOptions struct {
}

type Client struct {

CurrentUser string

common service

Auth *AuthService
Expand Down Expand Up @@ -60,5 +63,7 @@ func NewClient(client *http.Client, options ClientOptions) (*Client, error) {
return nil, err
}

ret.CurrentUser = options.Credentials.Username
return ret, nil
}

11 changes: 6 additions & 5 deletions cmd/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var notifyCommand = &cobra.Command{
os.Exit(1)
}

spin.StopSuccess(util.NoMessage)
spin.StopSuccess("Success")

spin = util.StartSpinner("Authenticating with RocketChat")

Expand All @@ -74,18 +74,19 @@ var notifyCommand = &cobra.Command{
os.Exit(1)
}

spin.StopSuccess(util.NoMessage)
spin.StopSuccess("Logged in as " + client.CurrentUser)

spin = util.StartSpinner("Sending notification to channel " + payload.Channel)
spin = util.StartSpinner("Sending notification")

// Post the message
if err = client.Message.Post(payload); err != nil {
resp, err := client.Message.Post(payload)
if err != nil {
spin.StopError(err)
fmt.Printf(" - %s\n", rocket.LastError.Message)
os.Exit(1)
}

spin.StopSuccess(util.NoMessage)
spin.StopSuccess("Sent to channel " + resp.Channel)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const (
version = "1.0.1"
version = "1.0.2"
)

var rootCommand = &cobra.Command{
Expand Down

0 comments on commit 40a25cb

Please sign in to comment.