Enhance logging and error handling in configuration loading, content fetching, and AI reply generation
This commit is contained in:
@@ -8,11 +8,13 @@ import (
|
||||
"mime/multipart"
|
||||
"net/mail"
|
||||
"paraclub-ai-mailer/config"
|
||||
"paraclub-ai-mailer/internal/logger"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/emersion/go-imap"
|
||||
"github.com/emersion/go-imap/client"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type IMAPClient struct {
|
||||
@@ -265,85 +267,141 @@ func (ic *IMAPClient) SaveDraft(email Email, response string) error {
|
||||
// extractMessageContent attempts to extract just the message content
|
||||
// by removing email headers and MIME boundaries
|
||||
func extractMessageContent(body string) string {
|
||||
logger.WithField("bodyLength", len(body)).Debug("Starting message content extraction")
|
||||
|
||||
msg, err := mail.ReadMessage(strings.NewReader(body))
|
||||
if err != nil {
|
||||
// Fallback to previous method if parsing fails
|
||||
logger.WithFields(logrus.Fields{
|
||||
"error": err,
|
||||
"bodyLength": len(body),
|
||||
}).Debug("Failed to parse email message, falling back to simple extraction")
|
||||
return fallbackExtractContent(body)
|
||||
}
|
||||
|
||||
mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
|
||||
contentTypeHeader := msg.Header.Get("Content-Type")
|
||||
logger.WithField("contentTypeHeader", contentTypeHeader).Debug("Got Content-Type header")
|
||||
|
||||
mediaType, params, err := mime.ParseMediaType(contentTypeHeader)
|
||||
if err != nil {
|
||||
logger.WithFields(logrus.Fields{
|
||||
"error": err,
|
||||
"contentTypeHeader": contentTypeHeader,
|
||||
}).Debug("Failed to parse Content-Type header, falling back to simple extraction")
|
||||
return fallbackExtractContent(body)
|
||||
}
|
||||
|
||||
logger.WithFields(logrus.Fields{
|
||||
"mediaType": mediaType,
|
||||
"params": params,
|
||||
}).Debug("Parsed message Content-Type")
|
||||
|
||||
if strings.HasPrefix(mediaType, "multipart/") {
|
||||
boundary := params["boundary"]
|
||||
if boundary == "" {
|
||||
logger.WithField("mediaType", mediaType).Debug("No boundary found in multipart message, falling back to simple extraction")
|
||||
return fallbackExtractContent(body)
|
||||
}
|
||||
|
||||
logger.WithFields(logrus.Fields{
|
||||
"mediaType": mediaType,
|
||||
"boundary": boundary,
|
||||
}).Debug("Processing multipart message")
|
||||
|
||||
reader := multipart.NewReader(msg.Body, boundary)
|
||||
var textContent string
|
||||
partIndex := 0
|
||||
|
||||
for {
|
||||
part, err := reader.NextPart()
|
||||
if err == io.EOF {
|
||||
logger.Debug("Finished processing all multipart parts")
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
logger.WithFields(logrus.Fields{
|
||||
"error": err,
|
||||
"partIndex": partIndex,
|
||||
}).Debug("Error reading multipart part, falling back to simple extraction")
|
||||
return fallbackExtractContent(body)
|
||||
}
|
||||
|
||||
// Check Content-Type of the part
|
||||
contentType := part.Header.Get("Content-Type")
|
||||
logger.WithFields(logrus.Fields{
|
||||
"partIndex": partIndex,
|
||||
"contentType": contentType,
|
||||
}).Debug("Processing message part")
|
||||
|
||||
if strings.HasPrefix(contentType, "text/plain") {
|
||||
buf := new(bytes.Buffer)
|
||||
_, err := buf.ReadFrom(part)
|
||||
if err != nil {
|
||||
logger.WithFields(logrus.Fields{
|
||||
"error": err,
|
||||
"partIndex": partIndex,
|
||||
}).Debug("Failed to read text/plain part")
|
||||
continue
|
||||
}
|
||||
textContent = buf.String()
|
||||
// Convert line endings to HTML breaks
|
||||
logger.WithFields(logrus.Fields{
|
||||
"partIndex": partIndex,
|
||||
"contentLength": len(textContent),
|
||||
}).Debug("Successfully extracted text/plain content")
|
||||
textContent = strings.ReplaceAll(textContent, "\n", "<br>\n")
|
||||
textContent = strings.ReplaceAll(textContent, "\r\n", "<br>\n")
|
||||
break
|
||||
}
|
||||
partIndex++
|
||||
}
|
||||
|
||||
if textContent != "" {
|
||||
return textContent // Remove TrimSpace to preserve formatting
|
||||
logger.WithField("contentLength", len(textContent)).Debug("Successfully extracted content from multipart message")
|
||||
return textContent
|
||||
}
|
||||
logger.Debug("No text/plain content found in multipart message, trying to read body directly")
|
||||
}
|
||||
|
||||
// For non-multipart messages, just read the body
|
||||
buf := new(bytes.Buffer)
|
||||
_, err = buf.ReadFrom(msg.Body)
|
||||
if err != nil {
|
||||
logger.WithFields(logrus.Fields{
|
||||
"error": err,
|
||||
"mediaType": mediaType,
|
||||
}).Debug("Failed to read message body, falling back to simple extraction")
|
||||
return fallbackExtractContent(body)
|
||||
}
|
||||
|
||||
content := buf.String()
|
||||
// Convert line endings to HTML breaks
|
||||
content = strings.ReplaceAll(content, "\r\n", "<br>\n")
|
||||
content = strings.ReplaceAll(content, "\n", "<br>\n")
|
||||
return content // Remove TrimSpace to preserve formatting
|
||||
logger.WithFields(logrus.Fields{
|
||||
"contentLength": len(content),
|
||||
"mediaType": mediaType,
|
||||
}).Debug("Successfully extracted content from message body")
|
||||
return content
|
||||
}
|
||||
|
||||
// fallbackExtractContent is the previous implementation used as fallback
|
||||
func fallbackExtractContent(body string) string {
|
||||
logger.WithField("bodyLength", len(body)).Debug("Using fallback content extraction method")
|
||||
parts := strings.Split(body, "\r\n\r\n")
|
||||
if len(parts) > 1 {
|
||||
content := strings.Join(parts[1:], "\r\n\r\n")
|
||||
// Convert line endings to HTML breaks
|
||||
content = strings.ReplaceAll(content, "\r\n", "<br>\n")
|
||||
content = strings.ReplaceAll(content, "\n", "<br>\n")
|
||||
return content // Remove TrimSpace to preserve formatting
|
||||
logger.WithFields(logrus.Fields{
|
||||
"contentLength": len(content),
|
||||
"partsCount": len(parts),
|
||||
}).Debug("Successfully extracted content using fallback method")
|
||||
return content
|
||||
}
|
||||
content := body
|
||||
// Convert line endings to HTML breaks
|
||||
content = strings.ReplaceAll(content, "\r\n", "<br>\n")
|
||||
content = strings.ReplaceAll(content, "\n", "<br>\n")
|
||||
return content // Remove TrimSpace to preserve formatting
|
||||
logger.WithFields(logrus.Fields{
|
||||
"contentLength": len(content),
|
||||
"fullBody": true,
|
||||
}).Debug("Using full body as content in fallback method")
|
||||
return content
|
||||
}
|
||||
|
||||
func (ic *IMAPClient) MarkAsProcessed(email Email) error {
|
||||
|
||||
Reference in New Issue
Block a user