Generic error handling example.
#!/usr/local/bin/perl
##############################################################################
# file: common_error.pl #
# 'msparser' toolkit example code #
##############################################################################
# COPYRIGHT NOTICE #
# Copyright 1998-2010 Matrix Science Limited All Rights Reserved. #
# #
##############################################################################
# $Source: parser/examples/test_perl/common_error.pl $ #
# $Author: villek@matrixscience.com $ #
# $Date: 2018-07-30 16:23:53 +0100 $ #
# $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_3_0_0-2024-09-24-0-g93ebaeb4f4 $ #
# $NoKeywords:: $ #
##############################################################################
use strict;
##############################################################################
use msparser;
my $file = new msparser::ms_datfile;
$file->setFileName(defined($ARGV[0]) ? $ARGV[0] : 'wrong_name.txt');
$file->read_file();
if ($file->isValid) {
print "The file has been read and parsed successfully. Congratulations!\n";
} else {
checkErrorHandler($file);
}
sub checkErrorHandler {
my ($obj) = @_;
print "Last error description \n";
print "=========================================\n";
print "Error: ", $obj->getLastErrorString(), "\n";
print "=========================================\n";
print "Testing the error handling... \n";
print "=========================================\n";
my $err = $obj->getErrorHandler();
my $i;
for my $i (1 .. $err->getNumberOfErrors) {
print "Error number: ";
print $err->getErrorNumber($i);
print " (";
print $err->getErrorRepeats($i) + 1;
print " times) : ";
print $err->getErrorString($i), "\n";
}
print "\n";
$obj->clearAllErrors();
}
=pod
Running the program with no arguments, for example with
perl -I../bin common_error.pl
will give the output:
Last error description
=========================================
Error: Cannot find Mascot configuration file 'wrong_name.txt'.
=========================================
Testing the error handling...
=========================================
Error number: 1537 (1 times) : Cannot find Mascot configuration file 'wrong_name.txt'.
=cut