Skip to content

Commit dc552e7

Browse files
committed
Improved Error Handling
1 parent 75a5722 commit dc552e7

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lib/VWF/Config.pm

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package VWF::Config;
22

33
=head1 NAME
44
5-
VWF::Config - Site-independent configuration file
5+
VWF::Config - Site-independent configuration file for the Versatile Web Framework
66
77
=head1 VERSION
88
@@ -62,7 +62,7 @@ sub new
6262
my @config_dirs;
6363
if($ENV{'CONFIG_DIR'}) {
6464
# Validate directory exists
65-
throw Error::Simple("CONFIG_DIR '$ENV{CONFIG_DIR}' does not exist or is not readable" )
65+
throw Error::Simple("CONFIG_DIR '$ENV{CONFIG_DIR}' does not exist or is not readable")
6666
unless -d $ENV{'CONFIG_DIR'} && -r $ENV{'CONFIG_DIR'};
6767
@config_dirs = ($ENV{'CONFIG_DIR'});
6868
} else {
@@ -128,14 +128,21 @@ sub new
128128
}
129129
}
130130

131-
my $config = Config::Abstraction->new(
132-
config_dirs => \@config_dirs,
133-
config_files => ['default', $info->domain_name(), $ENV{'CONFIG_FILE'}, $args{'config_file'}],
134-
logger => $args{'logger'})->all();
131+
my $config;
132+
eval {
133+
$config = Config::Abstraction->new(
134+
config_dirs => \@config_dirs,
135+
config_files => ['default', $info->domain_name(), $ENV{'CONFIG_FILE'}, $args{'config_file'}],
136+
logger => $args{'logger'}
137+
)->all();
138+
};
135139
if($@ || !defined($config)) {
136140
throw Error::Simple("Configuration error: $@");
137141
}
138142

143+
# Validate essential configuration structure
144+
throw Error::Simple('Configuration must be a hash reference') unless(ref($config) eq 'HASH');
145+
139146
# The values in config are defaults which can be overridden by
140147
# the values in args{config}
141148
if(defined($args{'config'})) {
@@ -184,14 +191,14 @@ sub AUTOLOAD
184191

185192
return undef unless($self);
186193

187-
# Extract the method name from the AUTOLOAD variable
194+
# Extract the key name from the AUTOLOAD variable
188195
(my $key = $AUTOLOAD) =~ s/.*:://;
189196

190197
# Don't handle special methods
191198
return if $key eq 'DESTROY';
192199

193200
# Validate method name - only allow safe config keys
194-
Carp::croak("Invalid key name: $key" ) unless $key =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/;
201+
Carp::croak("Invalid key name: $key") unless $key =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/;
195202

196203
# Return the value of the corresponding hash key
197204
# Only return existing keys to avoid auto-vivification

0 commit comments

Comments
 (0)