From bbf2ea68f216a0d46693bf2ccd66d4021f3d9fa6 Mon Sep 17 00:00:00 2001 From: Alexandre Morignot <erdnaxeli@cervoi.se> Date: Wed, 11 Feb 2015 00:57:26 +0100 Subject: [PATCH] handle some errors on insert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And this is probably just the begining… but this is the goal of RaisErrors. --- lib/commands/tag.pm | 15 +++++++++------ lib/sites/parser.pm | 22 ++++++++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/commands/tag.pm b/lib/commands/tag.pm index 85c6541..9660d66 100644 --- a/lib/commands/tag.pm +++ b/lib/commands/tag.pm @@ -17,12 +17,15 @@ sub addTag my $sth; - $sth = $dbh->prepare_cached('INSERT INTO playbot_tags (id, tag) - VALUES (?, ?)'); - $log->error("Couldn't prepare querie; aborting") unless (defined $sth); - - $sth->execute($id, $tag) - or $log->error("Couldn't finish transaction: " . $dbh->errstr); + $sth = $dbh->prepare_cached('INSERT INTO playbot_tags (id, tag) VALUES (?, ?)'); + $log->error("Couldn't prepare querie; aborting") unless (defined $sth); + + eval { + $sth->execute($id, $tag) + }; + if ($@) { + $log->error("Couldn't finish transaction: " . $@); + } } 1; diff --git a/lib/sites/parser.pm b/lib/sites/parser.pm index 6b60f06..639d860 100644 --- a/lib/sites/parser.pm +++ b/lib/sites/parser.pm @@ -65,14 +65,20 @@ sub parse { } else { # insertion de la vidéo dans la bdd - my $sth = $dbh->prepare_cached(' - INSERT INTO playbot (type, url, sender, title, duration) - VALUES (?,?,?,?,?)'); - $log->error("Couldn't prepare querie; aborting") unless (defined $sth); - - $sth->execute($content{'site'}, $content{'url'}, - $content{'author'}, $content{'title'}, $content{'duration'}) - or $log->error("Couldn't finish transaction: " . $dbh->errstr); + eval { + my $sth = $dbh->prepare_cached(' + INSERT INTO playbot (type, url, sender, title, duration) + VALUES (?,?,?,?,?)'); + $log->error("Couldn't prepare querie; aborting") unless (defined $sth); + + $sth->execute($content{'site'}, $content{'url'}, + $content{'author'}, $content{'title'}, $content{'duration'}); + + $dbh->commit; + }; + if ($@) { + $log->error("Couldn't finish transaction: " . $@); + } } # sélection de l'id de la vidéo insérée -- GitLab