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-2011, 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 // PinNotFound
00080 //
00081 /// Thrown by the Connector class when unable to find the requested Pin
00082 /// If the attached pin is not Valid(), then unable to autodetect device.
00083 /// If pin is Valid(), then the specified pin number was not available.
00084 /// probe_count is the number of devices found during the probe.
00085 ///
00086 class BXEXPORT PinNotFound : public Barry::Error
00087 {
00088         Barry::Pin m_pin;
00089         int m_probe_count;
00090 
00091 public:
00092         PinNotFound(Barry::Pin pin, int probe_count)
00093                 : Barry::Error("PIN not found: " + pin.Str())
00094                 , m_pin(pin)
00095                 , m_probe_count(probe_count)
00096                 {}
00097 
00098         const Barry::Pin& pin() const { return m_pin; }
00099         int probe_count() const { return m_probe_count; }
00100 };
00101 
00102 //
00103 // BadData
00104 //
00105 /// Thrown by record classes if their data is invalid and cannot be
00106 /// uploaded to the Blackberry.
00107 ///
00108 class BXEXPORT BadData : public Barry::Error
00109 {
00110 public:
00111         BadData(const std::string &str)
00112                 : Barry::Error(str)
00113                 {}
00114 };
00115 
00116 //
00117 // BadSize
00118 //
00119 /// Unexpected packet size, or not enough data.
00120 ///
00121 class BXEXPORT BadSize : public Barry::Error
00122 {
00123         unsigned int m_packet_size,
00124                 m_data_buf_size,
00125                 m_required_size;
00126 
00127         BXLOCAL static std::string GetMsg(const char *msg, unsigned int d, unsigned int r);
00128         BXLOCAL static std::string GetMsg(unsigned int p, unsigned int d, unsigned int r);
00129 
00130 public:
00131         BadSize(const char *msg, unsigned int data_size, unsigned int required_size);
00132         BadSize(unsigned int packet_size,
00133                 unsigned int data_buf_size,
00134                 unsigned int required_size);
00135         unsigned int packet_size() const { return m_packet_size; }
00136         unsigned int data_buf_size() const { return m_data_buf_size; }
00137         unsigned int required_size() const { return m_required_size; }
00138 };
00139 
00140 //
00141 // ErrnoError
00142 //
00143 /// System error that provides an errno error code.
00144 ///
00145 class BXEXPORT ErrnoError : public Barry::Error
00146 {
00147         int m_errno;
00148 
00149         BXLOCAL static std::string GetMsg(const std::string &msg, int err);
00150 
00151 protected:
00152         ErrnoError(const std::string &msg); // for derived classes
00153 
00154 public:
00155         ErrnoError(const std::string &msg, int err);
00156 
00157         int error_code() const { return m_errno; }
00158 };
00159 
00160 //
00161 // ConfigFileError
00162 //
00163 /// Thrown by the ConfigFile class when encountering a serious system
00164 /// error while loading the global config file for a given PIN.
00165 ///
00166 class BXEXPORT ConfigFileError : public Barry::ErrnoError
00167 {
00168 public:
00169         ConfigFileError(const char *msg) : Barry::ErrnoError(msg) {}
00170         ConfigFileError(const char *msg, int err)
00171                 : Barry::ErrnoError(msg, err)
00172                 {}
00173 };
00174 
00175 //
00176 // BadPackedFormat
00177 //
00178 /// Thrown by record classes that don't recognize a given packed format code.
00179 /// This exception is mostly handled internally, but is published here
00180 /// just in case it escapes.
00181 ///
00182 class BXEXPORT BadPackedFormat : public Barry::Error
00183 {
00184         uint8_t m_format;
00185 
00186 public:
00187         BadPackedFormat(uint8_t format)
00188                 : Barry::Error("Bad packed format - internal exception")
00189                 , m_format(format)
00190                 {}
00191 
00192         uint8_t format() const { return m_format; }
00193 };
00194 
00195 //
00196 // BadPacket
00197 //
00198 /// Thrown by the socket class if a packet command's response indicates
00199 /// an error.  Some commands may be able to recover inside the library,
00200 /// so a special exception is used, that includes the response code.
00201 ///
00202 class BXEXPORT BadPacket : public Barry::Error
00203 {
00204         uint8_t m_response;
00205 
00206 public:
00207         BadPacket(uint8_t response, const std::string &msg)
00208                 : Barry::Error(msg)
00209                 , m_response(response)
00210                 {}
00211 
00212         uint8_t response() const { return m_response; }
00213 };
00214 
00215 //
00216 // ConvertError
00217 //
00218 /// Thrown by the vformat related barrysync library classes.
00219 ///
00220 class BXEXPORT ConvertError : public Barry::Error
00221 {
00222 public:
00223         ConvertError(const std::string &msg) : Barry::Error(msg) {}
00224 };
00225 
00226 //
00227 // BackupError
00228 //
00229 /// Thrown by the Backup parser class when there is a problem with the
00230 /// low level file operation.
00231 ///
00232 class BXEXPORT BackupError : public Barry::Error
00233 {
00234 public:
00235         BackupError(const std::string &str) : Error(str) {}
00236 };
00237 
00238 //
00239 // RestoreError
00240 //
00241 /// Thrown by the Restore builder class when there is a problem with the
00242 /// low level file operation.
00243 ///
00244 class BXEXPORT RestoreError : public Barry::Error
00245 {
00246 public:
00247         RestoreError(const std::string &str) : Error(str) {}
00248 };
00249 
00250 /// @}
00251 
00252 } // namespace Barry
00253 
00254 #endif
00255 

Generated on Tue Mar 1 17:50:15 2011 for Barry by  doxygen 1.5.6