add utils.UnwrapError() to provide meaningful error messages

This commit is contained in:
Aine
2022-08-27 22:38:23 +03:00
parent d1c48b9b31
commit 6eae1a65c4
3 changed files with 30 additions and 3 deletions

View File

@@ -68,7 +68,7 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
content := email2content(email, cfg, threadID) content := email2content(email, cfg, threadID)
eventID, serr := b.lp.Send(roomID, content) eventID, serr := b.lp.Send(roomID, content)
if serr != nil { if serr != nil {
return serr return utils.UnwrapError(serr)
} }
if threadID == "" && !cfg.NoThreads() { if threadID == "" && !cfg.NoThreads() {

View File

@@ -112,9 +112,9 @@ func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
} }
} }
return config, err return config, utils.UnwrapError(err)
} }
func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error { func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg) return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg))
} }

View File

@@ -1,6 +1,7 @@
package utils package utils
import ( import (
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event" "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
) )
@@ -24,3 +25,29 @@ func RelatesTo(noThreads bool, parentID id.EventID) *event.RelatesTo {
EventID: parentID, EventID: parentID,
} }
} }
// UnwrapError tries to unwrap a error into something meaningful, like mautrix.HTTPError or mautrix.RespError
func UnwrapError(err error) error {
switch err.(type) {
case nil:
return nil
case mautrix.HTTPError:
return unwrapHTTPError(err)
default:
return err
}
}
func unwrapHTTPError(err error) error {
httperr, ok := err.(mautrix.HTTPError)
if !ok {
return err
}
uwerr := httperr.Unwrap()
if uwerr != nil {
return uwerr
}
return httperr
}