@@ -33,13 +33,20 @@ impl AppConfig {
33
33
config_file
34
34
) ) ;
35
35
}
36
+ log:: debug!( "Loading configuration from file: {:?}" , config_file) ;
36
37
load_from_file ( config_file) ?
37
38
} else if let Some ( config_dir) = & cli. config_dir {
39
+ log:: debug!( "Loading configuration from directory: {:?}" , config_dir) ;
38
40
load_from_directory ( config_dir) ?
39
41
} else {
42
+ log:: debug!( "Loading configuration from environment" ) ;
40
43
load_from_env ( ) ?
41
44
} ;
42
45
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
+ ) ;
43
50
config. web_root . clone_from ( web_root) ;
44
51
}
45
52
if let Some ( config_dir) = & cli. config_dir {
@@ -58,14 +65,25 @@ impl AppConfig {
58
65
"Configuration directory does not exist, creating it: {:?}" ,
59
66
config. configuration_directory
60
67
) ;
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
+ } ) ?;
62
74
}
63
75
64
76
if config. database_url . is_empty ( ) {
77
+ log:: debug!(
78
+ "Creating default database in {}" ,
79
+ config. configuration_directory. display( )
80
+ ) ;
65
81
config. database_url = create_default_database ( & config. configuration_directory ) ;
66
82
}
67
83
68
- config. validate ( ) ?;
84
+ config
85
+ . validate ( )
86
+ . context ( "The provided configuration is invalid" ) ?;
69
87
70
88
log:: debug!( "Loaded configuration: {:#?}" , config) ;
71
89
@@ -123,6 +141,7 @@ pub fn load_from_cli() -> anyhow::Result<AppConfig> {
123
141
pub fn load_from_env ( ) -> anyhow:: Result < AppConfig > {
124
142
let config_dir = configuration_directory ( ) ;
125
143
load_from_directory ( & config_dir)
144
+ . with_context ( || format ! ( "Unable to load configuration from {}" , config_dir. display( ) ) )
126
145
}
127
146
128
147
#[ derive( Debug , Deserialize , PartialEq , Clone ) ]
@@ -281,11 +300,17 @@ pub fn load_from_file(config_file: &Path) -> anyhow::Result<AppConfig> {
281
300
. add_source ( config:: File :: from ( config_file) . required ( false ) )
282
301
. add_source ( env_config ( ) )
283
302
. 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) ;
286
311
let app_config = config
287
312
. try_deserialize :: < AppConfig > ( )
288
- . with_context ( || "Unable to load configuration") ?;
313
+ . context ( "Failed to load the configuration") ?;
289
314
Ok ( app_config)
290
315
}
291
316
@@ -301,7 +326,11 @@ fn deserialize_socket_addr<'de, D: Deserializer<'de>>(
301
326
) -> Result < Option < SocketAddr > , D :: Error > {
302
327
let host_str: Option < String > = Deserialize :: deserialize ( deserializer) ?;
303
328
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
+ } )
305
334
. transpose ( )
306
335
}
307
336
@@ -350,7 +379,7 @@ fn parse_socket_addr(host_str: &str) -> anyhow::Result<SocketAddr> {
350
379
host_str
351
380
. to_socket_addrs ( ) ?
352
381
. next ( )
353
- . with_context ( || format ! ( "host '{host_str}' does not resolve to an IP " ) )
382
+ . with_context ( || format ! ( "Resolving host '{host_str}'" ) )
354
383
}
355
384
356
385
#[ cfg( test) ]
@@ -518,7 +547,7 @@ mod test {
518
547
519
548
#[ test]
520
549
fn test_cli_argument_parsing ( ) {
521
- let cli = Cli :: parse_from ( & [
550
+ let cli = Cli :: parse_from ( [
522
551
"sqlpage" ,
523
552
"--web-root" ,
524
553
"/path/to/web" ,
0 commit comments