Skip to content

Commit 63a47aa

Browse files
committed
Box all the figment errors to avoid large enum differences
1 parent e2aad08 commit 63a47aa

File tree

24 files changed

+245
-151
lines changed

24 files changed

+245
-151
lines changed

crates/cli/src/commands/config.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Options {
7272
SC::Dump { output } => {
7373
let _span = info_span!("cli.config.dump").entered();
7474

75-
let config = RootConfig::extract(figment)?;
75+
let config = RootConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
7676
let config = serde_yaml::to_string(&config)?;
7777

7878
if let Some(output) = output {
@@ -88,7 +88,7 @@ impl Options {
8888
SC::Check => {
8989
let _span = info_span!("cli.config.check").entered();
9090

91-
let _config = RootConfig::extract(figment)?;
91+
let _config = RootConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
9292
info!("Configuration file looks good");
9393
}
9494

@@ -105,7 +105,8 @@ impl Options {
105105

106106
if !synapse_config.is_empty() {
107107
info!("Adjusting MAS config to match Synapse config from {synapse_config:?}");
108-
let synapse_config = syn2mas::synapse_config::Config::load(&synapse_config)?;
108+
let synapse_config = syn2mas::synapse_config::Config::load(&synapse_config)
109+
.map_err(anyhow::Error::from_boxed)?;
109110
config = synapse_config.adjust_mas_config(config, &mut rng, clock.now());
110111
}
111112

@@ -121,7 +122,7 @@ impl Options {
121122
}
122123

123124
SC::Sync { prune, dry_run } => {
124-
let config = SyncConfig::extract(figment)?;
125+
let config = SyncConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
125126
let clock = SystemClock::default();
126127
let encrypter = config.secrets.encrypter().await?;
127128

crates/cli/src/commands/database.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ enum Subcommand {
3030
impl Options {
3131
pub async fn run(self, figment: &Figment) -> anyhow::Result<ExitCode> {
3232
let _span = info_span!("cli.database.migrate").entered();
33-
let config = DatabaseConfig::extract_or_default(figment)?;
33+
let config =
34+
DatabaseConfig::extract_or_default(figment).map_err(anyhow::Error::from_boxed)?;
3435
let mut conn = database_connection_from_config(&config).await?;
3536

3637
// Run pending migrations

crates/cli/src/commands/debug.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ impl Options {
4141
match self.subcommand {
4242
SC::Policy { with_dynamic_data } => {
4343
let _span = info_span!("cli.debug.policy").entered();
44-
let config = PolicyConfig::extract_or_default(figment)?;
45-
let matrix_config = MatrixConfig::extract(figment)?;
44+
let config =
45+
PolicyConfig::extract_or_default(figment).map_err(anyhow::Error::from_boxed)?;
46+
let matrix_config =
47+
MatrixConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
4648
info!("Loading and compiling the policy module");
4749
let policy_factory = policy_factory_from_config(&config, &matrix_config).await?;
4850

4951
if with_dynamic_data {
50-
let database_config = DatabaseConfig::extract(figment)?;
52+
let database_config =
53+
DatabaseConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
5154
let pool = database_pool_from_config(&database_config).await?;
5255
let repository_factory = PgRepositoryFactory::new(pool.clone());
5356
load_policy_factory_dynamic_data(&policy_factory, &repository_factory).await?;

crates/cli/src/commands/doctor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl Options {
3333
"💡 Running diagnostics, make sure that both MAS and Synapse are running, and that MAS is using the same configuration files as this tool."
3434
);
3535

36-
let config = RootConfig::extract(figment)?;
36+
let config = RootConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
3737

3838
// We'll need an HTTP client
3939
let http_client = mas_http::reqwest_client();

crates/cli/src/commands/manage.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,10 @@ impl Options {
219219
let _span =
220220
info_span!("cli.manage.set_password", user.username = %username).entered();
221221

222-
let database_config = DatabaseConfig::extract_or_default(figment)?;
223-
let passwords_config = PasswordsConfig::extract_or_default(figment)?;
222+
let database_config = DatabaseConfig::extract_or_default(figment)
223+
.map_err(anyhow::Error::from_boxed)?;
224+
let passwords_config = PasswordsConfig::extract_or_default(figment)
225+
.map_err(anyhow::Error::from_boxed)?;
224226

225227
let mut conn = database_connection_from_config(&database_config).await?;
226228
let password_manager = password_manager_from_config(&passwords_config).await?;
@@ -260,7 +262,8 @@ impl Options {
260262
)
261263
.entered();
262264

263-
let database_config = DatabaseConfig::extract_or_default(figment)?;
265+
let database_config = DatabaseConfig::extract_or_default(figment)
266+
.map_err(anyhow::Error::from_boxed)?;
264267
let mut conn = database_connection_from_config(&database_config).await?;
265268
let txn = conn.begin().await?;
266269
let mut repo = PgRepository::from_conn(txn);
@@ -314,7 +317,8 @@ impl Options {
314317
admin,
315318
device_id,
316319
} => {
317-
let database_config = DatabaseConfig::extract_or_default(figment)?;
320+
let database_config = DatabaseConfig::extract_or_default(figment)
321+
.map_err(anyhow::Error::from_boxed)?;
318322
let mut conn = database_connection_from_config(&database_config).await?;
319323
let txn = conn.begin().await?;
320324
let mut repo = PgRepository::from_conn(txn);
@@ -372,7 +376,8 @@ impl Options {
372376
(Some(_), true) => unreachable!(), // This should be handled by the clap group
373377
};
374378

375-
let database_config = DatabaseConfig::extract_or_default(figment)?;
379+
let database_config = DatabaseConfig::extract_or_default(figment)
380+
.map_err(anyhow::Error::from_boxed)?;
376381
let mut conn = database_connection_from_config(&database_config).await?;
377382
let txn = conn.begin().await?;
378383
let mut repo = PgRepository::from_conn(txn);
@@ -399,7 +404,8 @@ impl Options {
399404

400405
SC::ProvisionAllUsers => {
401406
let _span = info_span!("cli.manage.provision_all_users").entered();
402-
let database_config = DatabaseConfig::extract_or_default(figment)?;
407+
let database_config = DatabaseConfig::extract_or_default(figment)
408+
.map_err(anyhow::Error::from_boxed)?;
403409
let mut conn = database_connection_from_config(&database_config).await?;
404410
let mut txn = conn.begin().await?;
405411

@@ -425,7 +431,8 @@ impl Options {
425431
SC::KillSessions { username, dry_run } => {
426432
let _span =
427433
info_span!("cli.manage.kill_sessions", user.username = username).entered();
428-
let database_config = DatabaseConfig::extract_or_default(figment)?;
434+
let database_config = DatabaseConfig::extract_or_default(figment)
435+
.map_err(anyhow::Error::from_boxed)?;
429436
let mut conn = database_connection_from_config(&database_config).await?;
430437
let txn = conn.begin().await?;
431438
let mut repo = PgRepository::from_conn(txn);
@@ -497,7 +504,8 @@ impl Options {
497504
deactivate,
498505
} => {
499506
let _span = info_span!("cli.manage.lock_user", user.username = username).entered();
500-
let config = DatabaseConfig::extract_or_default(figment)?;
507+
let config = DatabaseConfig::extract_or_default(figment)
508+
.map_err(anyhow::Error::from_boxed)?;
501509
let mut conn = database_connection_from_config(&config).await?;
502510
let txn = conn.begin().await?;
503511
let mut repo = PgRepository::from_conn(txn);
@@ -529,7 +537,8 @@ impl Options {
529537

530538
SC::UnlockUser { username } => {
531539
let _span = info_span!("cli.manage.lock_user", user.username = username).entered();
532-
let config = DatabaseConfig::extract_or_default(figment)?;
540+
let config = DatabaseConfig::extract_or_default(figment)
541+
.map_err(anyhow::Error::from_boxed)?;
533542
let mut conn = database_connection_from_config(&config).await?;
534543
let txn = conn.begin().await?;
535544
let mut repo = PgRepository::from_conn(txn);
@@ -562,9 +571,12 @@ impl Options {
562571
ignore_password_complexity,
563572
} => {
564573
let http_client = mas_http::reqwest_client();
565-
let password_config = PasswordsConfig::extract_or_default(figment)?;
566-
let database_config = DatabaseConfig::extract_or_default(figment)?;
567-
let matrix_config = MatrixConfig::extract(figment)?;
574+
let password_config = PasswordsConfig::extract_or_default(figment)
575+
.map_err(anyhow::Error::from_boxed)?;
576+
let database_config = DatabaseConfig::extract_or_default(figment)
577+
.map_err(anyhow::Error::from_boxed)?;
578+
let matrix_config =
579+
MatrixConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
568580

569581
let password_manager = password_manager_from_config(&password_config).await?;
570582
let homeserver = homeserver_connection_from_config(&matrix_config, http_client);

crates/cli/src/commands/server.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl Options {
5959
pub async fn run(self, figment: &Figment) -> anyhow::Result<ExitCode> {
6060
let span = info_span!("cli.run.init").entered();
6161
let mut shutdown = LifecycleManager::new()?;
62-
let config = AppConfig::extract(figment)?;
62+
let config = AppConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
6363

6464
info!(version = crate::VERSION, "Starting up");
6565

@@ -101,8 +101,10 @@ impl Options {
101101
} else {
102102
// Sync the configuration with the database
103103
let mut conn = pool.acquire().await?;
104-
let clients_config = ClientsConfig::extract_or_default(figment)?;
105-
let upstream_oauth2_config = UpstreamOAuth2Config::extract_or_default(figment)?;
104+
let clients_config =
105+
ClientsConfig::extract_or_default(figment).map_err(anyhow::Error::from_boxed)?;
106+
let upstream_oauth2_config = UpstreamOAuth2Config::extract_or_default(figment)
107+
.map_err(anyhow::Error::from_boxed)?;
106108

107109
crate::sync::config_sync(
108110
upstream_oauth2_config,

crates/cli/src/commands/syn2mas.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl Options {
9696
}
9797

9898
let synapse_config = synapse_config::Config::load(&self.synapse_configuration_files)
99+
.map_err(anyhow::Error::from_boxed)
99100
.context("Failed to load Synapse configuration")?;
100101

101102
// Establish a connection to Synapse's Postgres database
@@ -111,7 +112,8 @@ impl Options {
111112
.await
112113
.context("could not connect to Synapse Postgres database")?;
113114

114-
let config = DatabaseConfig::extract_or_default(figment)?;
115+
let config =
116+
DatabaseConfig::extract_or_default(figment).map_err(anyhow::Error::from_boxed)?;
115117

116118
let mut mas_connection = database_connection_from_config_with_options(
117119
&config,
@@ -131,7 +133,7 @@ impl Options {
131133
// First perform a config sync
132134
// This is crucial to ensure we register upstream OAuth providers
133135
// in the MAS database
134-
let config = SyncConfig::extract(figment)?;
136+
let config = SyncConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
135137
let clock = SystemClock::default();
136138
let encrypter = config.secrets.encrypter().await?;
137139

@@ -213,7 +215,8 @@ impl Options {
213215

214216
Subcommand::Migrate { dry_run } => {
215217
let provider_id_mappings: HashMap<String, Uuid> = {
216-
let mas_oauth2 = UpstreamOAuth2Config::extract_or_default(figment)?;
218+
let mas_oauth2 = UpstreamOAuth2Config::extract_or_default(figment)
219+
.map_err(anyhow::Error::from_boxed)?;
217220

218221
mas_oauth2
219222
.providers
@@ -252,7 +255,8 @@ impl Options {
252255
let occasional_progress_logger_task =
253256
tokio::spawn(occasional_progress_logger(progress.clone()));
254257

255-
let mas_matrix = MatrixConfig::extract(figment)?;
258+
let mas_matrix =
259+
MatrixConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
256260
syn2mas::migrate(
257261
reader,
258262
writer,

crates/cli/src/commands/templates.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,20 @@ impl Options {
3737
SC::Check => {
3838
let _span = info_span!("cli.templates.check").entered();
3939

40-
let template_config = TemplatesConfig::extract_or_default(figment)?;
41-
let branding_config = BrandingConfig::extract_or_default(figment)?;
42-
let matrix_config = MatrixConfig::extract(figment)?;
43-
let experimental_config = ExperimentalConfig::extract_or_default(figment)?;
44-
let password_config = PasswordsConfig::extract_or_default(figment)?;
45-
let account_config = AccountConfig::extract_or_default(figment)?;
46-
let captcha_config = CaptchaConfig::extract_or_default(figment)?;
40+
let template_config = TemplatesConfig::extract_or_default(figment)
41+
.map_err(anyhow::Error::from_boxed)?;
42+
let branding_config = BrandingConfig::extract_or_default(figment)
43+
.map_err(anyhow::Error::from_boxed)?;
44+
let matrix_config =
45+
MatrixConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
46+
let experimental_config = ExperimentalConfig::extract_or_default(figment)
47+
.map_err(anyhow::Error::from_boxed)?;
48+
let password_config = PasswordsConfig::extract_or_default(figment)
49+
.map_err(anyhow::Error::from_boxed)?;
50+
let account_config = AccountConfig::extract_or_default(figment)
51+
.map_err(anyhow::Error::from_boxed)?;
52+
let captcha_config = CaptchaConfig::extract_or_default(figment)
53+
.map_err(anyhow::Error::from_boxed)?;
4754

4855
let clock = SystemClock::default();
4956
// XXX: we should disallow SeedableRng::from_entropy

crates/cli/src/commands/worker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Options {
2929
pub async fn run(self, figment: &Figment) -> anyhow::Result<ExitCode> {
3030
let shutdown = LifecycleManager::new()?;
3131
let span = info_span!("cli.worker.init").entered();
32-
let config = AppConfig::extract(figment)?;
32+
let config = AppConfig::extract(figment).map_err(anyhow::Error::from_boxed)?;
3333

3434
// Connect to the database
3535
info!("Connecting to the database");

crates/cli/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ async fn try_main() -> anyhow::Result<ExitCode> {
115115
// Load the base configuration files
116116
let figment = opts.figment();
117117

118-
let telemetry_config =
119-
TelemetryConfig::extract_or_default(&figment).context("Failed to load telemetry config")?;
118+
let telemetry_config = TelemetryConfig::extract_or_default(&figment)
119+
.map_err(anyhow::Error::from_boxed)
120+
.context("Failed to load telemetry config")?;
120121

121122
// Setup Sentry
122123
let sentry = sentry::init((

0 commit comments

Comments
 (0)