Apache Ignite C++ Client
Loading...
Searching...
No Matches
ignite_error.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include "error_codes.h"
21#include "uuid.h"
22
23#include <cstdint>
24#include <exception>
25#include <optional>
26#include <string>
27#include <any>
28#include <map>
29
30namespace ignite {
31
35class ignite_error : public std::exception {
36public:
37 // Default
38 ignite_error() = default;
39
45 explicit ignite_error(std::string message) noexcept
46 : m_message(std::move(message)) {} // NOLINT(bugprone-throw-keyword-missing)
47
53 explicit ignite_error(std::string message, std::int32_t flags) noexcept
54 : m_message(std::move(message))
55 , m_flags(flags) {} // NOLINT(bugprone-throw-keyword-missing)
56
63 explicit ignite_error(error::code code, std::string message) noexcept
64 : m_status_code(code)
65 , m_message(std::move(message)) {} // NOLINT(bugprone-throw-keyword-missing)
66
74 explicit ignite_error(error::code code, std::string message, std::exception_ptr cause) noexcept
75 : m_status_code(code)
76 , m_message(std::move(message))
77 , m_cause(std::move(cause)) {} // NOLINT(bugprone-throw-keyword-missing)
78
87 explicit ignite_error(error::code code, std::string message, uuid trace_id,
88 std::optional<std::string> java_st) noexcept
89 : m_status_code(code)
90 , m_message(std::move(message))
91 , m_trace_id(trace_id)
92 , m_java_stack_trace(java_st) {} // NOLINT(bugprone-throw-keyword-missing)
93
97 [[nodiscard]] char const *what() const noexcept override { return m_message.c_str(); }
98
102 [[nodiscard]] const std::string &what_str() const noexcept { return m_message; }
103
109 [[nodiscard]] error::code get_status_code() const noexcept { return m_status_code; }
110
118 [[nodiscard]] uuid get_trace_id() const noexcept { return m_trace_id; }
119
127 [[nodiscard]] const std::optional<std::string> &get_java_stack_trace() const noexcept { return m_java_stack_trace; }
128
134 [[nodiscard]] std::exception_ptr get_cause() const noexcept { return m_cause; }
135
142 [[nodiscard]] std::int32_t get_flags() const noexcept { return m_flags; }
143
151 template<typename T>
152 void add_extra(std::string key, T value) {
153 m_extras.emplace(std::pair{std::move(key), std::any{std::move(value)}});
154 }
155
161 template<typename T>
162 [[nodiscard]] std::optional<T> get_extra(const std::string &key) const noexcept {
163 auto it = m_extras.find(key);
164 if (it == m_extras.end())
165 return {};
166
167 return std::any_cast<T>(it->second);
168 }
169
170private:
172 error::code m_status_code{error::code::INTERNAL};
173
175 std::string m_message;
176
178 uuid m_trace_id{uuid::random()};
179
181 std::optional<std::string> m_java_stack_trace;
182
184 std::exception_ptr m_cause;
185
187 std::int32_t m_flags{0};
188
190 std::map<std::string, std::any> m_extras;
191};
192
193} // namespace ignite
std::optional< T > get_extra(const std::string &key) const noexcept
Definition ignite_error.h:162
ignite_error(error::code code, std::string message, uuid trace_id, std::optional< std::string > java_st) noexcept
Definition ignite_error.h:87
ignite_error(error::code code, std::string message, std::exception_ptr cause) noexcept
Definition ignite_error.h:74
const std::string & what_str() const noexcept
Definition ignite_error.h:102
std::int32_t get_flags() const noexcept
Definition ignite_error.h:142
ignite_error(std::string message, std::int32_t flags) noexcept
Definition ignite_error.h:53
std::exception_ptr get_cause() const noexcept
Definition ignite_error.h:134
const std::optional< std::string > & get_java_stack_trace() const noexcept
Definition ignite_error.h:127
char const * what() const noexcept override
Definition ignite_error.h:97
error::code get_status_code() const noexcept
Definition ignite_error.h:109
void add_extra(std::string key, T value)
Definition ignite_error.h:152
uuid get_trace_id() const noexcept
Definition ignite_error.h:118
ignite_error(std::string message) noexcept
Definition ignite_error.h:45
ignite_error(error::code code, std::string message) noexcept
Definition ignite_error.h:63
Universally unique identifier (UUID).
Definition uuid.h:32
static uuid random()
Definition uuid.cpp:25