File: ErrorHandler.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (117 lines) | stat: -rw-r--r-- 2,914 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

package MT::ErrorHandler;
use strict;

use vars qw( $ERROR );

sub new { bless {}, shift }

sub error {
    my $class = shift;
    my $msg = @_ ? $_[0] : '';
    if ( defined $msg ) {
        $msg .= "\n" if ( $msg ne '' ) && ( $msg !~ /\n$/ );
    }
    if ( ref($class) ) {
        $class->{_errstr} = $msg;
    }
    else {
        $ERROR = $msg;
    }
    return;
}

sub trans_error {
    my $obj = shift;
    return $obj->error(
        $obj->can('translate') ? $obj->translate(@_) : MT->translate(@_) );
}

sub errstr { ref( $_[0] ) ? $_[0]->{_errstr} : $ERROR }

1;
__END__


=head1 NAME

MT::ErrorHandler - MT error handling

=head1 SYNOPSIS

    package Foo;
    use MT::ErrorHandler;
    @Foo::ISA = qw( MT::ErrorHandler );

    sub class_method {
        my $class = shift;
        ...
        return $class->error("Help!")
            unless $continue;
    }

    sub object_method {
        my $obj = shift;
        ...
        return $obj->error("I am no more")
            unless $continue;
    }

    package main;
    use Foo;

    Foo->class_method or die Foo->errstr;

    my $foo = Foo->new;
    $foo->object_method or die $foo->errstr;

=head1 DESCRIPTION

I<MT::ErrorHandler> provides an error-handling mechanism for all <MT>
modules/classes. It is meant to be used as a base class for classes that wish
to use its error-handling methods: derived classes use its two methods,
I<error> and I<errstr>, to communicate error messages back to the calling
program.

On failure (for whatever reason), a subclass should call I<error> and return
to the caller; I<error> itself sets the error message internally, then returns
C<undef>. This has the effect of the method internally, then returns C<undef>.
This has the effect of the method that failed returning C<undef> to the
caller. The caller should check for errors by checking for a return value of
C<undef>, and in this case should call I<errstr> to get the value of the error
message. Note that calling I<errstr> when an error has not occurred is
undefined behavior and will I<rarely> do what you want.

As demonstrated in the I<SYNOPSIS> (above), I<error> and I<errstr> work both
as class methods and as object methods.

=head1 USAGE

=head2 Class->new

Constructs a new I<MT::ErrorHandler> instance.

=head2 Class->error($message)

=head2 $object->error($message)

Sets the error message for either the class I<Class> or the object
I<$object> to the message I<$message>. Returns C<undef>.

=head2 Class->errstr

=head2 $object->errstr

Accesses the last error message set in the class I<Class> or the
object I<$object>, respectively, and returns that error message.

=head1 AUTHOR & COPYRIGHTS

Please see the I<MT> manpage for author, copyright, and license information.

=cut