Skip to content

Commit fb2441d

Browse files
committed
Improve error handling in dispatcher
1 parent 9bb5406 commit fb2441d

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

internal/dispatcher/notification.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notificatio
7373

7474
evt, err := d.mautrixClient.SendMessageEvent(mId.RoomID(a.MatrixID), event.EventMessage, &messageEvent)
7575
if err != nil {
76+
log.L.Errorln(err)
7677
return "", err
7778
}
7879

79-
return evt.EventID.String(), err
80+
return evt.EventID.String(), nil
8081
}
8182

8283
// DeleteNotification sends a notification to the specified user that another notificaion is deleted
@@ -85,20 +86,19 @@ func (d *Dispatcher) DeleteNotification(a *model.Application, n *model.DeleteNot
8586
var oldFormattedBody string
8687
var oldBody string
8788

88-
// get the message we want to delete
89+
// Get the message we want to delete
8990
deleteMessage, err := d.getMessage(a, n.ID)
9091
if err != nil {
9192
log.L.Println(err)
9293
return pberrors.ErrorMessageNotFound
9394
}
9495

9596
oldBody, oldFormattedBody, err = bodiesFromMessage(deleteMessage)
96-
9797
if err != nil {
9898
return err
9999
}
100100

101-
// update the message with strikethrough
101+
// Update the message with strikethrough
102102
newBody := fmt.Sprintf("<del>%s</del>\n- deleted", oldBody)
103103
newFormattedBody := fmt.Sprintf("<del>%s</del><br>- deleted", oldFormattedBody)
104104

@@ -142,7 +142,7 @@ func (d *Dispatcher) getFormattedMessage(n *model.Notification) string {
142142
case "html", "text/html":
143143
message = strings.Replace(trimmedMessage, "\n", "<br />", -1)
144144
case "markdown", "md", "text/md", "text/markdown":
145-
// allow HTML in Markdown
145+
// Allow HTML in Markdown
146146
message = string(markdown.ToHTML([]byte(trimmedMessage), nil, nil))
147147
}
148148
}
@@ -183,18 +183,23 @@ func (d *Dispatcher) coloredText(color string, text string) string {
183183
func (d *Dispatcher) getMessage(a *model.Application, id string) (*event.Event, error) {
184184
start := ""
185185
end := ""
186-
maxPages := 10 // maximum pages to request (10 messages per page)
186+
maxPages := 10 // Maximum pages to request (10 messages per page)
187187

188188
for i := 0; i < maxPages; i++ {
189-
messages, _ := d.mautrixClient.Messages(mId.RoomID(a.MatrixID), start, end, 'b', nil, 10)
189+
messages, err := d.mautrixClient.Messages(mId.RoomID(a.MatrixID), start, end, 'b', nil, 10)
190+
if err != nil {
191+
return nil, err
192+
}
193+
190194
for _, event := range messages.Chunk {
191195
if event.ID.String() == id {
192196
return event, nil
193197
}
194198
}
195199
start = messages.End
196200
}
197-
return &event.Event{}, pberrors.ErrorMessageNotFound
201+
202+
return nil, pberrors.ErrorMessageNotFound
198203
}
199204

200205
// Replaces the content of a matrix message
@@ -222,7 +227,7 @@ func (d *Dispatcher) replaceMessage(a *model.Application, newBody, newFormattedB
222227

223228
sendEvent, err := d.mautrixClient.SendMessageEvent(mId.RoomID(a.MatrixID), event.EventMessage, &replaceEvent)
224229
if err != nil {
225-
log.L.Println(err)
230+
log.L.Errorln(err)
226231
return nil, err
227232
}
228233

@@ -236,7 +241,7 @@ func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody
236241
return nil, err
237242
}
238243

239-
// formatting according to https://matrix.org/docs/spec/client_server/latest#fallbacks-and-event-representation
244+
// Formatting according to https://matrix.org/docs/spec/client_server/latest#fallbacks-and-event-representation
240245
newFormattedBody := fmt.Sprintf("<mx-reply><blockquote><a href='https://matrix.to/#/%s/%s'>In reply to</a> <a href='https://matrix.to/#/%s'>%s</a><br />%s</blockquote>\n</mx-reply>%s", respondMessage.RoomID, respondMessage.ID, respondMessage.Sender, respondMessage.Sender, oldFormattedBody, formattedBody)
241246
newBody := fmt.Sprintf("> <%s>%s\n\n%s", respondMessage.Sender, oldBody, body)
242247

@@ -255,7 +260,13 @@ func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody
255260
}
256261
notificationEvent.RelatesTo = &notificationRelation
257262

258-
return d.mautrixClient.SendMessageEvent(mId.RoomID(a.MatrixID), event.EventMessage, &notificationEvent)
263+
sendEvent, err := d.mautrixClient.SendMessageEvent(mId.RoomID(a.MatrixID), event.EventMessage, &notificationEvent)
264+
if err != nil {
265+
log.L.Errorln(err)
266+
return nil, err
267+
}
268+
269+
return sendEvent, nil
259270
}
260271

261272
// Extracts body and formatted body from a matrix message event

0 commit comments

Comments
 (0)