Skip to content

Commit

Permalink
fix(GODT-1749): Convert panics to errors
Browse files Browse the repository at this point in the history
Converted most panics that can be handled properly as an error to error.
  • Loading branch information
LBeernaertProton committed Aug 25, 2022
1 parent c6e8af1 commit 9fd48f4
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 21 deletions.
17 changes: 11 additions & 6 deletions internal/backend/mailbox_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (m *Mailbox) fetchKeyword(msg *snapMsg, message *ent.Message, keyword proto
return response.ItemUID(msg.UID), nil

default:
panic("bad keyword")
return nil, fmt.Errorf("bad fetch keyword")
}
}

Expand Down Expand Up @@ -202,7 +202,12 @@ func (m *Mailbox) fetchBodyLiteral(body *proto.FetchBody, literal []byte) ([]byt
return nil, "", err
}

return b, renderSection(section.Section), nil
renderedSection, err := renderSection(section.Section)
if err != nil {
return nil, "", err
}

return b, renderedSection, nil

default:
return literal, "", nil
Expand Down Expand Up @@ -256,15 +261,15 @@ func (m *Mailbox) fetchBodySection(section *proto.BodySection, literal []byte) (
return root.Header(), nil

default:
panic("bad keyword")
return nil, fmt.Errorf("bad section keyword")
}

default:
return root.Body(), nil
}
}

func renderSection(section *proto.BodySection) string {
func renderSection(section *proto.BodySection) (string, error) {
var res []string

if len(section.Parts) > 0 {
Expand All @@ -290,11 +295,11 @@ func renderSection(section *proto.BodySection) string {
res = append(res, "MIME")

default:
panic("bad keyword")
return "", fmt.Errorf("bad body section keyword")
}
}

return strings.ToUpper(strings.Join(res, "."))
return strings.ToUpper(strings.Join(res, ".")), nil
}

func renderParts(sectionParts []int) string {
Expand Down
3 changes: 2 additions & 1 deletion internal/backend/mailbox_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backend
import (
"bytes"
"context"
"fmt"
"net/mail"
"strings"
"time"
Expand Down Expand Up @@ -157,7 +158,7 @@ func (m *Mailbox) matchSearchKey(ctx context.Context, candidates []*snapMsg, key
return m.matchSearchKeyList(ctx, candidates, key, decoder)

default:
panic("bad keyword")
return nil, fmt.Errorf("bad search keyword")
}
}

Expand Down
11 changes: 7 additions & 4 deletions internal/backend/seqset.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package backend

import "github.com/ProtonMail/gluon/internal/parser/proto"
import (
"fmt"
"github.com/ProtonMail/gluon/internal/parser/proto"
)

func toSeqSet(set *proto.SequenceSet) [][]string {
func toSeqSet(set *proto.SequenceSet) ([][]string, error) {
var seqRanges [][]string

for _, item := range set.GetItems() {
Expand All @@ -16,11 +19,11 @@ func toSeqSet(set *proto.SequenceSet) [][]string {
seqRange = append(seqRange, item.Range.GetBegin(), item.Range.GetEnd())

default:
panic("bad sequence range")
return nil, fmt.Errorf("bad sequence range")
}

seqRanges = append(seqRanges, seqRange)
}

return seqRanges
return seqRanges, nil
}
18 changes: 14 additions & 4 deletions internal/backend/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ func (snap *snapshot) getMessagesInRange(ctx context.Context, seq *proto.Sequenc
func (snap *snapshot) getMessagesInSeqRange(seq *proto.SequenceSet) ([]*snapMsg, error) {
var res []*snapMsg

for _, seqRange := range toSeqSet(seq) {
seqSet, err := toSeqSet(seq)
if err != nil {
return nil, err
}

for _, seqRange := range seqSet {
switch len(seqRange) {
case 1:
seq, err := snap.resolveSeq(seqRange[0])
Expand Down Expand Up @@ -138,7 +143,7 @@ func (snap *snapshot) getMessagesInSeqRange(seq *proto.SequenceSet) ([]*snapMsg,
res = append(res, snap.seqRange(begin, end)...)

default:
panic("bad sequence range")
return nil, fmt.Errorf("bad sequence range")
}
}

Expand All @@ -153,7 +158,12 @@ func (snap *snapshot) getMessagesInUIDRange(seq *proto.SequenceSet) ([]*snapMsg,
return nil, nil
}

for _, uidRange := range toSeqSet(seq) {
seqSet, err := toSeqSet(seq)
if err != nil {
return nil, err
}

for _, uidRange := range seqSet {
switch len(uidRange) {
case 1:
uid, err := snap.resolveUID(uidRange[0])
Expand Down Expand Up @@ -181,7 +191,7 @@ func (snap *snapshot) getMessagesInUIDRange(seq *proto.SequenceSet) ([]*snapMsg,
res = append(res, snap.uidRange(begin, end)...)

default:
panic("bad sequence range")
return nil, fmt.Errorf("bad sequence range")
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/backend/state_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func (state *State) actionRemoveMessageFlags(

func (state *State) actionSetMessageFlags(ctx context.Context, tx *ent.Tx, messageIDs []MessageIDPair, setFlags imap.FlagSet) error {
if setFlags.Contains(imap.FlagRecent) {
panic("recent flag is read-only")
return fmt.Errorf("recent flag is read-only")
}

curFlags := make(map[imap.MessageID]imap.FlagSet)
Expand Down
7 changes: 4 additions & 3 deletions internal/backend/state_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"context"
"fmt"

"github.com/ProtonMail/gluon/imap"
"github.com/ProtonMail/gluon/internal/backend/ent"
Expand All @@ -11,7 +12,7 @@ import (
// applyMessageFlagsAdded adds the flags to the given messages.
func (state *State) applyMessageFlagsAdded(ctx context.Context, tx *ent.Tx, messageIDs []imap.InternalMessageID, addFlags imap.FlagSet) error {
if addFlags.Contains(imap.FlagRecent) {
panic("the recent flag is read-only")
return fmt.Errorf("the recent flag is read-only")
}

client := tx.Client()
Expand Down Expand Up @@ -72,7 +73,7 @@ func (state *State) applyMessageFlagsAdded(ctx context.Context, tx *ent.Tx, mess
// applyMessageFlagsRemoved removes the flags from the given messages.
func (state *State) applyMessageFlagsRemoved(ctx context.Context, tx *ent.Tx, messageIDs []imap.InternalMessageID, remFlags imap.FlagSet) error {
if remFlags.Contains(imap.FlagRecent) {
panic("the recent flag is read-only")
return fmt.Errorf("the recent flag is read-only")
}

client := tx.Client()
Expand Down Expand Up @@ -139,7 +140,7 @@ func (state *State) applyMessageFlagsRemoved(ctx context.Context, tx *ent.Tx, me
// applyMessageFlagsSet sets the flags of the given messages.
func (state *State) applyMessageFlagsSet(ctx context.Context, tx *ent.Tx, messageIDs []imap.InternalMessageID, setFlags imap.FlagSet) error {
if setFlags.Contains(imap.FlagRecent) {
panic("the recent flag is read-only")
return fmt.Errorf("the recent flag is read-only")
}

if err := DBSetDeletedFlag(ctx, tx, state.snap.mboxID.InternalID, messageIDs, setFlags.Contains(imap.FlagDeleted)); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/user_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (user *user) removeState(ctx context.Context, stateID int) error {

state, ok := user.states[stateID]
if !ok {
panic("no such state")
return nil, fmt.Errorf("no such state")
}

messageIDs = xslices.Filter(messageIDs, func(messageID imap.InternalMessageID) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/user_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (user *user) apply(ctx context.Context, update imap.Update) error {
return user.applyMessageDeleted(ctx, update)

default:
panic("bad update")
return fmt.Errorf("bad update")
}
}

Expand Down

0 comments on commit 9fd48f4

Please sign in to comment.