lib: added reuse::TzWrapper class and utilities
[barry.git] / src / error.h
blob18e87c6223f2e63af0045f355616f001b03bea89
1 ///
2 /// \file error.h
3 /// Common exception classes for the Barry library
4 ///
6 /*
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_ERROR_H__
23 #define __BARRY_ERROR_H__
25 #include "dll.h"
26 #include <stdexcept>
27 #include <stdint.h>
29 namespace Barry {
31 /// \addtogroup exceptions
32 /// @{
35 // Error class
37 /// The base class for any future derived exceptions.
38 /// Can be thrown on any protocol error.
39 ///
40 class BXEXPORT Error : public std::runtime_error
42 public:
43 Error(const std::string &str) : std::runtime_error(str) {}
48 // BadPassword
50 /// A bad or unknown password when talking to the device.
51 /// Can be thrown in the following instances:
52 ///
53 /// - no password provided and the device requests one
54 /// - device rejected the available password
55 /// - too few remaining tries left... Barry will refuse to keep
56 /// trying passwords if there are fewer than
57 /// BARRY_MIN_PASSWORD_TRIES tries remaining. In this case,
58 /// out_of_tries() will return true.
59 ///
60 ///
61 class BXEXPORT BadPassword : public Barry::Error
63 int m_remaining_tries;
64 bool m_out_of_tries;
66 public:
67 BadPassword(const std::string &str, int remaining_tries,
68 bool out_of_tries)
69 : Barry::Error(str),
70 m_remaining_tries(remaining_tries),
71 m_out_of_tries(out_of_tries)
73 int remaining_tries() const { return m_remaining_tries; }
74 bool out_of_tries() const { return m_out_of_tries; }
78 // BadData
80 /// Thrown by record classes if their data is invalid and cannot be
81 /// uploaded to the Blackberry.
82 ///
83 class BXEXPORT BadData : public Barry::Error
85 public:
86 BadData(const std::string &str)
87 : Barry::Error(str)
92 // BadSize
94 /// Unexpected packet size, or not enough data.
95 ///
96 class BXEXPORT BadSize : public Barry::Error
98 unsigned int m_packet_size,
99 m_data_buf_size,
100 m_required_size;
102 BXLOCAL static std::string GetMsg(const char *msg, unsigned int d, unsigned int r);
103 BXLOCAL static std::string GetMsg(unsigned int p, unsigned int d, unsigned int r);
105 public:
106 BadSize(const char *msg, unsigned int data_size, unsigned int required_size)
107 : Barry::Error(GetMsg(msg, data_size, required_size))
108 , m_packet_size(0)
109 , m_data_buf_size(data_size)
110 , m_required_size(required_size)
112 BadSize(unsigned int packet_size,
113 unsigned int data_buf_size,
114 unsigned int required_size)
115 : Barry::Error(GetMsg(packet_size, data_buf_size, required_size))
116 , m_packet_size(packet_size)
117 , m_data_buf_size(data_buf_size)
118 , m_required_size(required_size)
120 unsigned int packet_size() const { return m_packet_size; }
121 unsigned int data_buf_size() const { return m_data_buf_size; }
122 unsigned int required_size() const { return m_required_size; }
126 // ErrnoError
128 /// System error that provides an errno error code.
130 class BXEXPORT ErrnoError : public Barry::Error
132 int m_errno;
134 static std::string GetMsg(const std::string &msg, int err);
136 protected:
137 ErrnoError(const std::string &msg) // for derived classes
138 : Barry::Error(msg)
139 , m_errno(0)
142 public:
143 ErrnoError(const std::string &msg, int err)
144 : Barry::Error(GetMsg(msg, err))
145 , m_errno(err)
148 int error_code() const { return m_errno; }
152 // ConfigFileError
154 /// Thrown by the ConfigFile class when encountering a serious system
155 /// error while loading the global config file for a given PIN.
157 class ConfigFileError : public Barry::ErrnoError
159 public:
160 ConfigFileError(const char *msg) : Barry::ErrnoError(msg) {}
161 ConfigFileError(const char *msg, int err)
162 : Barry::ErrnoError(msg, err)
167 // BadPackedFormat
169 /// Thrown by record classes that don't recognize a given packed format code.
170 /// This exception is mostly handled internally, but is published here
171 /// just in case it escapes.
173 class BXEXPORT BadPackedFormat : public Barry::Error
175 uint8_t m_format;
177 public:
178 BadPackedFormat(uint8_t format)
179 : Barry::Error("Bad packed format - internal exception")
180 , m_format(format)
183 uint8_t format() const { return m_format; }
187 // BadPacket
189 /// Thrown by the socket class if a packet command's response indicates
190 /// an error. Some commands may be able to recover inside the library,
191 /// so a special exception is used, that includes the response code.
193 class BXEXPORT BadPacket : public Barry::Error
195 uint8_t m_response;
197 public:
198 BadPacket(uint8_t response, const std::string &msg)
199 : Barry::Error(msg)
200 , m_response(response)
203 uint8_t response() const { return m_response; }
207 // ConvertError
209 /// Thrown by the vformat related barrysync library classes.
211 class ConvertError : public Barry::Error
213 public:
214 ConvertError(const std::string &msg) : Barry::Error(msg) {}
217 /// @}
219 } // namespace Barry
221 #endif