Skip to content

Commit 6e4f2be

Browse files
fix: replace panic with fallback in ID generation (gastownhall#213)
Replace panic calls in generateID() and generateThreadID() with time-based fallback when crypto/rand.Read fails. This is an extremely rare error case, but panicking is not the right behavior for ID generation functions. 🤝 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c8150ab commit 6e4f2be

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

internal/mail/types.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package mail
44
import (
55
"crypto/rand"
66
"encoding/hex"
7+
"fmt"
78
"strings"
89
"time"
910
)
@@ -142,19 +143,23 @@ func NewReplyMessage(from, to, subject, body string, original *Message) *Message
142143
}
143144

144145
// generateID creates a random message ID.
146+
// Falls back to time-based ID if crypto/rand fails (extremely rare).
145147
func generateID() string {
146148
b := make([]byte, 8)
147149
if _, err := rand.Read(b); err != nil {
148-
panic("crypto/rand.Read failed: " + err.Error())
150+
// Fallback to time-based ID instead of panicking
151+
return fmt.Sprintf("msg-%x", time.Now().UnixNano())
149152
}
150153
return "msg-" + hex.EncodeToString(b)
151154
}
152155

153156
// generateThreadID creates a random thread ID.
157+
// Falls back to time-based ID if crypto/rand fails (extremely rare).
154158
func generateThreadID() string {
155159
b := make([]byte, 6)
156160
if _, err := rand.Read(b); err != nil {
157-
panic("crypto/rand.Read failed: " + err.Error())
161+
// Fallback to time-based ID instead of panicking
162+
return fmt.Sprintf("thread-%x", time.Now().UnixNano())
158163
}
159164
return "thread-" + hex.EncodeToString(b)
160165
}

0 commit comments

Comments
 (0)