Improve [Imap] [Client] Error Handling #16
@ -5,7 +5,11 @@
|
|||||||
|
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import "github.com/emersion/go-imap/client"
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/emersion/go-imap/client"
|
||||||
|
)
|
||||||
|
|
||||||
// Config holds the configuration for the IMAP client
|
// Config holds the configuration for the IMAP client
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@ -35,3 +39,11 @@ type IMAPClient struct {
|
|||||||
config *Config
|
config *Config
|
||||||
client *client.Client
|
client *client.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrorsClientIsNotConnected is returned when an operation is attempted on a client that is not connected.
|
||||||
|
ErrorsClientIsNotConnected = errors.New("client is not connected")
|
||||||
|
|
||||||
|
// ErrorsFailedToReadBody is returned when there is a failure in reading the body of a message.
|
||||||
|
ErrorsFailedToReadBody = errors.New("failed to read body")
|
||||||
|
)
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
// ExportMessagesTo uses the provided exporter to write messages to the writer
|
// ExportMessagesTo uses the provided exporter to write messages to the writer
|
||||||
func (c *IMAPClient) ExportMessagesTo(writer io.Writer, exporter export.Exporter, mailbox string, numMessages uint32, config MessageConfig) error {
|
func (c *IMAPClient) ExportMessagesTo(writer io.Writer, exporter export.Exporter, mailbox string, numMessages uint32, config MessageConfig) error {
|
||||||
if c.client == nil {
|
if c.client == nil {
|
||||||
return fmt.Errorf("client is not connected")
|
return ErrorsClientIsNotConnected
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch messages based on the provided MessageConfig and number of messages
|
// Fetch messages based on the provided MessageConfig and number of messages
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
// ListMailboxes retrieves all available mailboxes for the connected user
|
// ListMailboxes retrieves all available mailboxes for the connected user
|
||||||
func (c *IMAPClient) ListMailboxes() ([]string, error) {
|
func (c *IMAPClient) ListMailboxes() ([]string, error) {
|
||||||
if c.client == nil {
|
if c.client == nil {
|
||||||
return nil, fmt.Errorf("client is not connected")
|
return nil, ErrorsClientIsNotConnected
|
||||||
}
|
}
|
||||||
|
|
||||||
mailboxes := make(chan *imap.MailboxInfo, 10)
|
mailboxes := make(chan *imap.MailboxInfo, 10)
|
||||||
|
@ -41,7 +41,7 @@ const (
|
|||||||
// ListMessages lists the messages in the specified mailbox based on the MessageConfig
|
// ListMessages lists the messages in the specified mailbox based on the MessageConfig
|
||||||
func (c *IMAPClient) ListMessages(mailbox string, numMessages uint32, config MessageConfig) ([]map[string]any, error) {
|
func (c *IMAPClient) ListMessages(mailbox string, numMessages uint32, config MessageConfig) ([]map[string]any, error) {
|
||||||
if c.client == nil {
|
if c.client == nil {
|
||||||
return nil, fmt.Errorf("client is not connected")
|
return nil, ErrorsClientIsNotConnected
|
||||||
}
|
}
|
||||||
|
|
||||||
mbox, err := c.selectMailbox(mailbox)
|
mbox, err := c.selectMailbox(mailbox)
|
||||||
@ -194,7 +194,7 @@ func (c *IMAPClient) extractBody(body map[*imap.BodySectionName]imap.Literal) (s
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
bytebufferpool.Put(buf)
|
bytebufferpool.Put(buf)
|
||||||
return "", fmt.Errorf("failed to read body")
|
return "", ErrorsFailedToReadBody
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user