Skip to content

Commit

Permalink
Item14237: Split Foswiki::Exception::Config into idividual modules
Browse files Browse the repository at this point in the history
Added more docs.
  • Loading branch information
vrurg committed Oct 13, 2017
1 parent c0e5386 commit a6ce4cd
Show file tree
Hide file tree
Showing 20 changed files with 893 additions and 252 deletions.
3 changes: 2 additions & 1 deletion core/lib/Foswiki/Config/Spec/Format/legacy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ foreach my $type ( sort $nodeClass->getAllTypes ) {
package Foswiki::Exception::Config::Spec::Format::legacy::Flow;

use Foswiki::Class;
extends qw(Foswiki::Exception::Harmless);
extends qw(Foswiki::Exception::Config);
with qw(Foswiki::Exception::Harmless);

# UpSection is to signal when to cancel processing and return control to the
# upper level stack frame.
Expand Down
2 changes: 1 addition & 1 deletion core/lib/Foswiki/Configure/Query.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ sub _getSetParams {
}
catch {
my $e = Foswiki::Exception::Fatal->transmute( $_, 0 );
if ( $e->isa('Foswiki::Exception::Fatal') ) {
if ( $e->DOES('Foswiki::Exception::Deadly') ) {
$reporter->ERROR(
"The value of $k was unreadable: <verbatim>"
. Foswiki::Configure::Reporter::stripStacktrace(
Expand Down
20 changes: 5 additions & 15 deletions core/lib/Foswiki/Exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Basic principles behind exceptions:
longer recommended.
1 Exception classes are inheriting from =Foswiki::Exception=.
1 =Foswiki::Exception= is an integral part of Fowiki's OO system and
inheriting from =Foswiki::Object=.
inherits from =%PERLDOC{Foswiki::Object}%=.
1 =Foswiki::Exception= is utilizing =CPAN:Throwable= role. Requires the
module to be installed.
1 Exception classes inheritance shall form a tree of relationships for
Expand Down Expand Up @@ -354,7 +354,7 @@ sub rethrowAs {
Reinstantiates $exception into $class.
If =$enforce= is *FALSE* and =$exception='s class is a =Foswiki::Exception=
If =$enforce= is *FALSE* and =$exception= is a =Foswiki::Exception=
descendant then no action would be taken. If =$enforce= is true then no matter
what the =$exception= type is - it would be coerced into =$class=.
Expand Down Expand Up @@ -479,19 +479,9 @@ sub prepareText {
return "text attribute hasn't been set";
}

#use Foswiki::Exception::Ext;
#use Foswiki::Exception::ASSERT;
#use Foswiki::Exception::Fatal;
#use Foswiki::Exception::FileOp;
#use Foswiki::Exception::HTTPResponse;
#use Foswiki::Exception::HTTPError;
#use Foswiki::Exception::Engine;

END {
use Foswiki;
for my $m (qw<Ext ASSERT Fatal FileOp HTTPResponse HTTPError Engine>) {
Foswiki::load_class("Foswiki::Exception::$m");
}
use Foswiki;
for my $m (qw<Ext ASSERT Fatal FileOp HTTPResponse HTTPError Engine>) {
Foswiki::load_class( __PACKAGE__ . "::" . $m );
}

1;
Expand Down
202 changes: 4 additions & 198 deletions core/lib/Foswiki/Exception/Config.pm
Original file line number Diff line number Diff line change
@@ -1,211 +1,17 @@
# See bottom of file for license and copyright information

package Foswiki::Exception::Config;
use Foswiki::Exception;

package Foswiki::Exception::Config::NoNextDef;

use Foswiki::Class;
extends qw<Foswiki::Exception>;
with qw<Foswiki::Exception::Harmless>;

# Role to prefix exception text with source file info.
package Foswiki::Exception::Config::SrcFile;

use Moo::Role;

has srcFile => ( is => 'ro', );
has srcLine => ( is => 'ro', );

sub sourceInfo {
my $this = shift;

my $file = $this->srcFile;
if ( UNIVERSAL::isa( $file, 'Foswiki::File' ) ) {
$file = $file->path;
}
if ( $file && defined $this->srcLine ) {
$file .= ":" . $this->srcLine;
}

return $file // '';
}

package Foswiki::Exception::Config::BadSpec;

use Foswiki::Class;
extends qw(Foswiki::Exception::Fatal);

has section => (
is => 'rw',
lazy => 1,
predicate => 1,
builder => 'prepareSection',
);
has key => (
is => 'rw',
lazy => 1,
predicate => 1,
builder => 'prepareKey',
);
has nodeObject => (
is => 'rw',
predicate => 1,
trigger => 1,
);

sub BUILD {
my $this = shift;

$this->_setFromNodeObject;
}

around stringify => sub {
my $orig = shift;
my $this = shift;
# Preload exceptions

my $nodeObject = $this->has_nodeObject ? $this->nodeObject : undef;

# TODO Report sources too.
my $keyInfo = $this->has_key
|| $nodeObject ? "key '" . $this->key . "' is " : "";

my $sourceObject =
defined $nodeObject
? $nodeObject
: ( $this->has_section ? $this->section : undef );
my $sourceInfo = '';
if ( $sourceObject && @{ $sourceObject->sources } > 0 ) {
$sourceInfo = " at "
. join( ", ",
map { $_->{file} . ( defined $_->{line} ? ":" . $_->{line} : "" ) }
@{ $sourceObject->sources } );
}

my $sectionInfo =
$this->has_section || $this->has_nodeObject
? " (${keyInfo}defined in section '"
. $this->section . "'"
. $sourceInfo . ")"
: '';

return $this->stringifyText . $sectionInfo . $this->stringifyPostfix;
};

sub prepareSection {
my $this = shift;

if ( $this->has_nodeObject ) {
return $this->nodeObject->section;
}
use Foswiki;
for my $m (qw<BadSpecData BadSpecSrc BadSpecValue InvalidKeyName NoNextDef>) {
Foswiki::load_class( __PACKAGE__ . "::" . $m );
}

sub prepareKey {
my $this = shift;

if ( $this->has_nodeObject ) {
return $this->nodeObject->fullName;
}
}

# Sets key and section from nodeObject
sub _setFromNodeObject {
my $this = shift;

if ( $this->has_nodeObject ) {

# Set section and key attrs manually if not set by user. This is to get
# around a problem where exception is propagaded out of scope where
# nodeObject's parent is defined making its fullName/fullPath methods
# useless.
unless ( $this->has_key ) {
$this->key( $this->nodeObject->fullName );
}
unless ( $this->has_section ) {
$this->section( $this->nodeObject->section );
}
}
}

sub _trigger_nodeObject {
my $this = shift;

$this->_setFromNodeObject;
}

package Foswiki::Exception::Config::BadSpecData;

use Foswiki::Class;
extends qw(Foswiki::Exception::Config::BadSpec);
with qw(Foswiki::Exception::Config::SrcFile);

around stringifyPostfix => sub {
my $orig = shift;
my $this = shift;

my $errMsg = $orig->( $this, @_ );

if ( defined $this->srcFile ) {
$errMsg = " from " . $this->sourceInfo . " " . $errMsg;
}

return $errMsg;
};

package Foswiki::Exception::Config::BadSpecValue;

use Foswiki::Class;
extends qw(Foswiki::Exception::Config::BadSpec);
with qw(Foswiki::Exception::Config::SrcFile);

package Foswiki::Exception::Config::BadSpecSrc;

use Foswiki::Class;
extends qw(Foswiki::Exception::Fatal);
with qw(Foswiki::Exception::Config::SrcFile);

has '+srcFile' => (
is => 'ro',
required => 1,
);

around stringifyText => sub {
my $orig = shift;
my $this = shift;

my $errMsg = $orig->( $this, @_ );

return "Failed to parse specs file " . $this->sourceInfo . ": " . $errMsg;
};

=begin TML
---+ Exception Foswiki::Exception::Config::InvalidKeyName
If configuration key doesn't pass validation.
=cut

package Foswiki::Exception::Config::InvalidKeyName;
use Foswiki::Class;
extends qw(Foswiki::Exception::Fatal);

has keyName => ( is => 'rw', required => 1, );

around stringifyText => sub {
my $orig = shift;
my $this = shift;
my ($text) = @_;

my $errMsg = $orig->( $this, @_ );
my $key = $this->keyName;

$errMsg .= " (the key is:"
. ( defined $key ? ( ref($key) || $key ) : '*undef*' ) . ")";

return $errMsg;
};

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Expand Down
Loading

0 comments on commit a6ce4cd

Please sign in to comment.