From a61ab090ee03bd4c73d72398f373805e08d8fefe Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 27 Feb 2008 00:35:10 +0000 Subject: [PATCH] convert Shop::Tax to use exceptions --- lib/WebGUI/Exception.pm | 5 +++++ lib/WebGUI/Shop/Tax.pm | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/WebGUI/Exception.pm b/lib/WebGUI/Exception.pm index a5e5e1de6..313c272fc 100644 --- a/lib/WebGUI/Exception.pm +++ b/lib/WebGUI/Exception.pm @@ -34,6 +34,11 @@ use Exception::Class ( description => "The object you were try to retrieve does not exist.", fields => ["id"], }, + 'WebGUI::Error::InvalidFile' => { + isa => 'WebGUI::Error', + description => "The file you have provided has errors.", + fields => [qw{ brokenFile brokenLine }], + }, ); diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index f00c0e08e..9cc8ffe21 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -3,9 +3,9 @@ package WebGUI::Shop::Tax; use strict; use Class::InsideOut qw{ :std }; -use Carp qw(croak); use WebGUI::Text; use WebGUI::Storage; +use WebGUI::Exception::Shop; =head1 NAME @@ -65,10 +65,11 @@ sub add { my $self = shift; my $params = shift; my $id = $self->session->id->generate(); - croak "Must pass in a hashref" + + WebGUI::Error::InvalidParam->throw(error => 'Must pass in a hashref of params') unless ref($params) eq 'HASH'; foreach my $key (qw/field value taxRate/) { - croak "Hash ref must contain a $key key with a defined value" + WebGUI::Error::InvalidParam->throw(error => "Hash ref must contain a $key key with a defined value") unless exists($params->{$key}) and defined $params->{$key}; } $self->session->db->write('insert into tax (taxId, field, value, taxRate) VALUES (?,?,?,?)', [$id, @{ $params }{qw[ field value taxRate ]}]); @@ -94,9 +95,9 @@ The taxId of the data to delete from the table. sub delete { my $self = shift; my $params = shift; - croak "Must pass in a hashref" + WebGUI::Error::InvalidParam->throw(error => 'Must pass in a hashref of params') unless ref($params) eq 'HASH'; - croak "Hash ref must contain a taxId key with a defined value" + WebGUI::Error::InvalidParam->throw(error => "Hash ref must contain a taxId key with a defined value") unless exists($params->{taxId}) and defined $params->{taxId}; $self->session->db->write('delete from tax where taxId=?', [$params->{taxId}]); return; @@ -162,19 +163,19 @@ if old data has been deleted and new has been inserted. sub importTaxData { my $self = shift; my $filePath = shift; - croak q{Must provide the path to a file} + WebGUI::Error::InvalidParam->throw(error => q{Must provide the path to a file}) unless $filePath; - croak qq{$filePath could not be found} + WebGUI::Error::InvalidParam->throw(error => qq{$filePath could not be found}) unless -e $filePath; - croak qq{$filePath is not readable} + WebGUI::Error::InvalidParam->throw(error => qq{$filePath is not readable}) unless -r $filePath; open my $table, '<', $filePath or - croak "Unable to open $filePath for reading: $!\n"; + WebGUI::Error->throw(error => qq{Unable to open $filePath for reading: $!\n}); my $headers; $headers = <$table>; chomp $headers; my @headers = WebGUI::Text::splitCSV($headers); - croak qq{Bad header found in the CSV file} + WebGUI::Error::InvalidFile->throw(error => qq{Bad header found in the CSV file}, brokenFile => $filePath) unless (join(q{-}, sort @headers) eq 'field-taxRate-value') and (scalar @headers == 3); my @taxData = (); @@ -184,7 +185,7 @@ sub importTaxData { $taxRow =~ s/\s*#.+$//; next unless $taxRow; my @taxRow = WebGUI::Text::splitCSV($taxRow); - croak qq{Error on line $line in file $filePath} + WebGUI::Error::InvalidFile->throw(error => qq{Error found in the CSV file}, brokenFile => $filePath, brokenLine => $line) unless scalar @taxRow == 3; push @taxData, [ @taxRow ]; }