refactor(jmap): improve errors management

This commit is contained in:
Clément DOUIN
2026-03-31 09:13:41 +02:00
parent 2afbc89d3e
commit 9d46dfd0a2
17 changed files with 178 additions and 138 deletions
+13 -8
View File
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use clap::Parser;
use io_jmap::{
rfc8621::coroutines::email_copy::{JmapEmailCopy, JmapEmailCopyResult},
@@ -67,20 +67,25 @@ impl JmapEmailCopyCommand {
};
if !not_created.is_empty() {
let mut ctx = anyhow!("Copy JMAP email(s) error");
let mut msg = String::from("Copy JMAP email(s) error");
for (id, err) in not_created {
if let Some(desc) = &err.description {
ctx = anyhow!("{id}: {desc}").context(ctx);
}
msg.push_str(&format!("\n `{id}`"));
if !err.properties.is_empty() {
let props = err.properties.join(", ");
ctx = anyhow!("{id}: Invalid properties {props}").context(ctx);
msg.push_str(": invalid properties `");
msg.push_str(&err.properties.join("`, `"));
msg.push('`');
}
if let Some(desc) = &err.description {
msg.push_str(" (");
msg.push_str(desc.to_lowercase().trim_end_matches(['.', '\n']));
msg.push(')');
}
}
bail!(ctx)
bail!(msg)
}
printer.out(Message::new("Email(s) successfully copied"))
+13 -8
View File
@@ -1,4 +1,4 @@
use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use clap::Parser;
use io_jmap::rfc8621::coroutines::email_set::{JmapEmailSet, JmapEmailSetArgs, JmapEmailSetResult};
use io_stream::runtimes::std::handle;
@@ -36,20 +36,25 @@ impl JmapEmailDestroyCommand {
};
if !not_destroyed.is_empty() {
let mut ctx = anyhow!("Destroy JMAP email(s) error");
let mut msg = String::from("Destroy JMAP email(s) error");
for (id, err) in not_destroyed {
if let Some(desc) = &err.description {
ctx = anyhow!("{id}: {desc}").context(ctx);
}
msg.push_str(&format!("\n `{id}`"));
if !err.properties.is_empty() {
let props = err.properties.join(", ");
ctx = anyhow!("{id}: Invalid properties {props}").context(ctx);
msg.push_str(": invalid properties `");
msg.push_str(&err.properties.join("`, `"));
msg.push('`');
}
if let Some(desc) = &err.description {
msg.push_str(" (");
msg.push_str(desc.to_lowercase().trim_end_matches(['.', '\n']));
msg.push(')');
}
}
bail!(ctx)
bail!(msg)
}
printer.out(Message::new("Email(s) successfully deleted"))
+14 -11
View File
@@ -3,7 +3,7 @@ use std::{
io::{stdin, BufRead, IsTerminal},
};
use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use clap::Parser;
use io_jmap::{
rfc8620::coroutines::blob_upload::{JmapBlobUpload, JmapBlobUploadResult},
@@ -115,7 +115,7 @@ impl ImportEmailCommand {
let mut coroutine = JmapEmailImport::new(&jmap.session, &jmap.http_auth, emails)?;
let mut arg = None;
let errs = loop {
let not_created = loop {
match coroutine.resume(arg.take()) {
JmapEmailImportResult::Io { io } => arg = Some(handle(&mut jmap.stream, io)?),
JmapEmailImportResult::Ok { not_created, .. } => break not_created,
@@ -123,19 +123,22 @@ impl ImportEmailCommand {
}
};
if let Some(err) = errs.get(&blob_id) {
let mut ctx = anyhow!("Import JMAP email from blob `{blob_id}` error");
if let Some(desc) = &err.description {
ctx = anyhow!("{desc}").context(ctx);
}
if let Some(err) = not_created.get(&blob_id) {
let mut msg = format!("Import JMAP email from blob `{blob_id}` error");
if !err.properties.is_empty() {
let props = err.properties.join(", ");
ctx = anyhow!("Invalid properties {props}").context(ctx);
msg.push_str(": invalid properties `");
msg.push_str(&err.properties.join("`, `"));
msg.push('`');
}
bail!(ctx);
if let Some(desc) = &err.description {
msg.push_str(" (");
msg.push_str(desc.to_lowercase().trim_end_matches(['.', '\n']));
msg.push(')');
}
bail!(msg);
}
printer.out(Message::new("Email successfully imported"))
+13 -8
View File
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use clap::Parser;
use io_jmap::rfc8621::coroutines::email_set::{JmapEmailSet, JmapEmailSetArgs, JmapEmailSetResult};
use io_stream::runtimes::std::handle;
@@ -85,20 +85,25 @@ impl JmapEmailUpdateCommand {
};
if !not_updated.is_empty() {
let mut ctx = anyhow!("Update JMAP email(s) error");
let mut msg = String::from("Update JMAP email(s) error");
for (id, err) in not_updated {
if let Some(desc) = &err.description {
ctx = anyhow!("{id}: {desc}").context(ctx);
}
msg.push_str(&format!("\n `{id}`"));
if !err.properties.is_empty() {
let props = err.properties.join(", ");
ctx = anyhow!("{id}: Invalid properties {props}").context(ctx);
msg.push_str(": invalid properties `");
msg.push_str(&err.properties.join("`, `"));
msg.push('`');
}
if let Some(desc) = &err.description {
msg.push_str(" (");
msg.push_str(desc.to_lowercase().trim_end_matches(['.', '\n']));
msg.push(')');
}
}
bail!(ctx)
bail!(msg)
}
printer.out(Message::new("Email(s) successfully updated"))