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 (21 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_5_msu, version_2_11_4_msu, HEAD
- Add $Id$ line in comments for display of version.

# The LearningOnline Network with CAPA - LON-CAPA
# Calculation exception
#
# $Id: CalcException.pm,v 1.2 2023/03/13 22:31:22 raeburn Exp $
#
# Copyright (C) 2014 Michigan State University Board of Trustees
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

##
# Calculation exception
##
package Apache::math_parser::CalcException;

use strict;
use warnings;
use utf8;

use Apache::lonlocal;

use overload '""' => \&toString;

##
# Constructor
# @param {string} msg - error message, using [_1] for the first parameter
# @param {...string} param - parameters for the message
##
sub new {
    my $class = shift;
    my $self = {
        _msg => shift,
        _params => [],
    };
    while (@_) {
        push(@{$self->{_params}}, shift);
    }
    bless $self, $class;
    return $self;
}

# Attribute helpers

##
# Error message, using [_1] for the first parameter.
# @returns {string}
##
sub msg {
    my $self = shift;
    return $self->{_msg};
}

##
# Parameters for the message.
# @returns {string[]}
##
sub params {
    my $self = shift;
    return $self->{_params};
}


##
# Returns the exception as a string, for debug only.
# @returns {string}
##
sub toString {
    my $self = shift;
    my $s = "Calculation error: ".$self->msg;
    if (scalar(@{$self->params}) > 0) {
        $s .= ", ".join(", ", @{$self->params});
    }
    return $s;
}

##
# Returns the error message localized for the user interface.
# @returns {string}
##
sub getLocalizedMessage {
    my $self = shift;
    return mt($self->msg, @{$self->params});
}

1;
__END__

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