Skip to content

Commit

Permalink
Merge pull request #1041 from IceBreeze/exceptions
Browse files Browse the repository at this point in the history
changes exception handling for plugins
  • Loading branch information
Difegue committed Sep 1, 2024
2 parents 832ddcc + 78f0eea commit 8554b94
Show file tree
Hide file tree
Showing 27 changed files with 967 additions and 752 deletions.
9 changes: 2 additions & 7 deletions lib/LANraragi/Controller/Batch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ sub socket {
$tags = redis_decode($tags);

my @tagarray = split_tags_to_array($tags);
@tagarray = rewrite_tags(\@tagarray, $rules, $hash_replace_rules);
@tagarray = rewrite_tags( \@tagarray, $rules, $hash_replace_rules );

# Merge array with commas
my $newtags = join( ', ', @tagarray );
Expand Down Expand Up @@ -200,12 +200,7 @@ sub batch_plugin {
my ( $id, $plugin, @args ) = @_;

# Run plugin with args on id
my %plugin_result;
eval { %plugin_result = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin, $id, "", @args ); };

if ($@) {
$plugin_result{error} = $@;
}
my %plugin_result = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin, $id, "", @args );

# If the plugin exec returned tags, add them
unless ( exists $plugin_result{error} ) {
Expand Down
143 changes: 76 additions & 67 deletions lib/LANraragi/Model/Plugins.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package LANraragi::Model::Plugins;

use v5.36;
use experimental 'try';

use strict;
use warnings;
use utf8;
Expand Down Expand Up @@ -53,42 +56,36 @@ sub exec_enabled_plugins_on_file {

my %pluginfo = $plugin->plugin_info();

#Every plugin execution is eval'd separately
eval { %plugin_result = exec_metadata_plugin( $plugin, $id, "", @args ); };
%plugin_result = exec_metadata_plugin( $plugin, $id, "", @args );

if ($@) {
$failures++;
$logger->error("$@");
} elsif ( exists $plugin_result{error} ) {
if ( exists $plugin_result{error} ) {
$failures++;
$logger->error( $plugin_result{error} );
} else {
$successes++;
next;
}

#If the plugin exec returned metadata, add it
unless ( exists $plugin_result{error} ) {
set_tags( $id, $plugin_result{new_tags}, 1 );
$successes++;

# Sum up all the added tags for later reporting.
# This doesn't take into account tags that are added twice
# (e.g by different plugins), but since this is more meant to show
# if the plugins added any data at all it's fine.
my @added_tags = split( ',', $plugin_result{new_tags} );
$addedtags += @added_tags;
#If the plugin exec returned metadata, add it
set_tags( $id, $plugin_result{new_tags}, 1 );

if ( exists $plugin_result{title} ) {
set_title( $id, $plugin_result{title} );
# Sum up all the added tags for later reporting.
# This doesn't take into account tags that are added twice
# (e.g by different plugins), but since this is more meant to show
# if the plugins added any data at all it's fine.
my @added_tags = split( ',', $plugin_result{new_tags} );
$addedtags += @added_tags;

$newtitle = $plugin_result{title};
$logger->debug("Changing title to $newtitle. (Will do nothing if title is blank)");
}
if ( exists $plugin_result{title} ) {
set_title( $id, $plugin_result{title} );

if ( exists $plugin_result{summary} ) {
set_summary( $id, $plugin_result{summary} );
$logger->debug("Summary has been changed."); # don't put the new summary in logs, it can be huge
}
$newtitle = $plugin_result{title};
$logger->debug("Changing title to $newtitle. (Will do nothing if title is blank)");
}

if ( exists $plugin_result{summary} ) {
set_summary( $id, $plugin_result{summary} );
$logger->debug("Summary has been changed."); # don't put the new summary in logs, it can be huge
}
}

Expand Down Expand Up @@ -128,25 +125,29 @@ sub exec_login_plugin {
sub exec_script_plugin {

my ( $plugin, $input, @settings ) = @_;
my $logger = get_logger( "Plugin System", "lanraragi" );

no warnings 'experimental::try';

#If the plugin has the method "run_script",
#catch all the required data and feed it to the plugin
if ( $plugin->can('run_script') ) {

my %pluginfo = $plugin->plugin_info();
my $ua = exec_login_plugin( $pluginfo{login_from} );

# Bundle all the potentially interesting info in a hash
my %infohash = (
user_agent => $ua,
oneshot_param => $input
);

# Scripts don't have any predefined metadata in their spec so they're just ran as-is.
# They can return whatever the heck they want in their hash as well, they'll just be shown as-is in the API output.
my %result = $plugin->run_script( \%infohash, @settings );
return %result;
try {
my %pluginfo = $plugin->plugin_info();
my $ua = exec_login_plugin( $pluginfo{login_from} );

# Bundle all the potentially interesting info in a hash
my %infohash = (
user_agent => $ua,
oneshot_param => $input
);

# Scripts don't have any predefined metadata in their spec so they're just ran as-is.
# They can return whatever the heck they want in their hash as well, they'll just be shown as-is in the API output.
return $plugin->run_script( \%infohash, @settings );
} catch ($e) {
return ( error => $e );
}
}
return ( error => "Plugin doesn't implement run_script despite having a 'script' type." );
}
Expand Down Expand Up @@ -193,40 +194,44 @@ sub exec_download_plugin {
sub exec_metadata_plugin {

my ( $plugin, $id, $oneshotarg, @args ) = @_;

no warnings 'experimental::try';

my $logger = get_logger( "Plugin System", "lanraragi" );

if ( $id eq 0 ) {
if ( !$id ) {
return ( error => "Tried to call a metadata plugin without providing an id." );
}

#If the plugin has the method "get_tags",
#catch all the required data and feed it to the plugin
if ( $plugin->can('get_tags') ) {
if ( !$plugin->can('get_tags') ) {
return ( error => "Plugin doesn't implement get_tags despite having a 'metadata' type." );
}

my $redis = LANraragi::Model::Config->get_redis;
my %hash = $redis->hgetall($id);
my $redis = LANraragi::Model::Config->get_redis;
my %hash = $redis->hgetall($id);

my ( $name, $title, $tags, $file, $thumbhash ) = @hash{qw(name title tags file thumbhash)};
my ( $name, $title, $tags, $file, $thumbhash ) = @hash{qw(name title tags file thumbhash)};

( $_ = LANraragi::Utils::Database::redis_decode($_) ) for ( $name, $title, $tags );
( $_ = LANraragi::Utils::Database::redis_decode($_) ) for ( $name, $title, $tags );

# If the thumbnail hash is empty or undefined, we'll generate it here.
unless ( length $thumbhash ) {
$logger->info("Thumbnail hash invalid, regenerating.");
my $thumbdir = LANraragi::Model::Config->get_thumbdir;
# If the thumbnail hash is empty or undefined, we'll generate it here.
unless ( length $thumbhash ) {
$logger->info("Thumbnail hash invalid, regenerating.");
my $thumbdir = LANraragi::Model::Config->get_thumbdir;
$thumbhash = "";

# Eval the thumbnail extraction, as it can error out and die
eval { extract_thumbnail( $thumbdir, $id, 0, 1 ) };
if ($@) {
$logger->warn("Error building thumbnail: $@");
$thumbhash = "";
} else {
$thumbhash = $redis->hget( $id, "thumbhash" );
$thumbhash = LANraragi::Utils::Database::redis_decode($thumbhash);
}
try {
extract_thumbnail( $thumbdir, $id, 0, 1 );
$thumbhash = $redis->hget( $id, "thumbhash" );
$thumbhash = LANraragi::Utils::Database::redis_decode($thumbhash);
} catch ($e) {
$logger->warn("Error building thumbnail: $e");
}
$redis->quit();
}
$redis->quit();

my %returnhash;
try {
# Hand it off to the plugin here.
# If the plugin requires a login, execute that first to get a UserAgent
my %pluginfo = $plugin->plugin_info();
Expand All @@ -243,8 +248,11 @@ sub exec_metadata_plugin {
oneshot_param => $oneshotarg
);

my %newmetadata = $plugin->get_tags( \%infohash, @args );
my %newmetadata;

%newmetadata = $plugin->get_tags( \%infohash, @args );

# TODO: remove this block after changing all the metadata plugins
#Error checking
if ( exists $newmetadata{error} ) {

Expand Down Expand Up @@ -274,11 +282,10 @@ sub exec_metadata_plugin {

# Strip last comma and return processed tags in a hash
chop($newtags);
my %returnhash = ( new_tags => $newtags );
%returnhash = ( new_tags => $newtags );

# Indicate a title change, if the plugin reports one
if ( exists $newmetadata{title} && LANraragi::Model::Config->can_replacetitles ) {

my $newtitle = $newmetadata{title};
$newtitle = trim($newtitle);
$returnhash{title} = $newtitle;
Expand All @@ -289,9 +296,11 @@ sub exec_metadata_plugin {
$returnhash{summary} = $newmetadata{summary};
}

return %returnhash;
} catch ($e) {
return ( error => $e );
}
return ( error => "Plugin doesn't implement get_tags despite having a 'metadata' type." );

return %returnhash;
}

1;
14 changes: 9 additions & 5 deletions lib/LANraragi/Plugin/Metadata/Chaika.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sub plugin_info {
type => "metadata",
namespace => "trabant",
author => "Difegue",
version => "2.3.1",
version => "2.4",
description =>
"Searches chaika.moe for tags matching your archive. This will try to use the thumbnail first, and fallback to a default text search.",
icon =>
Expand Down Expand Up @@ -66,14 +66,18 @@ sub get_tags {
# Try text search if it fails
if ( $newtags eq "" ) {
$logger->info("No results, falling back to text search.");
( $newtags, $newtitle ) =
search_for_archive( $lrr_info->{archive_title}, $lrr_info->{existing_tags}, $addextra, $addother, $addsource, $jpntitle );
( $newtags, $newtitle ) = search_for_archive(
$lrr_info->{archive_title},
$lrr_info->{existing_tags},
$addextra, $addother, $addsource, $jpntitle
);
}
}

if ( $newtags eq "" ) {
$logger->info("No matching Chaika Archive Found!");
return ( error => "No matching Chaika Archive Found!" );
my $message = "No matching Chaika Archive Found!";
$logger->info($message);
die "${message}\n";
} else {
$logger->info("Sending the following tags to LRR: $newtags");

Expand Down
9 changes: 5 additions & 4 deletions lib/LANraragi/Plugin/Metadata/ChaikaFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sub plugin_info {
type => "metadata",
namespace => "chaikafileplugin",
author => "Difegue & Plebs",
version => "0.2",
version => "0.3",
description => "Collects metadata embedded into your archives as Chaika-style api.json files",
icon =>
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA\nB3RJTUUH4wYCFQocjU4r+QAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUH\nAAAEZElEQVQ4y42T3WtTdxzGn/M7J+fk5SRpTk7TxMZkXU84tTbVNrUT3YxO7HA4pdtQZDe7cgx2\ns8vBRvEPsOwFYTDYGJUpbDI2wV04cGXCGFLonIu1L2ptmtrmxeb1JDkvv121ZKVze66f74eH7/f5\nMmjRwMCAwrt4/9KDpflMJpPHvyiR2DPcJklJ3TRDDa0xk36cvrm8vDwHAAwAqKrqjjwXecPG205w\nHBuqa9rk77/d/qJYLD7cCht5deQIIczbgiAEKLVAKXWUiqVV06Tf35q8dYVJJBJem2A7Kwi2nQzD\nZig1CG93+PO5/KN6tf5NKpVqbsBUVVVFUUxwHJc1TXNBoxojS7IbhrnLMMx9pVJlBqFQKBKPxwcB\nkJYgjKIo3QCE1nSKoghbfJuKRqN2RVXexMaQzWaLezyeEUEQD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ajUCnq+cL63fErl25s5/8baQPaWLhx6goAAAAA\nSUVORK5CYII=",
Expand Down Expand Up @@ -54,8 +54,9 @@ sub get_tags {
}

if ( $newtags eq "" ) {
$logger->info("No Chaika File Found!");
return ( error => "No Chaika File Found!" );
my $message = "No Chaika File Found!";
$logger->info($message);
die "${message}\n";
} else {

$logger->info("Sending the following tags to LRR: $newtags");
Expand All @@ -81,7 +82,7 @@ sub tags_from_file {
my $stringjson = "";

open( my $fh, '<:encoding(UTF-8)', $filepath )
or return ( error => "Could not open $filepath!" );
or die "Could not open $filepath!\n";

while ( my $row = <$fh> ) {
chomp $row;
Expand Down
Loading

0 comments on commit 8554b94

Please sign in to comment.