lib: added some operator<() members to basic record objects
[barry/progweb.git] / src / error.h
blobaeaab6968bf6d52c441accc1dc8f2a6fb60b9e0d
1 ///
2 /// \file error.h
3 /// Common exception classes for the Barry library
4 ///
6 /*
7 Copyright (C) 2005-2012, 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 "pin.h"
27 #include <stdexcept>
28 #include <stdint.h>
30 namespace Barry {
32 /// \addtogroup exceptions
33 /// @{
36 // Error class
38 /// The base class for any future derived exceptions.
39 /// Can be thrown on any protocol error.
40 ///
41 class BXEXPORT Error : public std::runtime_error
43 public:
44 Error(const std::string &str) : std::runtime_error(str) {}
49 // BadPassword
51 /// A bad or unknown password when talking to the device.
52 /// Can be thrown in the following instances:
53 ///
54 /// - no password provided and the device requests one
55 /// - device rejected the available password
56 /// - too few remaining tries left... Barry will refuse to keep
57 /// trying passwords if there are fewer than
58 /// BARRY_MIN_PASSWORD_TRIES tries remaining. In this case,
59 /// out_of_tries() will return true.
60 ///
61 ///
62 class BXEXPORT BadPassword : public Barry::Error
64 int m_remaining_tries;
65 bool m_out_of_tries;
67 public:
68 BadPassword(const std::string &str, int remaining_tries,
69 bool out_of_tries)
70 : Barry::Error(str),
71 m_remaining_tries(remaining_tries),
72 m_out_of_tries(out_of_tries)
74 int remaining_tries() const { return m_remaining_tries; }
75 bool out_of_tries() const { return m_out_of_tries; }
79 // SocketCloseOnOpen
81 /// Thrown by the Socket class if it receives a CLOSE message in
82 /// response to an OPEN command. This can mean a number of things:
83 ///
84 /// - device is password protected, and the wrong password was given
85 /// - device thinks that the socket is already open
86 ///
87 /// This special exception thrown so the application can try again
88 /// with a fresh Socket::Open() call.
89 ///
90 class BXEXPORT SocketCloseOnOpen : public Barry::Error
92 public:
93 SocketCloseOnOpen(const std::string &str) : Barry::Error(str) {}
97 // PinNotFound
99 /// Thrown by the Connector class when unable to find the requested Pin
100 /// If the attached pin is not Valid(), then unable to autodetect device.
101 /// If pin is Valid(), then the specified pin number was not available.
102 /// probe_count is the number of devices found during the probe.
104 class BXEXPORT PinNotFound : public Barry::Error
106 Barry::Pin m_pin;
107 int m_probe_count;
109 public:
110 PinNotFound(Barry::Pin pin, int probe_count)
111 : Barry::Error("PIN not found: " + pin.Str())
112 , m_pin(pin)
113 , m_probe_count(probe_count)
116 const Barry::Pin& pin() const { return m_pin; }
117 int probe_count() const { return m_probe_count; }
121 // BadData
123 /// Thrown by record classes if their data is invalid and cannot be
124 /// uploaded to the Blackberry.
126 class BXEXPORT BadData : public Barry::Error
128 public:
129 BadData(const std::string &str)
130 : Barry::Error(str)
135 // BadSize
137 /// Unexpected packet size, or not enough data.
139 class BXEXPORT BadSize : public Barry::Error
141 unsigned int m_packet_size,
142 m_data_buf_size,
143 m_required_size;
145 BXLOCAL static std::string GetMsg(const char *msg, unsigned int d, unsigned int r);
146 BXLOCAL static std::string GetMsg(unsigned int p, unsigned int d, unsigned int r);
148 public:
149 BadSize(const char *msg, unsigned int data_size, unsigned int required_size);
150 BadSize(unsigned int packet_size,
151 unsigned int data_buf_size,
152 unsigned int required_size);
153 unsigned int packet_size() const { return m_packet_size; }
154 unsigned int data_buf_size() const { return m_data_buf_size; }
155 unsigned int required_size() const { return m_required_size; }
159 // ErrnoError
161 /// System error that provides an errno error code.
163 class BXEXPORT ErrnoError : public Barry::Error
165 int m_errno;
167 BXLOCAL static std::string GetMsg(const std::string &msg, int err);
169 protected:
170 ErrnoError(const std::string &msg); // for derived classes
172 public:
173 ErrnoError(const std::string &msg, int err);
175 int error_code() const { return m_errno; }
179 // ConfigFileError
181 /// Thrown by the ConfigFile class when encountering a serious system
182 /// error while loading the global config file for a given PIN.
184 class BXEXPORT ConfigFileError : public Barry::ErrnoError
186 public:
187 ConfigFileError(const char *msg) : Barry::ErrnoError(msg) {}
188 ConfigFileError(const char *msg, int err)
189 : Barry::ErrnoError(msg, err)
194 // BadPackedFormat
196 /// Thrown by record classes that don't recognize a given packed format code.
197 /// This exception is mostly handled internally, but is published here
198 /// just in case it escapes.
200 class BXEXPORT BadPackedFormat : public Barry::Error
202 uint8_t m_format;
204 public:
205 BadPackedFormat(uint8_t format)
206 : Barry::Error("Bad packed format - internal exception")
207 , m_format(format)
210 uint8_t format() const { return m_format; }
214 // BadPacket
216 /// Thrown by the socket class if a packet command's response indicates
217 /// an error. Some commands may be able to recover inside the library,
218 /// so a special exception is used, that includes the response code.
220 class BXEXPORT BadPacket : public Barry::Error
222 uint8_t m_response;
224 public:
225 BadPacket(uint8_t response, const std::string &msg)
226 : Barry::Error(msg)
227 , m_response(response)
230 uint8_t response() const { return m_response; }
234 // ConvertError
236 /// Thrown by the vformat related barrysync library classes.
238 class BXEXPORT ConvertError : public Barry::Error
240 public:
241 ConvertError(const std::string &msg) : Barry::Error(msg) {}
245 // BackupError
247 /// Thrown by the Backup parser class when there is a problem with the
248 /// low level file operation.
250 class BXEXPORT BackupError : public Barry::Error
252 public:
253 BackupError(const std::string &str) : Error(str) {}
257 // RestoreError
259 /// Thrown by the Restore builder class when there is a problem with the
260 /// low level file operation.
262 class BXEXPORT RestoreError : public Barry::Error
264 public:
265 RestoreError(const std::string &str) : Error(str) {}
269 // ReturnCodeError
271 /// Thrown by the Mode::Desktop class when a database command returns
272 /// a non-zero error code. Can happen when writing or clearing a database.
273 /// The packet command and return code are passed along, for examination
274 /// by application code. Note that return code 0x02 usually means
275 /// you're trying to clear or write to a read-only database, like Time Zones.
277 class BXEXPORT ReturnCodeError : public Barry::Error
279 unsigned int m_command, m_return_code;
281 public:
282 ReturnCodeError(const std::string &str, unsigned int command,
283 unsigned int return_code)
284 : Error(str)
285 , m_command(command)
286 , m_return_code(return_code)
290 unsigned int command() const { return m_command; }
291 unsigned int return_code() const { return m_return_code; }
294 /// @}
296 } // namespace Barry
298 #endif