1 // Copyright (c) 2007 The NetBSD Foundation, Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
7 // 1. Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
13 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "atf-c++/detail/exceptions.hpp"
28 #if defined(HAVE_CONFIG_H)
38 #include "atf-c/error.h"
41 #include "atf-c++/detail/sanity.hpp"
43 // ------------------------------------------------------------------------
44 // The "system_error" type.
45 // ------------------------------------------------------------------------
47 atf::system_error::system_error(const std::string& who,
48 const std::string& message,
50 std::runtime_error(who + ": " + message),
55 atf::system_error::~system_error(void)
61 atf::system_error::code(void)
69 atf::system_error::what(void)
74 if (m_message.length() == 0) {
75 m_message = std::string(std::runtime_error::what()) + ": ";
76 m_message += ::strerror(m_sys_err);
79 return m_message.c_str();
81 return "Unable to format system_error message";
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
91 throw_libc_error(atf_error_t err)
93 PRE(atf_error_is(err, "libc"));
95 const int ecode = atf_libc_error_code(err);
96 const std::string msg = atf_libc_error_msg(err);
98 throw atf::system_error("XXX", msg, ecode);
103 throw_no_memory_error(atf_error_t err)
105 PRE(atf_error_is(err, "no_memory"));
108 throw std::bad_alloc();
113 throw_unknown_error(atf_error_t err)
115 PRE(atf_is_error(err));
117 static char buf[4096];
118 atf_error_format(err, buf, sizeof(buf));
120 throw std::runtime_error(buf);
124 atf::throw_atf_error(atf_error_t err)
126 static struct handler {
128 void (*m_func)(atf_error_t);
130 { "libc", throw_libc_error },
131 { "no_memory", throw_no_memory_error },
132 { NULL, throw_unknown_error },
135 PRE(atf_is_error(err));
137 handler* h = handlers;
138 while (h->m_name != NULL) {
139 if (atf_error_is(err, h->m_name)) {
145 // XXX: I'm not sure that raising an "unknown" error is a wise thing
146 // to do here. The C++ binding is supposed to have feature parity
147 // with the C one, so all possible errors raised by the C library
148 // should have their counterpart in the C++ library. Still, removing
149 // this will require some code auditing that I can't afford at the
151 INV(h->m_name == NULL && h->m_func != NULL);