fix: consistent error reporting + reset mapping modal per account

Error consistency: folder-level failures were counted only toward the task's
done_with_errors status, not the account's error_count, so a task showed
DONE_WITH_ERRORS while its only account showed 0 errors / DONE. Now folder
errors increment the account counter and the account status becomes
done_with_errors when errs>0.

Visibility: persist accounts.last_error (migration 0002) so the failing folder
/ login error survives a page reload (shown red under the source login);
cleared at the start of each run.

Modal reset: the folder-mapping modal kept its selections across opens, so
adding a second account showed the first account's mapping. It now mounts
fresh per add (conditional render + key), reflecting the newly-probed folders.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MMHQTtnQtQqL8muAXHr9kd
This commit is contained in:
2026-07-02 15:14:11 +07:00
parent cdb93926bf
commit 477077d9e3
8 changed files with 59 additions and 15 deletions
+2
View File
@@ -21,6 +21,7 @@ type AccountView struct {
Copied int64 `json:"copied"`
Skipped int64 `json:"skipped"`
Errors int64 `json:"errors"`
LastError string `json:"last_error,omitempty"`
}
func accountDTO(a store.Account) AccountView {
@@ -28,6 +29,7 @@ func accountDTO(a store.Account) AccountView {
ID: a.ID, SrcLogin: a.SrcLogin, DstLogin: a.DstLogin,
TestSrcStatus: a.TestSrcStatus, TestDstStatus: a.TestDstStatus,
Status: a.Status, Copied: a.Copied, Skipped: a.Skipped, Errors: a.Errors,
LastError: a.LastError,
}
}
+14 -4
View File
@@ -217,6 +217,7 @@ func (o *Orchestrator) runAccount(ctx context.Context, task store.Task, runID in
"dst_login": a.DstLogin, "dst_host": dstEP.Host, "dst_port": dstEP.Port,
}})
_ = o.store.SetAccountStatus(ctx, a.ID, "running")
_ = o.store.SetAccountError(ctx, a.ID, "") // clear any error from a previous run
// Per-account cancellable context: IMAP work uses actx (so CancelAccount
// stops it); DB writes keep the parent ctx so status/counters persist even
@@ -349,19 +350,23 @@ func (o *Orchestrator) runAccount(ctx context.Context, task store.Task, runID in
break // cancelled — stop scheduling more folders
}
res, err := imapx.CopyFolder(actx, src, dst, fp.src, fp.dst, deps)
folderErr := int64(0)
if err != nil && actx.Err() == nil {
slog.Warn("folder copy error", "account", a.ID, "src_login", a.SrcLogin, "folder", fp.src, "err", err)
errs++
folderErr = 1
_ = o.store.SetAccountError(ctx, a.ID, "folder \""+fp.src+"\": "+err.Error())
o.hub.Publish(wshub.Event{Type: "error", TaskID: task.ID, Data: map[string]any{
"account_id": a.ID, "src_login": a.SrcLogin, "folder": fp.src, "error": err.Error(),
}})
}
copied += int64(res.Copied)
skipped += int64(res.Skipped)
errs += int64(res.Errors)
errs += int64(res.Errors) + folderErr
baseCopied += int64(res.Copied)
baseSkipped += int64(res.Skipped)
_ = o.store.IncAccountCounters(ctx, a.ID, int64(res.Copied), int64(res.Skipped), int64(res.Errors))
// Persist message-level AND folder-level errors so the account row's
// error count matches the task status (done_with_errors).
_ = o.store.IncAccountCounters(ctx, a.ID, int64(res.Copied), int64(res.Skipped), int64(res.Errors)+folderErr)
}
if actx.Err() != nil {
@@ -373,7 +378,11 @@ func (o *Orchestrator) runAccount(ctx context.Context, task store.Task, runID in
return copied, skipped, errs
}
_ = o.store.SetAccountStatus(ctx, a.ID, "done")
acctStatus := "done"
if errs > 0 {
acctStatus = "done_with_errors"
}
_ = o.store.SetAccountStatus(ctx, a.ID, acctStatus)
o.hub.Publish(wshub.Event{Type: "account_done", TaskID: task.ID,
Data: map[string]any{"account_id": a.ID, "src_login": a.SrcLogin, "dst_login": a.DstLogin,
"copied": copied, "skipped": skipped, "errors": errs}})
@@ -395,6 +404,7 @@ func (o *Orchestrator) accountFailed(ctx context.Context, taskID int64, a store.
}
slog.Error("account failed", "account", a.ID, "side", side, "login", login, "host", host, "port", port, "err", err)
_ = o.store.SetAccountStatus(ctx, a.ID, "error")
_ = o.store.SetAccountError(ctx, a.ID, side+" "+login+"@"+host+": "+err.Error())
o.hub.Publish(wshub.Event{Type: "error", TaskID: taskID,
Data: map[string]any{"account_id": a.ID, "side": side, "login": login, "host": host, "port": port, "error": err.Error()}})
return 0, 0, 1
+10 -2
View File
@@ -18,6 +18,7 @@ type Account struct {
Copied int64
Skipped int64
Errors int64
LastError string
}
func (s *Store) CreateAccount(ctx context.Context, a Account) (int64, error) {
@@ -38,7 +39,7 @@ func (s *Store) DeleteAccount(ctx context.Context, id int64) error {
func (s *Store) ListAccountsByTask(ctx context.Context, taskID int64) ([]Account, error) {
rows, err := s.Pool.Query(ctx,
`SELECT id, task_id, src_login, src_pass_enc, dst_login, dst_pass_enc,
test_src_status, test_dst_status, status, copied_count, skipped_count, error_count
test_src_status, test_dst_status, status, copied_count, skipped_count, error_count, last_error
FROM accounts WHERE task_id=$1 ORDER BY id`, taskID)
if err != nil {
return nil, err
@@ -48,7 +49,7 @@ func (s *Store) ListAccountsByTask(ctx context.Context, taskID int64) ([]Account
for rows.Next() {
var a Account
if err := rows.Scan(&a.ID, &a.TaskID, &a.SrcLogin, &a.SrcPassEnc, &a.DstLogin, &a.DstPassEnc,
&a.TestSrcStatus, &a.TestDstStatus, &a.Status, &a.Copied, &a.Skipped, &a.Errors); err != nil {
&a.TestSrcStatus, &a.TestDstStatus, &a.Status, &a.Copied, &a.Skipped, &a.Errors, &a.LastError); err != nil {
return nil, err
}
out = append(out, a)
@@ -56,6 +57,13 @@ func (s *Store) ListAccountsByTask(ctx context.Context, taskID int64) ([]Account
return out, rows.Err()
}
// SetAccountError stores (or clears, with "") the last error message shown for
// an account, so it survives a page reload after the run's live log is gone.
func (s *Store) SetAccountError(ctx context.Context, id int64, msg string) error {
_, err := s.Pool.Exec(ctx, `UPDATE accounts SET last_error=$2 WHERE id=$1`, id, msg)
return err
}
// side = "src" | "dst"
func (s *Store) SetAccountTestStatus(ctx context.Context, id int64, side, status string) error {
col := "test_src_status"