Sync common code between configure.ac files
[xapian.git] / xapian-bindings / generic / generate-generic-exceptions
blobb8de2fbb09bfed08abd8ef61bc6a00b98a2f51e6
1 # generate-generic-exceptions: generate generic error handling code
2 my $copyright = <<'END';
3  Copyright (C) 2004,2005,2006,2007,2011,2012,2018 Olly Betts
4  Copyright (C) 2007 Lemur Consulting Ltd
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of the
9  License, or (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 END
21 use strict;
22 use exception_data;
24 mkdir "generic";
25 open FD, ">generic/except.i" or die $!;
27 $copyright =~ s/^/ */mg;
29 print FD <<"EOF";
31 /** \@file
32  * \@brief Language independent exception handling.
33  */
34 /* Warning: This file is generated by $0
35  * - do not modify directly!
36  *
37 $copyright */
39 /* This file is included for any languages which don't have language specific
40  * handling for exceptions.
41  */
43 EOF
45 print FD <<'EOF';
46 #include <exception>
48 static int XapianExceptionHandler(string & msg) {
49     try {
50         // Rethrow so we can look at the exception if it was a Xapian::Error.
51         throw;
52     } catch (const Xapian::Error &e) {
53         msg = e.get_description();
54         try {
55             // Re-rethrow the previous exception so we can handle the type in a
56             // fine-grained way, but only in one place to avoid bloating the
57             // file.
58             throw;
59         } catch (const Xapian::InvalidArgumentError &) {
60             return SWIG_ValueError;
61         } catch (const Xapian::RangeError &) {
62             return SWIG_IndexError;
63         } catch (const Xapian::DatabaseError &) {
64             return SWIG_IOError;
65         } catch (const Xapian::NetworkError &) {
66             return SWIG_IOError;
67         } catch (const Xapian::InternalError &) {
68             return SWIG_RuntimeError;
69         } catch (const Xapian::RuntimeError &) {
70             return SWIG_RuntimeError;
71         } catch (...) {
72             return SWIG_UnknownError;
73         }
74     } catch (const std::exception &e) {
75         msg = "std::exception: ";
76         msg += e.what();
77     } catch (...) {
78         msg = "unknown error in Xapian";
79     }
80     return SWIG_UnknownError;
84 /* Functions and methods which are marked as "nothrow": */
85 EOF
87 chdir($INC[0]);
88 exception_data::for_each_nothrow(sub { print FD "%exception $_[0];\n" });
90 print FD <<'EOF';
92 %exception {
93     try {
94         $function
95     } catch (...) {
96         string msg;
97         int code = XapianExceptionHandler(msg);
98         SWIG_exception(code, msg.c_str());
99     }
103 close FD or die $!;