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"))
+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::identity_set::{
JmapIdentitySet, JmapIdentitySetArgs, JmapIdentitySetResult,
@@ -38,20 +38,25 @@ impl DeleteIdentityCommand {
};
if !not_destroyed.is_empty() {
let mut ctx = anyhow!("Destroy JMAP identities error");
let mut msg = String::from("Destroy JMAP identities 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("Identity successfully deleted"))
+14 -11
View File
@@ -1,4 +1,4 @@
use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use clap::Parser;
use io_jmap::{
rfc8621::coroutines::identity_set::{
@@ -48,7 +48,7 @@ impl UpdateIdentityCommand {
let mut coroutine = JmapIdentitySet::new(&jmap.session, &jmap.http_auth, args)?;
let mut arg = None;
let errs = loop {
let not_updated = loop {
match coroutine.resume(arg.take()) {
JmapIdentitySetResult::Io { io } => arg = Some(handle(&mut jmap.stream, io)?),
JmapIdentitySetResult::Ok { not_updated, .. } => break not_updated,
@@ -56,19 +56,22 @@ impl UpdateIdentityCommand {
}
};
if let Some(err) = errs.get(&self.id) {
let mut ctx = anyhow!("Update identity `{}` error", &self.id);
if let Some(desc) = &err.description {
ctx = anyhow!("{desc}").context(ctx);
}
if let Some(err) = not_updated.get(&self.id) {
let mut msg = format!("Update identity `{}` error", self.id);
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("Identity successfully updated"))
+14 -13
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::mailbox_set::{JmapMailboxSet, JmapMailboxSetArgs, JmapMailboxSetResult},
@@ -56,21 +56,22 @@ impl JmapMailboxCreateCommand {
}
};
if !not_created.is_empty() {
let mut ctx = anyhow!("Create JMAP mailbox `{}` error", self.name);
if let Some(err) = not_created.get(&self.name) {
let mut msg = format!("Create JMAP mailbox `{}` error", self.name);
for (_, err) in not_created {
if let Some(desc) = &err.description {
ctx = anyhow!(desc.clone()).context(ctx);
}
if !err.properties.is_empty() {
let props = err.properties.join(", ");
ctx = anyhow!("Invalid properties {props}").context(ctx);
}
if !err.properties.is_empty() {
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("Mailbox successfully created"))
+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::mailbox_set::{
JmapMailboxSet, JmapMailboxSetArgs, JmapMailboxSetResult,
@@ -40,20 +40,25 @@ impl JmapMailboxDestroyCommand {
};
if !not_destroyed.is_empty() {
let mut ctx = anyhow!("Destroy JMAP mailbox(es) error");
let mut msg = String::from("Destroy JMAP mailbox(es) 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("Mailbox successfully deleted"))
+14 -11
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::mailbox_set::{JmapMailboxSet, JmapMailboxSetArgs, JmapMailboxSetResult},
@@ -72,7 +72,7 @@ impl JmapMailboxUpdateCommand {
let mut arg = None;
let mut coroutine = JmapMailboxSet::new(&jmap.session, &jmap.http_auth, args)?;
let errs = loop {
let not_updated = loop {
match coroutine.resume(arg.take()) {
JmapMailboxSetResult::Io { io } => arg = Some(handle(&mut jmap.stream, io)?),
JmapMailboxSetResult::Ok { not_updated, .. } => break not_updated,
@@ -80,19 +80,22 @@ impl JmapMailboxUpdateCommand {
}
};
if let Some(err) = errs.get(&self.id) {
let mut ctx = anyhow!("Update JMAP mailbox `{}` error", self.id);
if let Some(desc) = &err.description {
ctx = anyhow!("{desc}").context(ctx);
}
if let Some(err) = not_updated.get(&self.id) {
let mut msg = format!("Update JMAP mailbox `{}` error", self.id);
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("Mailbox successfully updated"))
+19 -9
View File
@@ -38,16 +38,26 @@ impl CancelSubmissionCommand {
}
};
for (id, err) in &not_updated {
let mut ctx = anyhow!("Cancel submission `{id}` error");
if let Some(desc) = &err.description {
ctx = anyhow!("{desc}").context(ctx);
if !not_updated.is_empty() {
let mut msg = String::from("Cancel submission(s) error");
for (id, err) in &not_updated {
msg.push_str(&format!("\n `{id}`"));
if !err.properties.is_empty() {
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(')');
}
}
if !err.properties.is_empty() {
let props = err.properties.join(", ");
ctx = anyhow!("Invalid properties {props}").context(ctx);
}
bail!(ctx);
bail!(msg);
}
printer.out(Message::new(format!(
+14 -11
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_submission_set::{
@@ -75,7 +75,7 @@ impl CreateSubmissionCommand {
JmapEmailSubmissionSet::new(&jmap.session, &jmap.http_auth, submissions)?;
let mut arg = None;
let (created, errs) = loop {
let (created, not_created) = loop {
match coroutine.resume(arg.take()) {
JmapEmailSubmissionSetResult::Io { io } => {
arg = Some(handle(&mut jmap.stream, io)?)
@@ -89,19 +89,22 @@ impl CreateSubmissionCommand {
}
};
if let Some(err) = errs.get(&self.email_id) {
let mut ctx = anyhow!("Send email `{}` error", &self.email_id);
if let Some(desc) = &err.description {
ctx = anyhow!("{desc}").context(ctx);
}
if let Some(err) = not_created.get(&self.email_id) {
let mut msg = format!("Send email `{}` error", self.email_id);
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);
}
let table = SubmissionsTable {