# exception_data.pm: details of the exception hierarchy used by Xapian.
package exception_data;
$copyright = <<'EOF';
/* Copyright (C) 2003,2004,2006,2007,2008,2009,2011,2015 Olly Betts
 * Copyright (C) 2007 Richard Boulton
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
EOF

use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
    $copyright $generated_warning @baseclasses @classes %subclasses %classcode
);

$generated_warning =
"/* Warning: This file is generated by $0 - do not modify directly! */\n";

@baseclasses = ();
@classes = ();
%subclasses = ();
%classcode = ();

sub errorbaseclass {
    push @baseclasses, join("\t", @_);
    push @{$subclasses{$_[1]}}, $_[0];
}

sub errorclass {
    my $typecode = shift;
    my ($class, $parent) = @_;
    push @classes, join("\t", @_);
    push @{$subclasses{$parent}}, $class;
    $classcode{$class} = $typecode;
}

errorbaseclass('LogicError', 'Error', <<'DOC');
/** The base class for exceptions indicating errors in the program logic.
 *
 *  A subclass of LogicError will be thrown if Xapian detects a violation
 *  of a class invariant or a logical precondition or postcondition, etc.
 */
DOC

errorclass(0, 'AssertionError', 'LogicError', <<'DOC');
/** AssertionError is thrown if a logical assertion inside Xapian fails.
 *
 *  In a debug build of Xapian, a failed assertion in the core library code
 *  will cause AssertionError to be thrown.
 *
 *  This represents a bug in Xapian (either an invariant, precondition, etc
 *  has been violated, or the assertion is incorrect!)
 */
DOC

errorclass(1, 'InvalidArgumentError', 'LogicError', <<'DOC');
/** InvalidArgumentError indicates an invalid parameter value was passed to the API.
*/
DOC

errorclass(2, 'InvalidOperationError', 'LogicError', <<'DOC');
/** InvalidOperationError indicates the API was used in an invalid way.
 */
DOC

errorclass(3, 'UnimplementedError', 'LogicError', <<'DOC');
/** UnimplementedError indicates an attempt to use an unimplemented feature. */
DOC

# RuntimeError and subclasses:

errorbaseclass('RuntimeError', 'Error', <<'DOC');
/** The base class for exceptions indicating errors only detectable at runtime.
 *
 *  A subclass of RuntimeError will be thrown if Xapian detects an error
 *  which is exception derived from RuntimeError is thrown when an
 *  error is caused by problems with the data or environment rather
 *  than a programming mistake.
 */
DOC

errorclass(4, 'DatabaseError', 'RuntimeError', <<'DOC');
/** DatabaseError indicates some sort of database related error. */
DOC

errorclass(5, 'DatabaseCorruptError', 'DatabaseError', <<'DOC');
/** DatabaseCorruptError indicates database corruption was detected. */
DOC

errorclass(6, 'DatabaseCreateError', 'DatabaseError', <<'DOC');
/** DatabaseCreateError indicates a failure to create a database. */
DOC

errorclass(7, 'DatabaseLockError', 'DatabaseError', <<'DOC');
/** DatabaseLockError indicates failure to lock a database. */
DOC

errorclass(8, 'DatabaseModifiedError', 'DatabaseError', <<'DOC');
/** DatabaseModifiedError indicates a database was modified.
 *
 *  To recover after catching this error, you need to call
 *  Xapian::Database::reopen() on the Database and repeat the operation
 *  which failed.
 */
DOC

errorclass(9, 'DatabaseOpeningError', 'DatabaseError', <<'DOC');
/** DatabaseOpeningError indicates failure to open a database. */
DOC

errorclass(10, 'DatabaseVersionError', 'DatabaseOpeningError', <<'DOC');
/** DatabaseVersionError indicates that a database is in an unsupported format.
 *
 *  From time to time, new versions of Xapian will require the database format
 *  to be changed, to allow new information to be stored or new optimisations
 *  to be performed.  Backwards compatibility will sometimes be maintained, so
 *  that new versions of Xapian can open old databases, but in some cases
 *  Xapian will be unable to open a database because it is in too old (or new)
 *  a format.  This can be resolved either be upgrading or downgrading the
 *  version of Xapian in use, or by rebuilding the database from scratch with
 *  the current version of Xapian.
 */
DOC

errorclass(11, 'DocNotFoundError', 'RuntimeError', <<'DOC');
/** Indicates an attempt to access a document not present in the database. */
DOC

errorclass(12, 'FeatureUnavailableError', 'RuntimeError', <<'DOC');
/** Indicates an attempt to use a feature which is unavailable.
 *
 *  Typically a feature is unavailable because it wasn't compiled in, or
 *  because it requires other software or facilities which aren't available.
 */
DOC

errorclass(13, 'InternalError', 'RuntimeError', <<'DOC');
/** InternalError indicates a runtime problem of some sort. */
DOC

errorclass(14, 'NetworkError', 'RuntimeError', <<'DOC');
/** Indicates a problem communicating with a remote database. */
DOC

errorclass(15, 'NetworkTimeoutError', 'NetworkError', <<'DOC');
/** Indicates a timeout expired while communicating with a remote database. */
DOC

errorclass(16, 'QueryParserError', 'RuntimeError', <<'DOC');
/** Indicates a query string can't be parsed. */
DOC

errorclass(17, 'SerialisationError', 'RuntimeError', <<'DOC');
/** Indicates an error in the std::string serialisation of an object. */
DOC

errorclass(18, 'RangeError', 'RuntimeError', <<'DOC');
/** RangeError indicates an attempt to access outside the bounds of a container.
 */
DOC

errorclass(19, 'WildcardError', 'RuntimeError', <<'DOC');
/** WildcardError indicates an error expanding a wildcarded query.
 */
DOC

sub for_each_nothrow {
    my $func = shift @_;
    my $class = '';
    foreach my $header ('include/xapian.h', <include/xapian/*.h>) {
	local $/ = undef;
	open H, '<', $header or die $!;
	my $header_text = <H>;
	# Strip comments, which might contain text describing XAPIAN_NOTHROW().
	$header_text =~ s!/(?:/[^\n]*|\*.*?\*/)! !gs;
	for (split /\n/, $header_text) {
	    if (/^\s*class\s+XAPIAN_VISIBILITY_DEFAULT\s+(\w+)/) {
		$class = "$1::";
		next;
	    }
	    if (/^[^#]*\bXAPIAN_NOTHROW\((.*)\)/) {
		&$func("Xapian::$class$1");
	    }
	}
	close H;
    }
}

1;
