#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Assert;

# This class provides an object-oriented abstraction around Importer validation errors.
# So, whenever possible, we will 'throw' one of these objects.
# This will make it easier to identify quasi-expected issues, and to provide
# better user messaging.

package Import::ValidationError;

sub new {
    my ( $class, $saleRec, $details ) = @_;
    my $self = bless {
        'saleRec' => $saleRec,
        'details' => $details,
        'caller'  => [caller],    # Store where we were called from, so we can make a useful message later
    }, $class;
    return $self;
}

sub saleRec  { shift->{saleRec} }
sub details  { shift->{details} }
sub failFile { shift->{caller}[1] }
sub failLine { shift->{caller}[2] }

# This method returns a formatted error message.
sub errorMessage {
    my ($self) = @_;

    my $msg = 'Error';
    if ( defined $self->saleRec->lineNum ) {
        $msg .= " at spreadsheet line " . $self->saleRec->lineNum;
    }

    # Add some debugging info into the error if not running on a production server
    unless ( Common::RSApp::IsProductionServer() ) {
        $msg .= ' [failed in ' . $self->failFile . ' line ' . $self->failLine . ']';
    }

    $msg .= ": " . $self->details;

    return $msg;
}

1;
