aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/exceptions.h
blob: c5a3f354fe67dfeaf94d685c41c1f94338a49656 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @file exceptions.h
 * @ingroup util
 *
 * @brief Various common exception classes.
 *
 * @author H�kon Humberset
 * @date 2004-03-31
 * @version $Id$
 */

#pragma once

#include "exception.h"
#include "stringfmt.h"
#include <memory>

namespace vespalib {

#ifdef IAM_DOXYGEN
/**
 * @class vespalib::UnsupportedOperationException
 * @brief Exception telling that the requested operation is not supported.
 **/
/**
 * @class vespalib::IllegalArgumentException
 * @brief Exception telling that illegal arguments was passed to a function.
 **/
/**
 * @class vespalib::IllegalStateException
 * @brief Exception telling that the object has an illegal state.
 **/
/**
 * @class vespalib::OverflowException
 * @brief XXX - Exception telling that some sort of overflow happened? unused
 **/
/**
 * @class vespalib::UnderflowException
 * @brief XXX - Exception telling that some sort of underflow happened? unused
 **/
/**
 * @class vespalib::FatalException
 * @brief Something went seriously wrong and the application should terminate
 **/

#else

VESPA_DEFINE_EXCEPTION(UnsupportedOperationException, Exception);
VESPA_DEFINE_EXCEPTION(IllegalArgumentException, Exception);
VESPA_DEFINE_EXCEPTION(IllegalStateException, Exception);
VESPA_DEFINE_EXCEPTION(OverflowException, Exception);
VESPA_DEFINE_EXCEPTION(UnderflowException, Exception);
VESPA_DEFINE_EXCEPTION(TimeoutException, Exception);
VESPA_DEFINE_EXCEPTION(FatalException, Exception);
VESPA_DEFINE_EXCEPTION(NetworkSetupFailureException, IllegalStateException);

#endif

//-----------------------------------------------------------------------------

/**
 * @brief Exception indicating the failure to listen for connections
 * on a socket.
 **/
class ExceptionWithPayload : public std::exception {
public:
    class Anything {
    public:
       using UP = std::unique_ptr<Anything>;
       virtual ~Anything() = default;
    };
    explicit ExceptionWithPayload(vespalib::stringref msg);
    ExceptionWithPayload(vespalib::stringref msg, Anything::UP payload);
    ExceptionWithPayload(ExceptionWithPayload &&) noexcept;
    ExceptionWithPayload & operator = (ExceptionWithPayload &&) noexcept;
    ~ExceptionWithPayload() override;
    void setPayload(Anything::UP payload) { _payload = std::move(payload); }
    const char * what() const noexcept override;
private:
    vespalib::string _msg;
    Anything::UP     _payload;
};

class OOMException : public ExceptionWithPayload {
public:
    explicit OOMException(vespalib::stringref msg) : ExceptionWithPayload(msg) { }
    OOMException(vespalib::stringref msg, Anything::UP payload) : ExceptionWithPayload(msg, std::move(payload)) { }
};

/**
 * @brief Exception indicating the failure to listen for connections
 * on a socket.
 **/
class PortListenException : public Exception
{
private:
    int _port;
    vespalib::string _protocol;

    vespalib::string make_message(int port, vespalib::stringref protocol, vespalib::stringref msg);

public:
    PortListenException(int port, vespalib::stringref protocol, vespalib::stringref msg = "",
                        vespalib::stringref location = "", int skipStack = 0);
    PortListenException(int port, vespalib::stringref protocol, const Exception &cause, vespalib::stringref msg = "",
                        vespalib::stringref location = "", int skipStack = 0);
    PortListenException(PortListenException &&) noexcept;
    PortListenException & operator = (PortListenException &&) noexcept;
    PortListenException(const PortListenException &);
    PortListenException & operator = (const PortListenException &);
    ~PortListenException() override;
    VESPA_DEFINE_EXCEPTION_SPINE(PortListenException);
    int get_port() const { return _port; }
    const vespalib::string &get_protocol() const { return _protocol; }
};

//-----------------------------------------------------------------------------

/**
 * @brief Exception signaling that some sort of I/O error happened.
 **/
class IoException : public Exception {
public:
    enum Type { UNSPECIFIED,
                ILLEGAL_PATH, NO_PERMISSION, DISK_PROBLEM,
                INTERNAL_FAILURE, NO_SPACE, NOT_FOUND, CORRUPT_DATA,
                TOO_MANY_OPEN_FILES, DIRECTORY_HAVE_CONTENT, FILE_FULL,
                ALREADY_EXISTS };

    IoException(stringref msg, Type type, stringref location, int skipStack = 0);
    IoException(stringref msg, Type type, const Exception& cause, stringref location, int skipStack = 0);
    IoException(const IoException &);
    IoException & operator =(const IoException &) = delete;
    IoException(IoException &&) noexcept;
    IoException & operator =(IoException &&) noexcept;
    ~IoException() override;

    VESPA_DEFINE_EXCEPTION_SPINE(IoException);

    static string createMessage(stringref msg, Type type);

    Type getType() const { return _type; }

    /** Use this function as a way to map error from errno to a Type. */
    static Type getErrorType(int error);

private:
    Type _type;
};

class SilenceUncaughtException : public ExceptionWithPayload::Anything {
public:
    SilenceUncaughtException(const SilenceUncaughtException &) = delete;
    SilenceUncaughtException & operator = (const SilenceUncaughtException &) = delete;
    SilenceUncaughtException(SilenceUncaughtException &&) noexcept = delete;
    SilenceUncaughtException & operator=(SilenceUncaughtException &&) noexcept = delete;
    SilenceUncaughtException(const std::exception & e);
    ~SilenceUncaughtException() override;
private:
    std::terminate_handler _oldTerminate;
};

/**
 * NOTE: This function must only be called from within a catch block,
 * and the parameter must reference the caught exception.
 *
 * Based on the run-time type of the exception, determine if it is
 * safe to handle this exception and continue normal program
 * operation. If the exception is considered safe, no additional
 * action is taken. If the exception is considered unsafe, it will be
 * re-thrown.
 *
 * Unsafe exceptions fall under two categories; exceptions that are
 * specifically designed to end program execution and exceptions that
 * have an elevated chance of causing issues when being thrown across
 * code that is not completely exception safe.
 **/
void rethrow_if_unsafe(const std::exception &e);

}