Path: blob/main/contrib/atf/atf-c++/detail/exceptions.cpp
39563 views
// Copyright (c) 2007 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#include "atf-c++/detail/exceptions.hpp"2627#if defined(HAVE_CONFIG_H)28#include "config.h"29#endif3031#include <cstdarg>32#include <cstdio>33#include <cstring>34#include <new>3536extern "C" {37#include "atf-c/error.h"38}3940#include "atf-c++/detail/sanity.hpp"4142// ------------------------------------------------------------------------43// The "system_error" type.44// ------------------------------------------------------------------------4546atf::system_error::system_error(const std::string& who,47const std::string& message,48int sys_err) :49std::runtime_error(who + ": " + message),50m_sys_err(sys_err)51{52}5354atf::system_error::~system_error(void)55throw()56{57}5859int60atf::system_error::code(void)61const62throw()63{64return m_sys_err;65}6667const char*68atf::system_error::what(void)69const70throw()71{72try {73if (m_message.length() == 0) {74m_message = std::string(std::runtime_error::what()) + ": ";75m_message += ::strerror(m_sys_err);76}7778return m_message.c_str();79} catch (...) {80return "Unable to format system_error message";81}82}8384// ------------------------------------------------------------------------85// Free functions.86// ------------------------------------------------------------------------8788static89void90throw_libc_error(atf_error_t err)91{92PRE(atf_error_is(err, "libc"));9394const int ecode = atf_libc_error_code(err);95const std::string msg = atf_libc_error_msg(err);96atf_error_free(err);97throw atf::system_error("XXX", msg, ecode);98}99100static101void102throw_no_memory_error(atf_error_t err)103{104PRE(atf_error_is(err, "no_memory"));105106atf_error_free(err);107throw std::bad_alloc();108}109110static111void112throw_unknown_error(atf_error_t err)113{114PRE(atf_is_error(err));115116static char buf[4096];117atf_error_format(err, buf, sizeof(buf));118atf_error_free(err);119throw std::runtime_error(buf);120}121122void123atf::throw_atf_error(atf_error_t err)124{125static struct handler {126const char* m_name;127void (*m_func)(atf_error_t);128} handlers[] = {129{ "libc", throw_libc_error },130{ "no_memory", throw_no_memory_error },131{ NULL, throw_unknown_error },132};133134PRE(atf_is_error(err));135136handler* h = handlers;137while (h->m_name != NULL) {138if (atf_error_is(err, h->m_name)) {139h->m_func(err);140UNREACHABLE;141} else142h++;143}144// XXX: I'm not sure that raising an "unknown" error is a wise thing145// to do here. The C++ binding is supposed to have feature parity146// with the C one, so all possible errors raised by the C library147// should have their counterpart in the C++ library. Still, removing148// this will require some code auditing that I can't afford at the149// moment.150INV(h->m_name == NULL && h->m_func != NULL);151h->m_func(err);152UNREACHABLE;153}154155156