summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/asyncStore/jrnl2/JournalError.h
blob: c3d9834a65469de2e988cbda2f77e328ab000e91 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * \file JournalError.h
 */

#ifndef qpid_asyncStore_jrnl2_JournalError_hpp_
#define qpid_asyncStore_jrnl2_JournalError_hpp_

#include "qpid/asyncStore/jrnl2/Streamable.h"

#include <map>
#include <stdexcept> // std::runtime_error
#include <stdint.h> // uint32_t

// Macro definitions

/**
 * \brief Macro to retrieve and format the C errno value as a string.
 *
 * \param errno Value of errno to be formatted.
 */
#define FORMAT_SYSERR(errno) " errno=" << errno << " (" << std::strerror(errno) << ")"

/**
 * \brief Macro to check for a clean pthread creation and throwing a JournalException with code JERR_PTHREAD if
 * thread creation failed.
 *
 * \param err Value or errno.
 * \param pfn Name of system call that failed.
 * \param cls Name of class in which function failed.
 * \param fn Name of class function that failed.
 */
#define PTHREAD_CHK(err, pfn, cls, fn) if(err != 0) { \
    std::ostringstream oss; \
    oss << pfn << " failed: " << FORMAT_SYSERR(err); \
    throw qpid::asyncStore::jrnl2::JournalError(qpid::asyncStore::jrnl2::JournalError::JERR_PTHREAD, oss.str(), cls, fn); \
    }

/**
 * \brief Macro for throwing state-machine errors related to invalid state transitions.
 *
 * \param jerrno Error number to be thrown.
 * \param target_st State into which the state machine was attemting to move.
 * \param curr_st Current state of the state machine (making transition illegal).
 * \param cls Name of class in which failure occurred.
 * \param fn Name of class function that failed.
 */
#define THROW_STATE_EXCEPTION(jerrno, target_st, curr_st, cls, fn) { \
    std::ostringstream oss; \
    oss << cls << "::" << fn << "() in state " << curr_st << " cannot be moved to state " << target_st << "."; \
    throw qpid::asyncStore::jrnl2::JournalError(jerrno, oss.str(), cls, fn); \
    }

namespace qpid {
namespace asyncStore {
namespace jrnl2 {

/**
 * \brief Universal error and exception class for the async store. The class uses a set of error codes to indicate
 * the error or failure condition.
 */
class JournalError : public std::runtime_error, public Streamable
{
public:
    /**
     * \brief Default constructor.
     */
    JournalError() throw ();

    /**
     * \brief Constructor which accepts only the error code.
     */
    JournalError(const uint32_t errorCode) throw ();

    /**
     * \brief Constructor which accepts only additional info as a c-string.
     */
    JournalError(const char* additionalInfo) throw ();

    /**
     * \brief Constructor which accepts only additional info as a std::string.
     */
    JournalError(const std::string& additionalInfo) throw ();

    /**
     * \brief Constructor which accepts both the error code and additional info as a c-string.
     */
    JournalError(const uint32_t errorCode,
                 const char* additionalInfo) throw ();

    /**
     * \brief Constructor which accepts both the error code and additional info as a std::string.
     */
    JournalError(const uint32_t errorCode,
                 const std::string& additionalInfo) throw ();


    /**
     * \brief Constructor which accepts both the error code and the throwing class name as a c-string.
     */
    JournalError(const uint32_t errorCode,
                 const char* throwingClass,
                 const char* throwingFunction) throw ();

    /**
     * \brief Constructor which accepts both the error code and the throwing class name as a std::string.
     */
    JournalError(const uint32_t errorCode,
                 const std::string& throwingClass,
                 const std::string& throwingFunction) throw ();


    /**
     * \brief Constructor which accepts the error code, the throwing class and function as c-strings.
     */
    JournalError(const uint32_t errorCode,
                 const char* additionalInfo,
                 const char* throwingClass,
                 const char* throwingFunction) throw ();

    /**
     * \brief Constructor which accepts the error code, the throwing class and function as std::strings.
     */
    JournalError(const uint32_t errorCode,
                 const std::string& additionalInfo,
                 const std::string& throwingClass,
                 const std::string& throwingFunction) throw ();


    /**
     * \brief Destructor guaranteed not to throw.
     */
    virtual ~JournalError() throw ();

    /**
     * \brief Overloaded std::exception::what() call which returns a string representation of the error or fault.
     *
     * \returns C-string representation of the error or fault.
     */
    const char* what() const throw (); // override std::exception::what()

    /**
     * \brief Get the error or fault code.
     *
     * \returns The error or fault code.
     */
    uint32_t getErrorCode() const throw ();

    /**
     * \brief Get additional error or fault info from the \c m_additionalInfo field.
     *
     * \returns Additional error or fault info.
     */
    const std::string getAdditionalInfo() const throw ();

    /**
     * \brief Get name of throwing class from the \c m_throwingClass field.
     *
     * \returns Name of throwing class.
     */
    const std::string getThrowingClass() const throw ();

    /**
     * \brief Get name of throwing function from \c m_throwingFunction field.
     *
     * \returns Name of throwing function.
     */
    const std::string getThrowingFunction() const throw ();

    // -- Implementation of Streamable methods ---

    virtual void toStream(std::ostream&) const;

    // --- Static error codes ---

    // generic and system errors
    static const uint32_t JERR_PTHREAD;         ///< pthread operation failure
    static const uint32_t JERR_RTCLOCK;         ///< realtime clock operation failure

    // illegal states
    static const uint32_t JERR_JRNLRUNSTATE;    ///< Illegal journal run state transition
    static const uint32_t JERR_MSGOPSTATE;      ///< Illegal msg op state transition
    static const uint32_t JERR_MSGWRCMPLSTATE;  ///< Illegal msg write completion state transition

    // file ops and file i/o
    static const uint32_t JERR_STAT;            ///< Call to stat() or lstat() failed
    static const uint32_t JERR_OPENDIR;         ///< Call to opendir() failed
    static const uint32_t JERR_CLOSEDIR;        ///< Call to closedir() failed
    static const uint32_t JERR_MKDIR;           ///< Call to rmdir() failed
    static const uint32_t JERR_RMDIR;           ///< Call to rmdir() failed
    static const uint32_t JERR_UNLINK;          ///< Call to unlink() failed
    static const uint32_t JERR_NOTADIR;         ///< Not a directory
    static const uint32_t JERR_BADFTYPE;        ///< Bad file type
    static const uint32_t JERR_DIRNOTEMPTY;     ///< Directory not empty

    /**
     * \brief Static method which converts an error or exception code into a c-string representation.
     *
     * \param err_no Error code for which a string representation is desired.
     * \returns C-string representation of the error code \c err_no.
     */
    static const char* s_errorMessage(const uint32_t err_no) throw ();

private:
    uint32_t m_errorCode;            ///< Error or failure code, taken from JournalErrors.
    std::string m_additionalInfo;       ///< Additional information pertaining to the error or failure.
    std::string m_throwingClass;        ///< Name of the class throwing the error.
    std::string m_throwingFunction;     ///< Name of the function throwing the error.
    std::string m_what;                 ///< Standard error of failure message, taken from JournalErrors.

    /**
     * \brief Protected function to format the error message.
     */
    void formatWhatStr() throw ();

    /**
     * \brief Return this class name to print in error messages. Derived classes will have different definitions
     * for s_className, and thus this function will return the correct name in any child class.
     */
    virtual const char* className();

    typedef std::map<uint32_t, const char*> errorMap_t; ///< Type for map of error messages
    typedef errorMap_t::const_iterator errorMapCitr_t;  ///< Const iterator for map of error messages

    static errorMap_t s_errorMap;                       ///< Map of error messages
    static errorMapCitr_t s_errorMapIterator;           ///< Const iterator

    static const char* s_className;                     ///< Name of this class, used in formatting error messages.
    static bool s_initializedFlag;                      ///< Dummy flag, used to initialize map.

    static bool s_initialize();                         ///< Static fn for initializing static data

};

}}} // namespace qpid::asyncStore::jrnl2

#endif // qpid_asyncStore_jrnl2_JournalError_hpp_