Banuba SDK
Loading...
Searching...
No Matches
exceptions.hpp
1#pragma once
2
3#include <stdexcept>
4#include <string>
5#include <cstdio>
6
7
8#define BNB_THROW(exception, message) \
9 do { \
10 const auto er_msg__ = std::string("At ") + __FILE__ + ":" + std::to_string(__LINE__) + ". " + message; \
11 std::fprintf(stderr, "BanubaSDK error: %s\n", er_msg__.c_str()); \
12 throw exception(er_msg__); \
13 } while (false)
14
15#define BNB_THROW_DOMAIN(exception, domain, message) \
16 do { \
17 const auto er_msg__ = std::string("At ") + __FILE__ + ":" + std::to_string(__LINE__) + ". " + message; \
18 std::fprintf(stderr, "BanubaSDK error: %s\n", er_msg__.c_str()); \
19 throw exception(domain, er_msg__); \
20 } while (false)
21
22namespace bnb
23{
25 {
26 public:
27 const char* domain() const noexcept
28 {
29 return m_domain.c_str();
30 }
31 virtual ~domain_specific_data() = default;
32
33 protected:
34 explicit domain_specific_data(std::string domain) noexcept
35 : m_domain(std::move(domain))
36 {
37 }
38 std::string m_domain;
39 };
40
41 class runtime_error : public std::runtime_error, public domain_specific_data
42 {
43 public:
44 runtime_error(std::string domain, std::string message) noexcept
45 : std::runtime_error(message)
46 , domain_specific_data(std::move(domain))
47 {
48 }
49 };
50
51 class logic_error : public std::logic_error, public domain_specific_data
52 {
53 public:
54 logic_error(std::string domain, std::string message) noexcept
55 : std::logic_error(message)
56 , domain_specific_data(std::move(domain))
57 {
58 }
59 };
60
62 {
63 public:
65 std::string domain, std::string message, std::string resource
66 ) noexcept
67 : bnb::runtime_error(std::move(domain), message + ". For: " + resource)
68 , m_resource(std::move(resource))
69 {
70 }
71
72 const char* resource() const noexcept
73 {
74 return m_resource.c_str();
75 }
76
77 protected:
78 std::string m_resource;
79 };
80} // namespace bnb