Skip to content

Commit cfbb604

Browse files
authored
fix(pop3): wrap underlying errors (#1320)
## What? `findAttachmentData` in `backend/pop3/pop3.go` had three error sites that dropped underlying context: - `gomail.CreateReader` returned an error but the wrapping call discarded it with a static `"not a multipart message"` string. - The `mr.NextPart()` loop broke on a non-EOF error without saving it, so the final return at the bottom of the function reported "not found" instead of the real read failure. - The not-found error had no count of how many parts the loop actually scanned, which made the failure hard to debug from a log line alone. This PR wraps the `CreateReader` error with `%w`, captures the `NextPart` error in a local `scanErr` and surfaces it as the final error when it is set, and includes the scanned-parts count in the not-found message. ## Why? Fixes #688. The reporter noted the missing `%w` wrapping at the original line 438. While poking at that site I noticed the silent `break` on `mr.NextPart()` errors right above it. Those two paths share the same downstream "not found" return, so a multipart parse error in the middle of the stream looks identical to a genuinely missing part. The third change (scanned-parts count) is the smallest possible nudge that makes the two failure modes distinguishable in an error string without changing the return type or call sites. Fixes #688
1 parent 2642736 commit cfbb604

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

backend/pop3/pop3.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,16 +465,18 @@ func parseMessageBody(r io.Reader) (string, string, []backend.Attachment, error)
465465
func findAttachmentData(r io.Reader, targetPartID string) ([]byte, error) {
466466
mr, err := gomail.CreateReader(r)
467467
if err != nil {
468-
return nil, fmt.Errorf("not a multipart message")
468+
return nil, fmt.Errorf("pop3: not a multipart message: %w", err)
469469
}
470470

471471
partIdx := 0
472+
var scanErr error
472473
for {
473474
part, err := mr.NextPart()
474475
if err == io.EOF {
475476
break
476477
}
477478
if err != nil {
479+
scanErr = err
478480
break
479481
}
480482
partIdx++
@@ -484,5 +486,9 @@ func findAttachmentData(r io.Reader, targetPartID string) ([]byte, error) {
484486
}
485487
}
486488

487-
return nil, fmt.Errorf("pop3: attachment part %s not found", targetPartID)
489+
if scanErr != nil {
490+
return nil, fmt.Errorf("pop3: failed to scan attachment parts: %w", scanErr)
491+
}
492+
493+
return nil, fmt.Errorf("pop3: attachment part %s not found (scanned %d parts)", targetPartID, partIdx)
488494
}

0 commit comments

Comments
 (0)