Add some return errors

This commit is contained in:
erroneousboat 2018-10-27 14:44:20 +02:00
parent 9485d2be5b
commit 457f47776b
4 changed files with 39 additions and 37 deletions

View File

@ -80,7 +80,10 @@ func CreateAppContext(flgConfig string, flgToken string, flgDebug bool, version
}
// Create the main view
view := views.CreateView(config, svc)
view, err := views.CreateView(config, svc)
if err != nil {
return nil, err
}
// Setup the interface
if flgDebug {

View File

@ -2,6 +2,7 @@ package handlers
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
@ -309,10 +310,15 @@ func actionSearchMode(ctx *context.AppContext) {
}
func actionGetMessages(ctx *context.AppContext) {
msgs := ctx.Service.GetMessages(
msgs, err := ctx.Service.GetMessages(
ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].ID,
ctx.View.Chat.GetMaxItems(),
)
if err != nil {
termbox.Close()
log.Println(err)
os.Exit(0)
}
ctx.View.Chat.SetMessages(msgs)
@ -385,10 +391,15 @@ func actionChangeChannel(ctx *context.AppContext) {
// Get messages of the SelectedChannel, and get the count of messages
// that fit into the Chat component
msgs := ctx.Service.GetMessages(
msgs, err := ctx.Service.GetMessages(
ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].ID,
ctx.View.Chat.GetMaxItems(),
)
if err != nil {
termbox.Close()
log.Println(err)
os.Exit(0)
}
// Set messages for the channel
ctx.View.Chat.SetMessages(msgs)

View File

@ -3,7 +3,6 @@ package service
import (
"errors"
"fmt"
"log"
"regexp"
"sort"
"strconv"
@ -69,7 +68,7 @@ func NewSlackService(config *config.Config) (*SlackService, error) {
return svc, nil
}
func (s *SlackService) GetChannels() []components.ChannelItem {
func (s *SlackService) GetChannels() ([]components.ChannelItem, error) {
slackChans := make([]slack.Channel, 0)
// Initial request
@ -86,7 +85,7 @@ func (s *SlackService) GetChannels() []components.ChannelItem {
},
)
if err != nil {
log.Fatal(err) // FIXME
return nil, err
}
slackChans = append(slackChans, initChans...)
@ -108,7 +107,7 @@ func (s *SlackService) GetChannels() []components.ChannelItem {
},
)
if err != nil {
log.Fatal(err) // FIXME
return nil, err
}
slackChans = append(slackChans, channels...)
@ -236,7 +235,7 @@ func (s *SlackService) GetChannels() []components.ChannelItem {
}
}
return chans
return chans, nil
}
// GetUserPresence will get the presence of a specific user
@ -251,31 +250,10 @@ func (s *SlackService) GetUserPresence(userID string) (string, error) {
// MarkAsRead will set the channel as read
func (s *SlackService) MarkAsRead(channelID string) {
// TODO: does this work with other channel types? See old one below,
// test this
s.Client.SetChannelReadMark(
channelID, fmt.Sprintf("%f",
float64(time.Now().Unix())),
)
// switch channel.Type {
// case ChannelTypeChannel:
// s.Client.SetChannelReadMark(
// channel.ID, fmt.Sprintf("%f",
// float64(time.Now().Unix())),
// )
// case ChannelTypeGroup:
// s.Client.SetGroupReadMark(
// channel.ID, fmt.Sprintf("%f",
// float64(time.Now().Unix())),
// )
// case ChannelTypeIM:
// s.Client.MarkIMChannel(
// channel.ID, fmt.Sprintf("%f",
// float64(time.Now().Unix())),
// )
// }
}
// SendMessage will send a message to a particular channel
@ -299,8 +277,8 @@ func (s *SlackService) SendMessage(channelID string, message string) error {
// GetMessages will get messages for a channel, group or im channel delimited
// by a count.
func (s *SlackService) GetMessages(channelID string, count int) []components.Message {
// TODO: check other parameters
func (s *SlackService) GetMessages(channelID string, count int) ([]components.Message, error) {
// https://godoc.org/github.com/nlopes/slack#GetConversationHistoryParameters
historyParams := slack.GetConversationHistoryParameters{
ChannelID: channelID,
@ -310,7 +288,7 @@ func (s *SlackService) GetMessages(channelID string, count int) []components.Mes
history, err := s.Client.GetConversationHistory(&historyParams)
if err != nil {
log.Fatal(err) // FIXME
return nil, err
}
// Construct the messages
@ -327,7 +305,7 @@ func (s *SlackService) GetMessages(channelID string, count int) []components.Mes
messagesReversed = append(messagesReversed, messages[i])
}
return messagesReversed
return messagesReversed, nil
}
// CreateMessage will create a string formatted message that can be rendered

View File

@ -17,7 +17,7 @@ type View struct {
Debug *components.Debug
}
func CreateView(config *config.Config, svc *service.SlackService) *View {
func CreateView(config *config.Config, svc *service.SlackService) (*View, error) {
// Create Input component
input := components.CreateInputComponent()
@ -25,19 +25,29 @@ func CreateView(config *config.Config, svc *service.SlackService) *View {
channels := components.CreateChannelsComponent(input.Par.Height)
// Channels: fill the component
slackChans := svc.GetChannels()
slackChans, err := svc.GetChannels()
if err != nil {
return nil, err
}
// Channels: set channels in component
channels.SetChannels(slackChans)
// Chat: create the component
chat := components.CreateChatComponent(input.Par.Height)
// Chat: fill the component
msgs := svc.GetMessages(
msgs, err := svc.GetMessages(
channels.ChannelItems[channels.SelectedChannel].ID,
chat.GetMaxItems(),
)
if err != nil {
return nil, err
}
// Chat: set messages in component
chat.SetMessages(msgs)
chat.SetBorderLabel(
channels.ChannelItems[channels.SelectedChannel].GetChannelName(),
)
@ -57,7 +67,7 @@ func CreateView(config *config.Config, svc *service.SlackService) *View {
Debug: debug,
}
return view
return view, nil
}
func (v *View) Refresh() {