Skip to content

Commit fb05e4c

Browse files
Kirill BulatovSomeoneToIgnore
authored andcommitted
Show better error messages on pageserver failures
1 parent b0a7234 commit fb05e4c

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

pageserver/src/bin/pageserver.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ use std::{
99
env,
1010
net::TcpListener,
1111
path::{Path, PathBuf},
12-
process::exit,
1312
str::FromStr,
1413
thread,
1514
};
1615
use zenith_utils::{auth::JwtAuth, logging, postgres_backend::AuthType};
1716

18-
use anyhow::{bail, ensure, Result};
17+
use anyhow::{bail, ensure, Context, Result};
1918
use clap::{App, Arg, ArgMatches};
2019
use daemonize::Daemonize;
2120

@@ -349,7 +348,10 @@ fn main() -> Result<()> {
349348
.get_matches();
350349

351350
let workdir = Path::new(arg_matches.value_of("workdir").unwrap_or(".zenith"));
352-
let cfg_file_path = workdir.canonicalize()?.join("pageserver.toml");
351+
let cfg_file_path = workdir
352+
.canonicalize()
353+
.with_context(|| format!("Error opening workdir '{}'", workdir.display()))?
354+
.join("pageserver.toml");
353355

354356
let args_params = CfgFileParams::from_args(&arg_matches);
355357

@@ -361,22 +363,37 @@ fn main() -> Result<()> {
361363
args_params
362364
} else {
363365
// Supplement the CLI arguments with the config file
364-
let cfg_file_contents = std::fs::read_to_string(&cfg_file_path)?;
365-
let file_params: CfgFileParams = toml::from_str(&cfg_file_contents)?;
366+
let cfg_file_contents = std::fs::read_to_string(&cfg_file_path)
367+
.with_context(|| format!("No pageserver config at '{}'", cfg_file_path.display()))?;
368+
let file_params: CfgFileParams = toml::from_str(&cfg_file_contents).with_context(|| {
369+
format!(
370+
"Failed to read '{}' as pageserver config",
371+
cfg_file_path.display()
372+
)
373+
})?;
366374
args_params.or(file_params)
367375
};
368376

369377
// Set CWD to workdir for non-daemon modes
370-
env::set_current_dir(&workdir)?;
378+
env::set_current_dir(&workdir).with_context(|| {
379+
format!(
380+
"Failed to set application's current dir to '{}'",
381+
workdir.display()
382+
)
383+
})?;
371384

372385
// Ensure the config is valid, even if just init-ing
373-
let mut conf = params.try_into_config()?;
386+
let mut conf = params.try_into_config().with_context(|| {
387+
format!(
388+
"Pageserver config at '{}' is not valid",
389+
cfg_file_path.display()
390+
)
391+
})?;
374392

375393
conf.daemonize = arg_matches.is_present("daemonize");
376394

377395
if init && conf.daemonize {
378-
eprintln!("--daemonize cannot be used with --init");
379-
exit(1);
396+
bail!("--daemonize cannot be used with --init")
380397
}
381398

382399
// The configuration is all set up now. Turn it into a 'static
@@ -386,16 +403,21 @@ fn main() -> Result<()> {
386403

387404
// Create repo and exit if init was requested
388405
if init {
389-
branches::init_pageserver(conf, create_tenant)?;
406+
branches::init_pageserver(conf, create_tenant).context("Failed to init pageserver")?;
390407
// write the config file
391-
let cfg_file_contents = toml::to_string_pretty(&params)?;
408+
let cfg_file_contents = toml::to_string_pretty(&params)
409+
.context("Failed to create pageserver config contents for initialisation")?;
392410
// TODO support enable-auth flag
393-
std::fs::write(&cfg_file_path, cfg_file_contents)?;
394-
395-
return Ok(());
411+
std::fs::write(&cfg_file_path, cfg_file_contents).with_context(|| {
412+
format!(
413+
"Failed to initialize pageserver config at '{}'",
414+
cfg_file_path.display()
415+
)
416+
})?;
417+
Ok(())
418+
} else {
419+
start_pageserver(conf).context("Failed to start pageserver")
396420
}
397-
398-
start_pageserver(conf)
399421
}
400422

401423
fn start_pageserver(conf: &'static PageServerConf) -> Result<()> {

0 commit comments

Comments
 (0)