error.h

Go to the documentation of this file.
00001 ///
00002 /// \file       error.h
00003 ///             Common exception classes for the Barry library
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __BARRY_ERROR_H__
00023 #define __BARRY_ERROR_H__
00024 
00025 #include "dll.h"
00026 #include "pin.h"
00027 #include <stdexcept>
00028 #include <stdint.h>
00029 
00030 namespace Barry {
00031 
00032 /// \addtogroup exceptions
00033 /// @{
00034 
00035 //
00036 // Error class
00037 //
00038 /// The base class for any future derived exceptions.
00039 /// Can be thrown on any protocol error.
00040 ///
00041 class BXEXPORT Error : public std::runtime_error
00042 {
00043 public:
00044         Error(const std::string &str) : std::runtime_error(str) {}
00045 };
00046 
00047 
00048 //
00049 // BadPassword
00050 //
00051 /// A bad or unknown password when talking to the device.
00052 /// Can be thrown in the following instances:
00053 ///
00054 ///     - no password provided and the device requests one
00055 ///     - device rejected the available password
00056 ///     - too few remaining tries left... Barry will refuse to keep
00057 ///             trying passwords if there are fewer than
00058 ///             BARRY_MIN_PASSWORD_TRIES tries remaining.  In this case,
00059 ///             out_of_tries() will return true.
00060 ///
00061 ///
00062 class BXEXPORT BadPassword : public Barry::Error
00063 {
00064         int m_remaining_tries;
00065         bool m_out_of_tries;
00066 
00067 public:
00068         BadPassword(const std::string &str, int remaining_tries,
00069                 bool out_of_tries)
00070                 : Barry::Error(str),
00071                 m_remaining_tries(remaining_tries),
00072                 m_out_of_tries(out_of_tries)
00073                 {}
00074         int remaining_tries() const { return m_remaining_tries; }
00075         bool out_of_tries() const { return m_out_of_tries; }
00076 };
00077 
00078 //
00079 // SocketCloseOnOpen
00080 //
00081 /// Thrown by the Socket class if it receives a CLOSE message in
00082 /// response to an OPEN command.  This can mean a number of things:
00083 ///
00084 ///     - device is password protected, and the wrong password was given
00085 ///     - device thinks that the socket is already open
00086 ///
00087 /// This special exception thrown so the application can try again
00088 /// with a fresh Socket::Open() call.
00089 ///
00090 class BXEXPORT SocketCloseOnOpen : public Barry::Error
00091 {
00092 public:
00093         SocketCloseOnOpen(const std::string &str) : Barry::Error(str) {}
00094 };
00095 
00096 //
00097 // PinNotFound
00098 //
00099 /// Thrown by the Connector class when unable to find the requested Pin
00100 /// If the attached pin is not Valid(), then unable to autodetect device.
00101 /// If pin is Valid(), then the specified pin number was not available.
00102 /// probe_count is the number of devices found during the probe.
00103 ///
00104 class BXEXPORT PinNotFound : public Barry::Error
00105 {
00106         Barry::Pin m_pin;
00107         int m_probe_count;
00108 
00109 public:
00110         PinNotFound(Barry::Pin pin, int probe_count)
00111                 : Barry::Error("PIN not found: " + pin.Str())
00112                 , m_pin(pin)
00113                 , m_probe_count(probe_count)
00114                 {}
00115 
00116         const Barry::Pin& pin() const { return m_pin; }
00117         int probe_count() const { return m_probe_count; }
00118 };
00119 
00120 //
00121 // BadData
00122 //
00123 /// Thrown by record classes if their data is invalid and cannot be
00124 /// uploaded to the Blackberry.
00125 ///
00126 class BXEXPORT BadData : public Barry::Error
00127 {
00128 public:
00129         BadData(const std::string &str)
00130                 : Barry::Error(str)
00131                 {}
00132 };
00133 
00134 //
00135 // BadSize
00136 //
00137 /// Unexpected packet size, or not enough data.
00138 ///
00139 class BXEXPORT BadSize : public Barry::Error
00140 {
00141         unsigned int m_packet_size,
00142                 m_data_buf_size,
00143                 m_required_size;
00144 
00145         BXLOCAL static std::string GetMsg(const char *msg, unsigned int d, unsigned int r);
00146         BXLOCAL static std::string GetMsg(unsigned int p, unsigned int d, unsigned int r);
00147 
00148 public:
00149         BadSize(const char *msg, unsigned int data_size, unsigned int required_size);
00150         BadSize(unsigned int packet_size,
00151                 unsigned int data_buf_size,
00152                 unsigned int required_size);
00153         unsigned int packet_size() const { return m_packet_size; }
00154         unsigned int data_buf_size() const { return m_data_buf_size; }
00155         unsigned int required_size() const { return m_required_size; }
00156 };
00157 
00158 //
00159 // ErrnoError
00160 //
00161 /// System error that provides an errno error code.
00162 ///
00163 class BXEXPORT ErrnoError : public Barry::Error
00164 {
00165         int m_errno;
00166 
00167         BXLOCAL static std::string GetMsg(const std::string &msg, int err);
00168 
00169 protected:
00170         ErrnoError(const std::string &msg); // for derived classes
00171 
00172 public:
00173         ErrnoError(const std::string &msg, int err);
00174 
00175         int error_code() const { return m_errno; }
00176 };
00177 
00178 //
00179 // ConfigFileError
00180 //
00181 /// Thrown by the ConfigFile class when encountering a serious system
00182 /// error while loading the global config file for a given PIN.
00183 ///
00184 class BXEXPORT ConfigFileError : public Barry::ErrnoError
00185 {
00186 public:
00187         ConfigFileError(const char *msg) : Barry::ErrnoError(msg) {}
00188         ConfigFileError(const char *msg, int err)
00189                 : Barry::ErrnoError(msg, err)
00190                 {}
00191 };
00192 
00193 //
00194 // BadPackedFormat
00195 //
00196 /// Thrown by record classes that don't recognize a given packed format code.
00197 /// This exception is mostly handled internally, but is published here
00198 /// just in case it escapes.
00199 ///
00200 class BXEXPORT BadPackedFormat : public Barry::Error
00201 {
00202         uint8_t m_format;
00203 
00204 public:
00205         BadPackedFormat(uint8_t format)
00206                 : Barry::Error("Bad packed format - internal exception")
00207                 , m_format(format)
00208                 {}
00209 
00210         uint8_t format() const { return m_format; }
00211 };
00212 
00213 //
00214 // BadPacket
00215 //
00216 /// Thrown by the socket class if a packet command's response indicates
00217 /// an error.  Some commands may be able to recover inside the library,
00218 /// so a special exception is used, that includes the response code.
00219 ///
00220 class BXEXPORT BadPacket : public Barry::Error
00221 {
00222         uint8_t m_response;
00223 
00224 public:
00225         BadPacket(uint8_t response, const std::string &msg)
00226                 : Barry::Error(msg)
00227                 , m_response(response)
00228                 {}
00229 
00230         uint8_t response() const { return m_response; }
00231 };
00232 
00233 //
00234 // ConvertError
00235 //
00236 /// Thrown by the vformat related barrysync library classes.
00237 ///
00238 class BXEXPORT ConvertError : public Barry::Error
00239 {
00240 public:
00241         ConvertError(const std::string &msg) : Barry::Error(msg) {}
00242 };
00243 
00244 //
00245 // BackupError
00246 //
00247 /// Thrown by the Backup parser class when there is a problem with the
00248 /// low level file operation.
00249 ///
00250 class BXEXPORT BackupError : public Barry::Error
00251 {
00252 public:
00253         BackupError(const std::string &str) : Error(str) {}
00254 };
00255 
00256 //
00257 // RestoreError
00258 //
00259 /// Thrown by the Restore builder class when there is a problem with the
00260 /// low level file operation.
00261 ///
00262 class BXEXPORT RestoreError : public Barry::Error
00263 {
00264 public:
00265         RestoreError(const std::string &str) : Error(str) {}
00266 };
00267 
00268 //
00269 // ReturnCodeError
00270 //
00271 /// Thrown by the Mode::Desktop class when a database command returns
00272 /// a non-zero error code.  Can happen when writing or clearing a database.
00273 /// The packet command and return code are passed along, for examination
00274 /// by application code.  Note that return code 0x02 usually means
00275 /// you're trying to clear or write to a read-only database, like Time Zones.
00276 ///
00277 class BXEXPORT ReturnCodeError : public Barry::Error
00278 {
00279         unsigned int m_command, m_return_code;
00280 
00281 public:
00282         ReturnCodeError(const std::string &str, unsigned int command,
00283                         unsigned int return_code)
00284                 : Error(str)
00285                 , m_command(command)
00286                 , m_return_code(return_code)
00287         {
00288         }
00289 
00290         unsigned int command() const { return m_command; }
00291         unsigned int return_code() const { return m_return_code; }
00292 
00293         bool IsReadOnly() const { return return_code() == 0x02; }
00294 };
00295 
00296 //
00297 // ValidationError
00298 //
00299 /// Thrown by one of the record classes' Validate() function.  Contains
00300 /// and error message describing the deficiency.
00301 ///
00302 class BXEXPORT ValidationError : public Barry::Error
00303 {
00304 public:
00305         explicit ValidationError(const std::string &str)
00306                 : Error(str)
00307         {
00308         }
00309 };
00310 
00311 /// @}
00312 
00313 } // namespace Barry
00314 
00315 #endif
00316