File Coverage

blib/lib/Starch/Plugin/LogStoreExceptions.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 19 19 100.0


line stmt bran cond sub pod time code
1             package Starch::Plugin::LogStoreExceptions;
2 8     8   5060 use 5.008001;
  8         76  
3 8     8   54 use strictures 2;
  8         72  
  8         383  
4             our $VERSION = '0.12';
5              
6             =head1 NAME
7              
8             Starch::Plugin::LogStoreExceptions - Turn Starch store exceptions into log messages.
9              
10             =head1 SYNOPSIS
11              
12             my $starch = Starch->new(
13             plugins => ['::LogStoreExceptions'],
14             ...,
15             );
16              
17             =head1 DESCRIPTION
18              
19             This plugin causes any exceptions thrown when C, C, or C is
20             called on a store to produce an error log message instead of an exception.
21              
22             Typically you'll want to use this in production, as the state store being
23             down is often not enough of a reason to produce 500 errors on every page.
24              
25             This plugin should be listed last in the plugin list so that it catches
26             exceptions produced by other plugins.
27              
28             =cut
29              
30 8     8   2019 use Try::Tiny;
  8         20  
  8         547  
31              
32 8     8   50 use Moo::Role;
  8         17  
  8         88  
33 8     8   3399 use namespace::clean;
  8         22  
  8         63  
34              
35             with qw(
36             Starch::Plugin::ForStore
37             );
38              
39             foreach my $method (qw( set get remove )) {
40             around $method => sub{
41             my $orig = shift;
42             my $self = shift;
43              
44             my @args = @_;
45              
46             return try {
47             return $self->$orig( @args );
48             }
49             catch {
50             $self->log->errorf(
51             'Starch store %s errored when %s was called: %s',
52             $self->short_store_class_name(), $method, $_,
53             );
54             return {
55             $self->manager->no_store_state_key() => 1,
56             } if $method eq 'get';
57             return;
58             };
59             };
60             }
61              
62             1;
63             __END__