Hazelcast C++ Client
Hazelcast C++ Client Library
iexception.h
1 /*
2  * Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <string>
19 #include <stdexcept>
20 #include <ostream>
21 
22 #include <boost/format.hpp>
23 #include <boost/exception_ptr.hpp>
24 
25 #include "hazelcast/util/export.h"
26 
27 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
28 #pragma warning(push)
29 #pragma warning(disable : 4251) // for dll export
30 #pragma warning(disable : 4275) // for dll export
31 #endif
32 
33 namespace hazelcast {
34 namespace client {
35 namespace exception {
48 class HAZELCAST_API iexception : public std::exception
49 {
50 public:
51  iexception();
52 
53  // TODO: Remove isRuntime and retryable and use the derived class concept as
54  // in Java
55  iexception(std::string exception_name,
56  std::string source,
57  std::string message,
58  std::string details,
59  int32_t error_no,
60  std::exception_ptr cause,
61  bool is_runtime,
62  bool retryable);
63 
64  ~iexception() noexcept override;
65 
70  char const* what() const noexcept override;
71 
72  const std::string& get_source() const;
73 
74  const std::string& get_message() const;
75 
76  const std::string& get_details() const;
77 
78  int32_t get_error_code() const;
79 
80  bool is_runtime() const;
81 
82  bool is_retryable() const;
83 
84  friend std::ostream HAZELCAST_API& operator<<(std::ostream& os,
85  const iexception& exception);
86 
87 protected:
88  std::string src_;
89  std::string msg_;
90  std::string details_;
91  int32_t error_code_;
92  std::exception_ptr cause_;
93  bool runtime_exception_;
94  bool retryable_;
95  std::string report_;
96 };
97 
98 std::ostream HAZELCAST_API&
99 operator<<(std::ostream& os, const iexception& exception);
100 
101 template<typename EXCEPTIONCLASS>
103 {
104 public:
105  explicit exception_builder(const std::string& source)
106  : source_(source)
107  {}
108 
109  template<typename T>
110  exception_builder& operator<<(const T& message)
111  {
112  msg_ << message;
113  return *this;
114  }
115 
120  boost::exception_detail::clone_impl<EXCEPTIONCLASS> build()
121  {
122  return boost::enable_current_exception(
123  EXCEPTIONCLASS(source_, msg_.str()));
124  }
125 
126 private:
127  std::string source_;
128  std::ostringstream msg_;
129 };
130 
131 } // namespace exception
132 } // namespace client
133 } // namespace hazelcast
134 
135 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
136 #pragma warning(pop)
137 #endif
boost::exception_detail::clone_impl< EXCEPTIONCLASS > build()
Definition: iexception.h:120
Base class for all exception originated from Hazelcast methods.
Definition: iexception.h:49