@@ -33,13 +33,20 @@ impl AppConfig {
3333 config_file
3434 ) ) ;
3535 }
36+ log:: debug!( "Loading configuration from file: {:?}" , config_file) ;
3637 load_from_file ( config_file) ?
3738 } else if let Some ( config_dir) = & cli. config_dir {
39+ log:: debug!( "Loading configuration from directory: {:?}" , config_dir) ;
3840 load_from_directory ( config_dir) ?
3941 } else {
42+ log:: debug!( "Loading configuration from environment" ) ;
4043 load_from_env ( ) ?
4144 } ;
4245 if let Some ( web_root) = & cli. web_root {
46+ log:: debug!(
47+ "Setting web root to value from the command line: {:?}" ,
48+ web_root
49+ ) ;
4350 config. web_root . clone_from ( web_root) ;
4451 }
4552 if let Some ( config_dir) = & cli. config_dir {
@@ -58,14 +65,25 @@ impl AppConfig {
5865 "Configuration directory does not exist, creating it: {:?}" ,
5966 config. configuration_directory
6067 ) ;
61- std:: fs:: create_dir_all ( & config. configuration_directory ) ?;
68+ std:: fs:: create_dir_all ( & config. configuration_directory ) . with_context ( || {
69+ format ! (
70+ "Failed to create configuration directory in {}" ,
71+ config. configuration_directory. display( )
72+ )
73+ } ) ?;
6274 }
6375
6476 if config. database_url . is_empty ( ) {
77+ log:: debug!(
78+ "Creating default database in {}" ,
79+ config. configuration_directory. display( )
80+ ) ;
6581 config. database_url = create_default_database ( & config. configuration_directory ) ;
6682 }
6783
68- config. validate ( ) ?;
84+ config
85+ . validate ( )
86+ . context ( "The provided configuration is invalid" ) ?;
6987
7088 log:: debug!( "Loaded configuration: {:#?}" , config) ;
7189
@@ -123,6 +141,7 @@ pub fn load_from_cli() -> anyhow::Result<AppConfig> {
123141pub fn load_from_env ( ) -> anyhow:: Result < AppConfig > {
124142 let config_dir = configuration_directory ( ) ;
125143 load_from_directory ( & config_dir)
144+ . with_context ( || format ! ( "Unable to load configuration from {}" , config_dir. display( ) ) )
126145}
127146
128147#[ derive( Debug , Deserialize , PartialEq , Clone ) ]
@@ -281,11 +300,17 @@ pub fn load_from_file(config_file: &Path) -> anyhow::Result<AppConfig> {
281300 . add_source ( config:: File :: from ( config_file) . required ( false ) )
282301 . add_source ( env_config ( ) )
283302 . add_source ( env_config ( ) . prefix ( "SQLPAGE" ) )
284- . build ( ) ?;
285- log:: trace!( "Configuration sources: {}" , config. cache) ;
303+ . build ( )
304+ . with_context ( || {
305+ format ! (
306+ "Unable to build configuration loader for {}" ,
307+ config_file. display( )
308+ )
309+ } ) ?;
310+ log:: trace!( "Configuration sources: {:#?}" , config. cache) ;
286311 let app_config = config
287312 . try_deserialize :: < AppConfig > ( )
288- . with_context ( || "Unable to load configuration") ?;
313+ . context ( "Failed to load the configuration") ?;
289314 Ok ( app_config)
290315}
291316
@@ -301,7 +326,11 @@ fn deserialize_socket_addr<'de, D: Deserializer<'de>>(
301326) -> Result < Option < SocketAddr > , D :: Error > {
302327 let host_str: Option < String > = Deserialize :: deserialize ( deserializer) ?;
303328 host_str
304- . map ( |h| parse_socket_addr ( & h) . map_err ( D :: Error :: custom) )
329+ . map ( |h| {
330+ parse_socket_addr ( & h) . map_err ( |e| {
331+ D :: Error :: custom ( anyhow:: anyhow!( "Failed to parse socket address {h:?}: {e}" ) )
332+ } )
333+ } )
305334 . transpose ( )
306335}
307336
@@ -350,7 +379,7 @@ fn parse_socket_addr(host_str: &str) -> anyhow::Result<SocketAddr> {
350379 host_str
351380 . to_socket_addrs ( ) ?
352381 . next ( )
353- . with_context ( || format ! ( "host '{host_str}' does not resolve to an IP " ) )
382+ . with_context ( || format ! ( "Resolving host '{host_str}'" ) )
354383}
355384
356385#[ cfg( test) ]
@@ -518,7 +547,7 @@ mod test {
518547
519548 #[ test]
520549 fn test_cli_argument_parsing ( ) {
521- let cli = Cli :: parse_from ( & [
550+ let cli = Cli :: parse_from ( [
522551 "sqlpage" ,
523552 "--web-root" ,
524553 "/path/to/web" ,
0 commit comments