Move error handling into own function

This commit is contained in:
ekzyis 2024-10-16 19:55:26 +02:00
parent b6991ce3e5
commit c1ad076828
1 changed files with 23 additions and 24 deletions

47
main.go
View File

@ -59,19 +59,7 @@ func tickGameStart(c *sn.Client) {
}
if err = handleGameStart(&n.Item); err != nil {
// don't reply to mentions that we failed to parse as a game start
// to support unrelated mentions
if err.Error() == "failed to parse game start" {
log.Printf("ignoring error for item %d: %v\n", n.Item.Id, err)
return
}
if _, err2 := createComment(n.Item.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
log.Printf("failed to reply with error to item %d: %v\n", n.Item.Id, err2)
} else {
log.Printf("replied to game start in item %d with error: %v\n", n.Item.Id, err)
}
handleError(&n.Item, err)
} else {
log.Printf("started new game via item %d\n", n.Item.Id)
}
@ -116,17 +104,7 @@ func tickGameProgress(c *sn.Client) {
}
if err = handleGameProgress(&n.Item); err != nil {
if err.Error() == "failed to parse game update" {
log.Printf("ignoring error for item %d: %v\n", n.Item.Id, err)
return
}
if _, err2 := createComment(n.Item.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
log.Printf("failed to reply with error to item %d: %v\n", n.Item.Id, err2)
} else {
log.Printf("replied to game start in item %d with error: %v\n", n.Item.Id, err)
}
handleError(&n.Item, err)
} else {
log.Printf("updated game via item %d\n", n.Item.Id)
}
@ -241,6 +219,27 @@ func handleGameProgress(req *sn.Item) error {
return nil
}
func handleError(req *sn.Item, err error) {
// don't reply to mentions that we failed to parse as a game start
// to support unrelated mentions
if err.Error() == "failed to parse game start" {
log.Printf("ignoring error for item %d: %v\n", req.Id, err)
return
}
if err.Error() == "failed to parse game update" {
log.Printf("ignoring error for item %d: %v\n", req.Id, err)
return
}
if _, err2 := createComment(req.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
log.Printf("failed to reply with error to item %d: %v\n", req.Id, err2)
} else {
log.Printf("replied to game start in item %d with error: %v\n", req.Id, err)
}
}
func createComment(parentId int, text string) (*sn.Item, error) {
var (
commentId int