From 6e4f2bea299521b0b32a7ad642117d3d6b96ca3c Mon Sep 17 00:00:00 2001 From: Cong <7380929+robotlearning123@users.noreply.github.com> Date: Tue, 6 Jan 2026 00:33:40 -0500 Subject: [PATCH] fix: replace panic with fallback in ID generation (#213) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- internal/mail/types.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/mail/types.go b/internal/mail/types.go index 58415f5a..76339ca4 100644 --- a/internal/mail/types.go +++ b/internal/mail/types.go @@ -4,6 +4,7 @@ package mail import ( "crypto/rand" "encoding/hex" + "fmt" "strings" "time" ) @@ -142,19 +143,23 @@ func NewReplyMessage(from, to, subject, body string, original *Message) *Message } // generateID creates a random message ID. +// Falls back to time-based ID if crypto/rand fails (extremely rare). func generateID() string { b := make([]byte, 8) if _, err := rand.Read(b); err != nil { - panic("crypto/rand.Read failed: " + err.Error()) + // Fallback to time-based ID instead of panicking + return fmt.Sprintf("msg-%x", time.Now().UnixNano()) } return "msg-" + hex.EncodeToString(b) } // generateThreadID creates a random thread ID. +// Falls back to time-based ID if crypto/rand fails (extremely rare). func generateThreadID() string { b := make([]byte, 6) if _, err := rand.Read(b); err != nil { - panic("crypto/rand.Read failed: " + err.Error()) + // Fallback to time-based ID instead of panicking + return fmt.Sprintf("thread-%x", time.Now().UnixNano()) } return "thread-" + hex.EncodeToString(b) }