Added nicer explosions via try::tiny

This commit is contained in:
Finn 2019-07-05 18:44:46 +01:00
parent d1cd30928e
commit afc635fdb4
No known key found for this signature in database
GPG key ID: 7455B4B17685B598
6 changed files with 27 additions and 33 deletions

View file

@ -1,7 +1,7 @@
package Pear::LocalLoop::Controller::Admin::ImportFrom;
use Mojo::Base 'Mojolicious::Controller';
use Moo;
use Devel::Dwarn;
use Try::Tiny;
use Pear::LocalLoop::Import::LCCCsv::Suppliers;
use Pear::LocalLoop::Import::LCCCsv::Transactions;
@ -47,19 +47,22 @@ sub post_transactions {
$c->flash( error => "CSV file size is too large" );
return $c->redirect_to( '/admin/import_from' );
}
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Transactions->new(
csv_string => $c->param('transactions_csv')->slurp,
schema => $c->app->schema
)->import_csv;
if ($csv_import->csv_error) {
$c->flash( error => $csv_import->csv_error );
my $csv_error;
try {
Pear::LocalLoop::Import::LCCCsv::Transactions->new(
csv_string => $c->param('transactions_csv')->slurp,
schema => $c->app->schema
)->import_csv;
} catch {
$csv_error = $_;
};
if ( $csv_error ) {
$c->flash( error => $csv_error );
return $c->redirect_to( '/admin/import_from' );
} else {
$c->flash( success => "CSV imported" );
return $c->redirect_to( '/admin/import_from' );
}
}
1;