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