Show better errors when configuring (#1917)

* Show all errors when configuring

* Shorten some overly long msgs
This commit is contained in:
Hocuri
2020-09-21 15:06:33 +02:00
committed by GitHub
parent 987eaae0c1
commit 637d2661e8
6 changed files with 113 additions and 36 deletions

View File

@@ -8,6 +8,7 @@ mod server_params;
use anyhow::{bail, ensure, Context as _, Result};
use async_std::prelude::*;
use async_std::task;
use itertools::Itertools;
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use crate::config::Config;
@@ -250,22 +251,28 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> {
let smtp_config_task = task::spawn(async move {
let mut smtp_configured = false;
let mut errors = Vec::new();
for smtp_server in smtp_servers {
smtp_param.user = smtp_server.username.clone();
smtp_param.server = smtp_server.hostname.clone();
smtp_param.port = smtp_server.port;
smtp_param.security = smtp_server.socket;
if try_smtp_one_param(&context_smtp, &smtp_param, &smtp_addr, oauth2, &mut smtp).await {
smtp_configured = true;
break;
match try_smtp_one_param(&context_smtp, &smtp_param, &smtp_addr, oauth2, &mut smtp)
.await
{
Ok(_) => {
smtp_configured = true;
break;
}
Err(e) => errors.push(e),
}
}
if smtp_configured {
Some(smtp_param)
Ok(smtp_param)
} else {
None
Err(errors)
}
});
@@ -281,15 +288,19 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> {
.filter(|params| params.protocol == Protocol::IMAP)
.collect();
let imap_servers_count = imap_servers.len();
let mut errors = Vec::new();
for (imap_server_index, imap_server) in imap_servers.into_iter().enumerate() {
param.imap.user = imap_server.username.clone();
param.imap.server = imap_server.hostname.clone();
param.imap.port = imap_server.port;
param.imap.security = imap_server.socket;
if try_imap_one_param(ctx, &param.imap, &param.addr, oauth2, &mut imap).await {
imap_configured = true;
break;
match try_imap_one_param(ctx, &param.imap, &param.addr, oauth2, &mut imap).await {
Ok(_) => {
imap_configured = true;
break;
}
Err(e) => errors.push(e),
}
progress!(
ctx,
@@ -297,16 +308,19 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> {
);
}
if !imap_configured {
bail!("IMAP autoconfig did not succeed");
bail!(nicer_configuration_error(ctx, errors).await);
}
progress!(ctx, 850);
// Wait for SMTP configuration
if let Some(smtp_param) = smtp_config_task.await {
param.smtp = smtp_param;
} else {
bail!("SMTP autoconfig did not succeed");
match smtp_config_task.await {
Ok(smtp_param) => {
param.smtp = smtp_param;
}
Err(errors) => {
bail!(nicer_configuration_error(ctx, errors).await);
}
}
progress!(ctx, 900);
@@ -478,7 +492,7 @@ async fn try_imap_one_param(
addr: &str,
oauth2: bool,
imap: &mut Imap,
) -> bool {
) -> Result<(), ConfigurationError> {
let inf = format!(
"imap: {}@{}:{} security={} certificate_checks={} oauth2={}",
param.user, param.server, param.port, param.security, param.certificate_checks, oauth2
@@ -487,10 +501,13 @@ async fn try_imap_one_param(
if let Err(err) = imap.connect(context, param, addr, oauth2).await {
info!(context, "failure: {}", err);
false
Err(ConfigurationError {
config: inf,
msg: err.to_string(),
})
} else {
info!(context, "success: {}", inf);
true
Ok(())
}
}
@@ -500,7 +517,7 @@ async fn try_smtp_one_param(
addr: &str,
oauth2: bool,
smtp: &mut Smtp,
) -> bool {
) -> Result<(), ConfigurationError> {
let inf = format!(
"smtp: {}@{}:{} security={} certificate_checks={} oauth2={}",
param.user, param.server, param.port, param.security, param.certificate_checks, oauth2
@@ -509,14 +526,48 @@ async fn try_smtp_one_param(
if let Err(err) = smtp.connect(context, param, addr, oauth2).await {
info!(context, "failure: {}", err);
false
Err(ConfigurationError {
config: inf,
msg: err.to_string(),
})
} else {
info!(context, "success: {}", inf);
smtp.disconnect().await;
true
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
#[error("Trying {config}…\nError: {msg}")]
pub struct ConfigurationError {
config: String,
msg: String,
}
async fn nicer_configuration_error(context: &Context, errors: Vec<ConfigurationError>) -> String {
let first_err = if let Some(f) = errors.first() {
f
} else {
return "".to_string();
};
if errors
.iter()
.all(|e| e.msg.to_lowercase().contains("could not resolve"))
{
return context
.stock_str(StockMessage::ErrorNoNetwork)
.await
.to_string();
}
if errors.iter().all(|e| e.msg == first_err.msg) {
return first_err.msg.to_string();
}
errors.iter().map(|e| e.to_string()).join("\n\n")
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Invalid email address: {0:?}")]