xref: /minix3/external/bsd/atf/dist/atf-c++/detail/exceptions.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc //
2*11be35a1SLionel Sambuc // Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc // All rights reserved.
6*11be35a1SLionel Sambuc //
7*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc // are met:
10*11be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc //
16*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc //
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #if defined(HAVE_CONFIG_H)
31*11be35a1SLionel Sambuc #include "bconfig.h"
32*11be35a1SLionel Sambuc #endif
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc #include <cstdarg>
35*11be35a1SLionel Sambuc #include <cstdio>
36*11be35a1SLionel Sambuc #include <cstring>
37*11be35a1SLionel Sambuc #include <new>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc extern "C" {
40*11be35a1SLionel Sambuc #include "../../atf-c/error.h"
41*11be35a1SLionel Sambuc };
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc #include "exceptions.hpp"
44*11be35a1SLionel Sambuc #include "sanity.hpp"
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
47*11be35a1SLionel Sambuc // The "system_error" type.
48*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
49*11be35a1SLionel Sambuc 
system_error(const std::string & who,const std::string & message,int sys_err)50*11be35a1SLionel Sambuc atf::system_error::system_error(const std::string& who,
51*11be35a1SLionel Sambuc                                 const std::string& message,
52*11be35a1SLionel Sambuc                                 int sys_err) :
53*11be35a1SLionel Sambuc     std::runtime_error(who + ": " + message),
54*11be35a1SLionel Sambuc     m_sys_err(sys_err)
55*11be35a1SLionel Sambuc {
56*11be35a1SLionel Sambuc }
57*11be35a1SLionel Sambuc 
~system_error(void)58*11be35a1SLionel Sambuc atf::system_error::~system_error(void)
59*11be35a1SLionel Sambuc     throw()
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc }
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc int
code(void) const64*11be35a1SLionel Sambuc atf::system_error::code(void)
65*11be35a1SLionel Sambuc     const
66*11be35a1SLionel Sambuc     throw()
67*11be35a1SLionel Sambuc {
68*11be35a1SLionel Sambuc     return m_sys_err;
69*11be35a1SLionel Sambuc }
70*11be35a1SLionel Sambuc 
71*11be35a1SLionel Sambuc const char*
what(void) const72*11be35a1SLionel Sambuc atf::system_error::what(void)
73*11be35a1SLionel Sambuc     const
74*11be35a1SLionel Sambuc     throw()
75*11be35a1SLionel Sambuc {
76*11be35a1SLionel Sambuc     try {
77*11be35a1SLionel Sambuc         if (m_message.length() == 0) {
78*11be35a1SLionel Sambuc             m_message = std::string(std::runtime_error::what()) + ": ";
79*11be35a1SLionel Sambuc             m_message += ::strerror(m_sys_err);
80*11be35a1SLionel Sambuc         }
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc         return m_message.c_str();
83*11be35a1SLionel Sambuc     } catch (...) {
84*11be35a1SLionel Sambuc         return "Unable to format system_error message";
85*11be35a1SLionel Sambuc     }
86*11be35a1SLionel Sambuc }
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
89*11be35a1SLionel Sambuc // Free functions.
90*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc static
93*11be35a1SLionel Sambuc void
throw_libc_error(atf_error_t err)94*11be35a1SLionel Sambuc throw_libc_error(atf_error_t err)
95*11be35a1SLionel Sambuc {
96*11be35a1SLionel Sambuc     PRE(atf_error_is(err, "libc"));
97*11be35a1SLionel Sambuc 
98*11be35a1SLionel Sambuc     const int ecode = atf_libc_error_code(err);
99*11be35a1SLionel Sambuc     const std::string msg = atf_libc_error_msg(err);
100*11be35a1SLionel Sambuc     atf_error_free(err);
101*11be35a1SLionel Sambuc     throw atf::system_error("XXX", msg, ecode);
102*11be35a1SLionel Sambuc }
103*11be35a1SLionel Sambuc 
104*11be35a1SLionel Sambuc static
105*11be35a1SLionel Sambuc void
throw_no_memory_error(atf_error_t err)106*11be35a1SLionel Sambuc throw_no_memory_error(atf_error_t err)
107*11be35a1SLionel Sambuc {
108*11be35a1SLionel Sambuc     PRE(atf_error_is(err, "no_memory"));
109*11be35a1SLionel Sambuc 
110*11be35a1SLionel Sambuc     atf_error_free(err);
111*11be35a1SLionel Sambuc     throw std::bad_alloc();
112*11be35a1SLionel Sambuc }
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc static
115*11be35a1SLionel Sambuc void
throw_unknown_error(atf_error_t err)116*11be35a1SLionel Sambuc throw_unknown_error(atf_error_t err)
117*11be35a1SLionel Sambuc {
118*11be35a1SLionel Sambuc     PRE(atf_is_error(err));
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc     static char buf[4096];
121*11be35a1SLionel Sambuc     atf_error_format(err, buf, sizeof(buf));
122*11be35a1SLionel Sambuc     atf_error_free(err);
123*11be35a1SLionel Sambuc     throw std::runtime_error(buf);
124*11be35a1SLionel Sambuc }
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc void
throw_atf_error(atf_error_t err)127*11be35a1SLionel Sambuc atf::throw_atf_error(atf_error_t err)
128*11be35a1SLionel Sambuc {
129*11be35a1SLionel Sambuc     static struct handler {
130*11be35a1SLionel Sambuc         const char* m_name;
131*11be35a1SLionel Sambuc         void (*m_func)(atf_error_t);
132*11be35a1SLionel Sambuc     } handlers[] = {
133*11be35a1SLionel Sambuc         { "libc", throw_libc_error },
134*11be35a1SLionel Sambuc         { "no_memory", throw_no_memory_error },
135*11be35a1SLionel Sambuc         { NULL, throw_unknown_error },
136*11be35a1SLionel Sambuc     };
137*11be35a1SLionel Sambuc 
138*11be35a1SLionel Sambuc     PRE(atf_is_error(err));
139*11be35a1SLionel Sambuc 
140*11be35a1SLionel Sambuc     handler* h = handlers;
141*11be35a1SLionel Sambuc     while (h->m_name != NULL) {
142*11be35a1SLionel Sambuc         if (atf_error_is(err, h->m_name)) {
143*11be35a1SLionel Sambuc             h->m_func(err);
144*11be35a1SLionel Sambuc             UNREACHABLE;
145*11be35a1SLionel Sambuc         } else
146*11be35a1SLionel Sambuc             h++;
147*11be35a1SLionel Sambuc     }
148*11be35a1SLionel Sambuc     // XXX: I'm not sure that raising an "unknown" error is a wise thing
149*11be35a1SLionel Sambuc     // to do here.  The C++ binding is supposed to have feature parity
150*11be35a1SLionel Sambuc     // with the C one, so all possible errors raised by the C library
151*11be35a1SLionel Sambuc     // should have their counterpart in the C++ library.  Still, removing
152*11be35a1SLionel Sambuc     // this will require some code auditing that I can't afford at the
153*11be35a1SLionel Sambuc     // moment.
154*11be35a1SLionel Sambuc     INV(h->m_name == NULL && h->m_func != NULL);
155*11be35a1SLionel Sambuc     h->m_func(err);
156*11be35a1SLionel Sambuc     UNREACHABLE;
157*11be35a1SLionel Sambuc }
158