| 1 | package EntityModel::Error; | ||||||||||||
| 2 | 3 | $EntityModel::Error::VERSION = '0.016'; | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | =head1 NAME | ||||||||||||
| 7 | |||||||||||||
| 8 | EntityModel::Error - generic error object | ||||||||||||
| 9 | |||||||||||||
| 10 | =head1 VERSION | ||||||||||||
| 11 | |||||||||||||
| 12 | Version 0.016 | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||
| 15 | |||||||||||||
| 16 | Uses some overload tricks and C< AUTOLOAD > to allow chained method calls without needing to wrap in eval. | ||||||||||||
| 17 | |||||||||||||
| 18 | =head1 METHODS | ||||||||||||
| 19 | |||||||||||||
| 20 | =cut | ||||||||||||
| 21 | |||||||||||||
| 22 | 3 | ▶ | use EntityModel::Log ':all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 3 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | use overload | ||||||||||||
| 26 | 3 | ▶ | 'bool' => sub { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 27 | 0 | my $self = shift; | |||||||||||
| 28 | logWarning('Error: [%s], chain was [%s]', | ||||||||||||
| 29 | Data::Dumper::Dumper($self->{message}), | ||||||||||||
| 30 | join(',', map { | ||||||||||||
| 31 | 0 | ▶ | $_->{method} // 'unknown' | ||||||||||
|
Condition0%0/2
$_->{"method"} // "unknown"
MC/DC0%0/1
$_->{"method"} // "unknown"
$_->{"method"}
Truth table0%0/2
A
A= $_->{"method"}
| |||||||||||||
| 32 | 0 | } @{ $self->{chain} }) | |||||||||||
| 33 | ); | ||||||||||||
| 34 | 0 | return 0; | |||||||||||
| 35 | }, | ||||||||||||
| 36 | 0 | ▶ | 'ne' => sub { 1 }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 37 | 0 | ▶ | 'eq' => sub { 0 }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 38 | 3 | 'fallback' => 1; | |||||||||||
| 39 | |||||||||||||
| 40 | =head2 new | ||||||||||||
| 41 | |||||||||||||
| 42 | Instantiate a new L<EntityModel::Error> object. Takes the following parameters: | ||||||||||||
| 43 | |||||||||||||
| 44 | =over 4 | ||||||||||||
| 45 | |||||||||||||
| 46 | =item * $parent - the parent error which raised this one | ||||||||||||
| 47 | |||||||||||||
| 48 | =item * $msg - error message, string | ||||||||||||
| 49 | |||||||||||||
| 50 | =item * $opt (optional) - hashref of options | ||||||||||||
| 51 | |||||||||||||
| 52 | =back | ||||||||||||
| 53 | |||||||||||||
| 54 | =cut | ||||||||||||
| 55 | |||||||||||||
| 56 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 57 | 0 | my ($class, $parent, $msg, $opt) = @_; | |||||||||||
| 58 | 0 | ▶ | $msg = $parent if @_ < 3; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 0 | ▶ | $opt ||= { }; | ||||||||||
|
Condition0%0/2
$opt ||= {}
MC/DC0%0/1
$opt ||= {}
$opt
Truth table0%0/2
A
A= $opt
| |||||||||||||
| 60 | |||||||||||||
| 61 | 0 | ▶ | logWarning($msg) if $opt->{warning}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 0 | ▶ | logError($msg) if $opt->{error}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 0 | logInfo("Had error [%s] from %S", $msg); | |||||||||||
| 64 | |||||||||||||
| 65 | 0 | my $self = bless { | |||||||||||
| 66 | message => $msg, | ||||||||||||
| 67 | parent => $parent, | ||||||||||||
| 68 | chain => [ ] | ||||||||||||
| 69 | }, $class; | ||||||||||||
| 70 | 0 | return $self; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | 3 | our $AUTOLOAD; | |||||||||||
| 74 | |||||||||||||
| 75 | 0 | ▶ | sub AUTOLOAD { | ||||||||||
|
Subroutine
AUTOLOAD: not called CC=3 SCAR=24.8
| |||||||||||||
| 76 | 0 | my $self = shift; | |||||||||||
| 77 | 0 | my ($method) = $AUTOLOAD; | |||||||||||
| 78 | 0 | $method =~ s/^.*:://g; | |||||||||||
| 79 | 0 | ▶ | return if $method eq 'DESTROY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | |||||||||||||
| 81 | logWarning('Bad method [%s] called in error, original message [%s] with object [%s]', | ||||||||||||
| 82 | $method, | ||||||||||||
| 83 | $self->{message}, | ||||||||||||
| 84 | $self->{parent} | ||||||||||||
| 85 | 0 | ▶ | ) unless eval { $self->{parent}->can($method) }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | |||||||||||||
| 87 | 0 | push @{$self->{chain}}, {method => $method }; | |||||||||||
| 88 | 0 | return $self; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 3 | 1; | |||||||||||
| 92 | |||||||||||||
| 93 | __END__ | ||||||||||||