File:  [LON-CAPA] / loncom / homework / math_parser / CalcException.pm
Revision 1.2: download - view: text, annotated - select for diffs
Mon Mar 13 22:31:22 2023 UTC (14 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_4_msu, HEAD
- Add $Id$ line in comments for display of version.

    1: # The LearningOnline Network with CAPA - LON-CAPA
    2: # Calculation exception
    3: #
    4: # $Id: CalcException.pm,v 1.2 2023/03/13 22:31:22 raeburn Exp $
    5: #
    6: # Copyright (C) 2014 Michigan State University Board of Trustees
    7: #
    8: # This program is free software: you can redistribute it and/or modify
    9: # it under the terms of the GNU General Public License as published by
   10: # the Free Software Foundation, either version 3 of the License, or
   11: # (at your option) any later version.
   12: #
   13: # This program is distributed in the hope that it will be useful,
   14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   16: # GNU General Public License for more details.
   17: #
   18: # You should have received a copy of the GNU General Public License
   19: # along with this program. If not, see <http://www.gnu.org/licenses/>.
   20: #
   21: 
   22: ##
   23: # Calculation exception
   24: ##
   25: package Apache::math_parser::CalcException;
   26: 
   27: use strict;
   28: use warnings;
   29: use utf8;
   30: 
   31: use Apache::lonlocal;
   32: 
   33: use overload '""' => \&toString;
   34: 
   35: ##
   36: # Constructor
   37: # @param {string} msg - error message, using [_1] for the first parameter
   38: # @param {...string} param - parameters for the message
   39: ##
   40: sub new {
   41:     my $class = shift;
   42:     my $self = {
   43:         _msg => shift,
   44:         _params => [],
   45:     };
   46:     while (@_) {
   47:         push(@{$self->{_params}}, shift);
   48:     }
   49:     bless $self, $class;
   50:     return $self;
   51: }
   52: 
   53: # Attribute helpers
   54: 
   55: ##
   56: # Error message, using [_1] for the first parameter.
   57: # @returns {string}
   58: ##
   59: sub msg {
   60:     my $self = shift;
   61:     return $self->{_msg};
   62: }
   63: 
   64: ##
   65: # Parameters for the message.
   66: # @returns {string[]}
   67: ##
   68: sub params {
   69:     my $self = shift;
   70:     return $self->{_params};
   71: }
   72: 
   73: 
   74: ##
   75: # Returns the exception as a string, for debug only.
   76: # @returns {string}
   77: ##
   78: sub toString {
   79:     my $self = shift;
   80:     my $s = "Calculation error: ".$self->msg;
   81:     if (scalar(@{$self->params}) > 0) {
   82:         $s .= ", ".join(", ", @{$self->params});
   83:     }
   84:     return $s;
   85: }
   86: 
   87: ##
   88: # Returns the error message localized for the user interface.
   89: # @returns {string}
   90: ##
   91: sub getLocalizedMessage {
   92:     my $self = shift;
   93:     return mt($self->msg, @{$self->params});
   94: }
   95: 
   96: 1;
   97: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>