Electroneum
exceptions.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-Present, Electroneum
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef ELECTRONEUM_EXCEPTIONS_H
31 #define ELECTRONEUM_EXCEPTIONS_H
32 
33 #include <exception>
34 #include <string>
35 #include <boost/optional.hpp>
36 
37 namespace hw {
38 namespace trezor {
39 namespace exc {
40 
41  class SecurityException : public std::exception {
42  protected:
43  boost::optional<std::string> reason;
44 
45  public:
46  SecurityException(): reason("General Security exception"){}
48 
49  virtual const char* what() const throw() {
50  return reason.get().c_str();
51  }
52  };
53 
55  public:
57  Poly1305TagInvalid(): SecurityException("Poly1305 authentication tag invalid"){}
58  };
59 
60  class TrezorException : public std::exception {
61  protected:
62  boost::optional<std::string> reason;
63 
64  public:
65  TrezorException(): reason("General Trezor exception"){}
67 
68  virtual const char* what() const throw() {
69  return reason.get().c_str();
70  }
71  };
72 
74  public:
76  CommunicationException(): TrezorException("Trezor communication error"){}
77  };
78 
80  public:
82  EncodingException(): CommunicationException("Trezor message encoding error"){}
83  };
84 
86  public:
88  NotConnectedException(): CommunicationException("Trezor not connected"){}
89  };
90 
92  public:
94  DeviceNotResponsiveException(): CommunicationException("Trezor does not respond to ping"){}
95  };
96 
98  public:
100  DeviceAcquireException(): CommunicationException("Trezor could not be acquired"){}
101  };
102 
104  public:
106  SessionException(): CommunicationException("Trezor session expired"){}
107  };
108 
110  public:
112  TimeoutException(): CommunicationException("Trezor communication timeout"){}
113  };
114 
116  public:
118  ProtocolException(): CommunicationException("Trezor protocol error"){}
119  };
120 
121  // Communication protocol namespace
122  // Separated to distinguish between client and Trezor side exceptions.
123 namespace proto {
124 
126  public:
128  SecurityException(): ProtocolException("Security assertion violated in the protocol"){}
129  };
130 
132  private:
133  boost::optional<uint32_t> code;
134  boost::optional<std::string> message;
135  public:
137  FailureException(): ProtocolException("Trezor returned failure"){}
138  FailureException(boost::optional<uint32_t> code,
139  boost::optional<std::string> message)
140  : code(code), message(message) {
141  reason = "Trezor returned failure: code="
142  + (code ? std::to_string(code.get()) : "")
143  + ", message=" + (message ? message.get() : "");
144  };
145  };
146 
148  public:
150  UnexpectedMessageException(): FailureException("Trezor claims unexpected message received"){}
151  };
152 
154  public:
156  CancelledException(): FailureException("Trezor returned: cancelled operation"){}
157  };
158 
160  public:
162  PinExpectedException(): FailureException("Trezor claims PIN is expected"){}
163  };
164 
166  public:
168  InvalidPinException(): FailureException("Trezor claims PIN is invalid"){}
169  };
170 
172  public:
174  NotEnoughFundsException(): FailureException("Trezor claims not enough funds"){}
175  };
176 
178  public:
180  NotInitializedException(): FailureException("Trezor claims not initialized"){}
181  };
182 
184  public:
186  FirmwareErrorException(): FailureException("Trezor returned firmware error"){}
187  };
188 
189 }
190 }
191 }
192 }
193 #endif //ELECTRONEUM_EXCEPTIONS_H
boost::optional< std::string > reason
Definition: exceptions.hpp:62
::std::string string
Definition: gtest-port.h:1097
TrezorException(std::string what)
Definition: exceptions.hpp:66
boost::optional< std::string > reason
Definition: exceptions.hpp:43
Definition: device.cpp:38
std::string message("Message requiring signing")
SecurityException(std::string what)
Definition: exceptions.hpp:47
FailureException(boost::optional< uint32_t > code, boost::optional< std::string > message)
Definition: exceptions.hpp:138
virtual const char * what() const
Definition: exceptions.hpp:68
std::string to_string(t_connection_type type)
virtual const char * what() const
Definition: exceptions.hpp:49