Merror
1.3
Merror is a class implementing a mechanism to handle errors OOP style which means that it gaves you the ability to easily set or get error codes, error descriptions and gives you the ability to print out a stacktrace.
Markus Mazurczak <
coding@markus-mazurczak.de>
If you find any bugs or you want to have some features included register at
http://mantis.markus-mazurczak.de
Installation must be done as with every other standard perl module installed manually:
- perl Makefile.PL
- make
- make test
- make install
If yout want to install that module in some other than your perl standard paths specify PREFIX=.
e.g.:
- perl Makefile.PL PREFIX=/home/foouser/perllib
Lets consider we created the following errormapping file named "errorcodes" in /home/foobar/test:
- 0: Test error
- 1: Another test error
- 2: Yet another one
- #3: This will never be printed out
Lets create the following test perl module:
package test:
BEGIN {
use strict;
use warnings;
use File::Spec;
use File::Basename;
}
use Merror;
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
MERROR => Merror->new(stackdepth => 16, errorfile => "/home/foobar/test/errorcodes", ramusage => 1);
TEXT => "Test messages";
};
bless $self, $class;
return($self);
}
sub tellme {
my $self = shift;
my $errorcode = shift;
#indicate that an error happened
$self->{MERROR}->error(1);
#set an error code and the mapped description
$self->{MERROR}->mappedError($errorcode);
}
sub tellmethesecond {
my $self = shift;
#indicate that an error happened
$self->{MERROR}->error(1);
#set an error code
$self->{MERROR}->ec($errorcode);
#set the error description
$self->{MERROR}->et("This message will be the error description");
}
1;
Now lets create a file foo.pl in /home/foobar/test
#!/usr/bin/perl
BEGIN {
use strict;
use warnings;
use Merror;
}
use test;
my $foobar = Test->new();
$foobar->tellmethesecond();
if($foobar->{MERROR}->error()) {
#print out the error code
print("Errorcode: " . $foobar->{MERROR}->ec . "\n");
#print out the error description
print("Errordescription: " . $foobar->{MERROR}->et . "\n");
#print out a stacktrace
print("Stacktrace: \n");
$foobar->{MERROR}->stacktrace;
}
$foobar->tellme();
if($foobar->{MERROR}->error(1)) {
#print out the error code
print("Errorcode: " . $foobar->{MERROR}->ec . "\n");
#print out the error description
print("Errordescription: " . $foobar->{MERROR}->et . "\n");
#print out a stacktrace
print("Stacktrace: \n");
$foobar->{MERROR}->stacktrace;
}
Every line of the errorfile consists out of 3 parts.
- Errornumber
- :
- Errordescription
It does not matter if there are any whitespaces between the parts, they will be substituted. The perl regexp looks like the following:
/^(\d{1,})\s{0,}:\s{0,}(.*)/