Browse Source

Prevent errors from having no rows.

master
Syfaro 3 years ago
parent
commit
a8b605b19a

+ 7
- 2
commands/manage/count_items.go View File

@@ -2,6 +2,7 @@ package commands
2 2
 
3 3
 import (
4 4
 	"bytes"
5
+	"database/sql"
5 6
 	"github.com/syfaro/finch"
6 7
 	"github.com/syfaro/selectionsbot/database"
7 8
 	"gopkg.in/telegram-bot-api.v4"
@@ -32,8 +33,10 @@ func (cmd countItems) Execute(message tgbotapi.Message) error {
32 33
 		order by
33 34
 			id desc
34 35
 	`, message.Chat.ID)
35
-	if err != nil {
36
+	if err != nil && err != sql.ErrNoRows {
36 37
 		return err
38
+	} else if err == sql.ErrNoRows {
39
+		return cmd.QuickReply(message, "No currently active selections")
37 40
 	}
38 41
 
39 42
 	var items []database.SelectionVoteCount
@@ -51,8 +54,10 @@ func (cmd countItems) Execute(message tgbotapi.Message) error {
51 54
 		group by
52 55
 			selection_item_id
53 56
 	`, selection.ID)
54
-	if err != nil {
57
+	if err != nil && err != sql.ErrNoRows {
55 58
 		return err
59
+	} else if err == sql.ErrNoRows {
60
+		return cmd.QuickReply(message, "No votes were found")
56 61
 	}
57 62
 
58 63
 	b := bytes.Buffer{}

+ 4
- 2
commands/manage/end_selection.go View File

@@ -30,8 +30,10 @@ func (cmd endSelection) Execute(message tgbotapi.Message) error {
30 30
 			active = 1 and
31 31
 			chat_id = $1
32 32
 	`, message.Chat.ID)
33
-	if err != nil {
33
+	if err != nil && err != sql.ErrNoRows {
34 34
 		return err
35
+	} else if err == sql.ErrNoRows {
36
+		return cmd.QuickReply(message, "No currently active selections")
35 37
 	}
36 38
 
37 39
 	u := database.User{}
@@ -48,7 +50,7 @@ func (cmd endSelection) Execute(message tgbotapi.Message) error {
48 50
 			where
49 51
 				chat_id = $1
50 52
 	`, message.Chat.ID)
51
-	if err != nil {
53
+	if err != nil && err != sql.ErrNoRows {
52 54
 		return err
53 55
 	}
54 56
 

+ 12
- 7
commands/manage/select_item.go View File

@@ -30,11 +30,10 @@ func (cmd selectItem) Execute(message tgbotapi.Message) error {
30 30
 			chat_id = $1 and
31 31
 			active = 1
32 32
 	`, message.Chat.ID)
33
-	if err == sql.ErrNoRows {
34
-		cmd.QuickReply(message, "There are no active selections")
35
-		return nil
36
-	} else if err != nil {
33
+	if err != nil && err != sql.ErrNoRows {
37 34
 		return err
35
+	} else if err == sql.ErrNoRows {
36
+		return cmd.QuickReply(message, "No currently active selections")
38 37
 	}
39 38
 
40 39
 	var items []database.SelectionItem
@@ -46,8 +45,10 @@ func (cmd selectItem) Execute(message tgbotapi.Message) error {
46 45
 		where
47 46
 			selection_id = $1
48 47
 	`, selection.ID)
49
-	if err != nil {
48
+	if err != nil && err != sql.ErrNoRows {
50 49
 		return err
50
+	} else if err == sql.ErrNoRows {
51
+		return cmd.QuickReply(message, "No selection items were found")
51 52
 	}
52 53
 
53 54
 	var itemList [][]tgbotapi.KeyboardButton
@@ -100,8 +101,10 @@ func (cmd selectItem) ExecuteWaiting(message tgbotapi.Message) error {
100 101
 			chat_id = $1 and
101 102
 			active = 1
102 103
 	`, message.Chat.ID)
103
-	if err != nil {
104
+	if err != nil && err != sql.ErrNoRows {
104 105
 		return err
106
+	} else if err == sql.ErrNoRows {
107
+		return cmd.QuickReply(message, "No currently active selections")
105 108
 	}
106 109
 
107 110
 	var item database.SelectionItem
@@ -114,8 +117,10 @@ func (cmd selectItem) ExecuteWaiting(message tgbotapi.Message) error {
114 117
 			item = $1 and
115 118
 			selection_id = $2
116 119
 	`, message.Text, selection.ID)
117
-	if err != nil {
120
+	if err != nil && err != sql.ErrNoRows {
118 121
 		return err
122
+	} else if err == sql.ErrNoRows {
123
+		return cmd.QuickReply(message, "No selection items were found")
119 124
 	}
120 125
 
121 126
 	_, err = database.DB.Exec(`

+ 10
- 3
commands/manage/selection_list.go View File

@@ -2,6 +2,7 @@ package commands
2 2
 
3 3
 import (
4 4
 	"bytes"
5
+	"database/sql"
5 6
 	"github.com/syfaro/finch"
6 7
 	"github.com/syfaro/selectionsbot/database"
7 8
 	"gopkg.in/telegram-bot-api.v4"
@@ -32,8 +33,10 @@ func (cmd selectionList) Execute(message tgbotapi.Message) error {
32 33
 		order by
33 34
 			id desc
34 35
 	`, message.Chat.ID)
35
-	if err != nil {
36
+	if err != nil && err != sql.ErrNoRows {
36 37
 		return err
38
+	} else if err == sql.ErrNoRows {
39
+		return cmd.QuickReply(message, "No currently active selections")
37 40
 	}
38 41
 
39 42
 	var items []database.SelectionItem
@@ -45,8 +48,10 @@ func (cmd selectionList) Execute(message tgbotapi.Message) error {
45 48
 		where
46 49
 			selection_id = $1
47 50
 	`, selection.ID)
48
-	if err != nil {
51
+	if err != nil && err != sql.ErrNoRows {
49 52
 		return err
53
+	} else if err == sql.ErrNoRows {
54
+		return cmd.QuickReply(message, "No items were found")
50 55
 	}
51 56
 
52 57
 	b := bytes.Buffer{}
@@ -68,8 +73,10 @@ func (cmd selectionList) Execute(message tgbotapi.Message) error {
68 73
 				selection_id = $1 and
69 74
 				selection_item_id = $2
70 75
 		`, selection.ID, item.ID)
71
-		if err != nil {
76
+		if err != nil && err != sql.ErrNoRows {
72 77
 			return err
78
+		} else if err == sql.ErrNoRows {
79
+			return cmd.QuickReply(message, "No votes were found")
73 80
 		}
74 81
 
75 82
 		var users []string

Loading…
Cancel
Save