exception.hpp
1 #pragma once
2 
3 #include <exception>
4 #include <vector>
5 
6 #include "../global.hpp"
7 #include "../exception.hpp"
8 
9 #include "opcua.hpp"
10 
11 #include "../_cexports/c_opcua.h"
12 
13 
14 namespace Cvb
15 {
16 
17 CVB_BEGIN_INLINE_NS
18 
19 namespace OpcUa
20 {
25 {
26 public:
31  explicit OpcUaException(int errorCode) noexcept
32  : std::runtime_error(LastErrorMessage()), errorCode_(errorCode)
33  {
34  }
35 
41  explicit OpcUaException(int errorCode, const std::string & msg) noexcept
42  : std::runtime_error(msg), errorCode_(errorCode)
43  {
44  }
45 
50  int ErrorCode()
51  {
52  return errorCode_;
53  }
54 
55 private:
57  int errorCode_ = Cvb::ErrorCodes::CVB_ERROR;
58 
59 protected:
60  // no expection without error code
61  OpcUaException() = delete;
62 
63  static std::string LastErrorMessage() noexcept
64  {
65  std::size_t bufferSize = 0;
66  if (CVB_CALL_CAPI(CVOPCGetLastErrorString(reinterpret_cast<char*>(0), bufferSize)) < 0)
67  return "unable to get error message buffer size";
68  std::vector<char> buffer(bufferSize);
69  if (CVB_CALL_CAPI(CVOPCGetLastErrorString(buffer.data(), bufferSize)) < 0)
70  return "unable to get error message";
71 
72  return std::string(buffer.data());
73  }
75 };
76 
77 }
78 
79 
80 CVB_END_INLINE_NS
81 
82 }
OpcUaException(int errorCode) noexcept
Creates a runtime exception to carry a native error code.
Definition: exception.hpp:31
OpcUaException(int errorCode, const std::string &msg) noexcept
Creates a runtime exception to carry a user choosen error code and message.
Definition: exception.hpp:41
const int CVB_ERROR
Generic unspecified error.
Definition: exception.hpp:24
int ErrorCode()
Retuns the error code. See Cvb::ErrorCodes.
Definition: exception.hpp:50
STL class.
Root namespace for the Image Manager interface.
Definition: version.hpp:11
Special runtime exception to carry a native error code.
Definition: exception.hpp:24
STL class.