| 1 | package Workflow::Exception; | ||||||||||
| 2 | |||||||||||
| 3 | 58 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 58 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 58 | ▶ | use v5.14.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | # Declare some of our exceptions... | ||||||||||
| 8 | |||||||||||
| 9 | 58 | ▶ | use Exception::Class ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 58 | 'Workflow::Exception::Configuration' => { | |||||||||
| 11 | isa => 'Workflow::Exception', | ||||||||||
| 12 | description => 'Configuration errors', | ||||||||||
| 13 | }, | ||||||||||
| 14 | 'Workflow::Exception::Persist' => { | ||||||||||
| 15 | isa => 'Workflow::Exception', | ||||||||||
| 16 | description => 'Persistence errors', | ||||||||||
| 17 | }, | ||||||||||
| 18 | 'Workflow::Exception::Validation' => { | ||||||||||
| 19 | isa => 'Workflow::Exception', | ||||||||||
| 20 | description => 'Validation errors', | ||||||||||
| 21 | fields => 'invalid_fields', | ||||||||||
| 22 | }, | ||||||||||
| 23 | 58 | ); | |||||||||
| 24 | |||||||||||
| 25 | 58 | ▶ | use Log::Any; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 58 | my %TYPE_CLASSES = ( | |||||||||
| 28 | configuration_error => 'Workflow::Exception::Configuration', | ||||||||||
| 29 | persist_error => 'Workflow::Exception::Persist', | ||||||||||
| 30 | validation_error => 'Workflow::Exception::Validation', | ||||||||||
| 31 | workflow_error => 'Workflow::Exception', | ||||||||||
| 32 | ); | ||||||||||
| 33 | 58 | my %TYPE_LOGGING = ( | |||||||||
| 34 | configuration_error => 'error', | ||||||||||
| 35 | persist_error => 'error', | ||||||||||
| 36 | validation_error => 'info', | ||||||||||
| 37 | workflow_error => 'error', | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | 58 | $Workflow::Exception::VERSION = '2.12'; | |||||||||
| 42 | 58 | @Workflow::Exception::ISA = qw( Exporter Exception::Class::Base ); | |||||||||
| 43 | 58 | @Workflow::Exception::EXPORT_OK = keys %TYPE_CLASSES; | |||||||||
| 44 | |||||||||||
| 45 | # Exported shortcuts | ||||||||||
| 46 | |||||||||||
| 47 | 28 | ▶ | sub _mythrow { | ||||||||
|
Subroutine
_mythrow: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 28 | my ( $type, @items ) = @_; | |||||||||
| 49 | |||||||||||
| 50 | 28 | my ( $msg, %params ) = _massage(@items); | |||||||||
| 51 | 28 | my $caller = caller; | |||||||||
| 52 | 28 | my $log = Log::Any->get_logger( category => $caller ); # log as if part of the package of the caller | |||||||||
| 53 | 28 | my ( $pkg, $line ) = (caller)[ 0, 2 ]; | |||||||||
| 54 | 28 | my ( $prev_pkg, $prev_line ) = ( caller 1 )[ 0, 2 ]; | |||||||||
| 55 | |||||||||||
| 56 | # Do not log condition errors | ||||||||||
| 57 | 28 | my $method = $TYPE_LOGGING{$type}; | |||||||||
| 58 | 28 | $log->$method( | |||||||||
| 59 | "$type exception thrown from [$pkg: $line; before: ", | ||||||||||
| 60 | "$prev_pkg: $prev_line]: $msg" | ||||||||||
| 61 | ); | ||||||||||
| 62 | |||||||||||
| 63 | goto &Exception::Class::Base::throw( | ||||||||||
| 64 | 28 | $TYPE_CLASSES{$type}, | |||||||||
| 65 | message => $msg, | ||||||||||
| 66 | %params | ||||||||||
| 67 | ); | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | # Use 'goto' here to maintain the stack trace | ||||||||||
| 71 | |||||||||||
| 72 | 11 | ▶ | sub configuration_error { | ||||||||
|
Subroutine
configuration_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 11 | unshift @_, 'configuration_error'; | |||||||||
| 74 | 11 | goto &_mythrow; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 6 | ▶ | sub persist_error { | ||||||||
|
Subroutine
persist_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 78 | 6 | unshift @_, 'persist_error'; | |||||||||
| 79 | 6 | goto &_mythrow; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 4 | ▶ | sub validation_error { | ||||||||
|
Subroutine
validation_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 83 | 4 | unshift @_, 'validation_error'; | |||||||||
| 84 | 4 | goto &_mythrow; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 7 | ▶ | sub workflow_error { | ||||||||
|
Subroutine
workflow_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 88 | 7 | unshift @_, 'workflow_error'; | |||||||||
| 89 | 7 | goto &_mythrow; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | # Override 'throw' so we can massage the message and parameters into | ||||||||||
| 93 | # the right format for E::C | ||||||||||
| 94 | |||||||||||
| 95 | 1 | ▶ | sub throw { | ||||||||
|
Subroutine
throw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 96 | 1 | my ( $class, @items ) = @_; | |||||||||
| 97 | |||||||||||
| 98 | 1 | my ( $msg, %params ) = _massage(@items); | |||||||||
| 99 | 1 | goto &Exception::Class::Base::throw( $class, message => $msg, %params ); | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 29 | ▶ | sub _massage { | ||||||||
|
Subroutine
_massage: called CC=2 SCAR=6.9
| |||||||||||
| 103 | 29 | my @items = @_; | |||||||||
| 104 | |||||||||||
| 105 | 29 | ▶ | my %params = ( ref $items[-1] eq 'HASH' ) ? %{ pop @items } : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 29 | my $msg = join '', @items; | |||||||||
| 107 | 29 | $msg =~ s/\\n/ /g; # don't log newlines as per Log4perl recommendations | |||||||||
| 108 | 29 | return ( $msg, %params ); | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 58 | 1; | |||||||||
| 112 | |||||||||||
| 113 | __END__ | ||||||||||