# generate-generic-exceptions: generate generic error handling code
my $copyright = <<'END';
 Copyright (C) 2004,2005,2006,2007,2011,2012,2018 Olly Betts
 Copyright (C) 2007 Lemur Consulting Ltd

 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, see
 <https://www.gnu.org/licenses/>.
END

use strict;
use exception_data;

mkdir "generic";
open FD, ">generic/except.i" or die $!;

$copyright =~ s/^/ */mg;

print FD <<"EOF";
%{
/** \@file
 * \@brief Language independent exception handling.
 */
/* Warning: This file is generated by $0
 * - do not modify directly!
 *
$copyright */

/* This file is included for any languages which don't have language specific
 * handling for exceptions.
 */

EOF

print FD <<'EOF';
#include <exception>

static int XapianExceptionHandler(string & msg) {
    try {
	// Rethrow so we can look at the exception if it was a Xapian::Error.
	throw;
    } catch (const Xapian::Error &e) {
	msg = e.get_description();
	try {
	    // Re-rethrow the previous exception so we can handle the type in a
	    // fine-grained way, but only in one place to avoid bloating the
	    // file.
	    throw;
	} catch (const Xapian::InvalidArgumentError &) {
	    return SWIG_ValueError;
	} catch (const Xapian::RangeError &) {
	    return SWIG_IndexError;
	} catch (const Xapian::DatabaseError &) {
	    return SWIG_IOError;
	} catch (const Xapian::NetworkError &) {
	    return SWIG_IOError;
	} catch (const Xapian::InternalError &) {
	    return SWIG_RuntimeError;
	} catch (const Xapian::RuntimeError &) {
	    return SWIG_RuntimeError;
	} catch (...) {
	    return SWIG_UnknownError;
	}
    } catch (const std::exception &e) {
	msg = "std::exception: ";
        msg += e.what();
    } catch (...) {
	msg = "unknown error in Xapian";
    }
    return SWIG_UnknownError;
}
%}

/* Functions and methods which are marked as "nothrow": */
EOF

chdir($INC[0]);
exception_data::for_each_nothrow(sub { print FD "%exception $_[0];\n" });

print FD <<'EOF';

%exception {
    try {
	$function
    } catch (...) {
	string msg;
	int code = XapianExceptionHandler(msg);
	SWIG_exception(code, msg.c_str());
    }
}
EOF

close FD or die $!;
