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 <stdexcept>
00027 #include <stdint.h>
00028 
00029 namespace Barry {
00030 
00031 /// \addtogroup exceptions
00032 /// @{
00033 
00034 //
00035 // Error class
00036 //
00037 /// The base class for any future derived exceptions.
00038 /// Can be thrown on any protocol error.
00039 ///
00040 class BXEXPORT Error : public std::runtime_error
00041 {
00042 public:
00043         Error(const std::string &str) : std::runtime_error(str) {}
00044 };
00045 
00046 
00047 //
00048 // BadPassword
00049 //
00050 /// A bad or unknown password when talking to the device.
00051 /// Can be thrown in the following instances:
00052 ///
00053 ///     - no password provided and the device requests one
00054 ///     - device rejected the available password
00055 ///     - too few remaining tries left... Barry will refuse to keep
00056 ///             trying passwords if there are fewer than
00057 ///             BARRY_MIN_PASSWORD_TRIES tries remaining.  In this case,
00058 ///             out_of_tries() will return true.
00059 ///
00060 ///
00061 class BXEXPORT BadPassword : public Barry::Error
00062 {
00063         int m_remaining_tries;
00064         bool m_out_of_tries;
00065 
00066 public:
00067         BadPassword(const std::string &str, int remaining_tries,
00068                 bool out_of_tries)
00069                 : Barry::Error(str),
00070                 m_remaining_tries(remaining_tries),
00071                 m_out_of_tries(out_of_tries)
00072                 {}
00073         int remaining_tries() const { return m_remaining_tries; }
00074         bool out_of_tries() const { return m_out_of_tries; }
00075 };
00076 
00077 //
00078 // BadData
00079 //
00080 /// Thrown by record classes if their data is invalid and cannot be
00081 /// uploaded to the Blackberry.
00082 ///
00083 class BXEXPORT BadData : public Barry::Error
00084 {
00085 public:
00086         BadData(const std::string &str)
00087                 : Barry::Error(str)
00088                 {}
00089 };
00090 
00091 //
00092 // BadSize
00093 //
00094 /// Unexpected packet size, or not enough data.
00095 ///
00096 class BXEXPORT BadSize : public Barry::Error
00097 {
00098         unsigned int m_packet_size,
00099                 m_data_buf_size,
00100                 m_required_size;
00101 
00102         BXLOCAL static std::string GetMsg(const char *msg, unsigned int d, unsigned int r);
00103         BXLOCAL static std::string GetMsg(unsigned int p, unsigned int d, unsigned int r);
00104 
00105 public:
00106         BadSize(const char *msg, unsigned int data_size, unsigned int required_size);
00107         BadSize(unsigned int packet_size,
00108                 unsigned int data_buf_size,
00109                 unsigned int required_size);
00110         unsigned int packet_size() const { return m_packet_size; }
00111         unsigned int data_buf_size() const { return m_data_buf_size; }
00112         unsigned int required_size() const { return m_required_size; }
00113 };
00114 
00115 //
00116 // ErrnoError
00117 //
00118 /// System error that provides an errno error code.
00119 ///
00120 class BXEXPORT ErrnoError : public Barry::Error
00121 {
00122         int m_errno;
00123 
00124         BXLOCAL static std::string GetMsg(const std::string &msg, int err);
00125 
00126 protected:
00127         ErrnoError(const std::string &msg); // for derived classes
00128 
00129 public:
00130         ErrnoError(const std::string &msg, int err);
00131 
00132         int error_code() const { return m_errno; }
00133 };
00134 
00135 //
00136 // ConfigFileError
00137 //
00138 /// Thrown by the ConfigFile class when encountering a serious system
00139 /// error while loading the global config file for a given PIN.
00140 ///
00141 class BXEXPORT ConfigFileError : public Barry::ErrnoError
00142 {
00143 public:
00144         ConfigFileError(const char *msg) : Barry::ErrnoError(msg) {}
00145         ConfigFileError(const char *msg, int err)
00146                 : Barry::ErrnoError(msg, err)
00147                 {}
00148 };
00149 
00150 //
00151 // BadPackedFormat
00152 //
00153 /// Thrown by record classes that don't recognize a given packed format code.
00154 /// This exception is mostly handled internally, but is published here
00155 /// just in case it escapes.
00156 ///
00157 class BXEXPORT BadPackedFormat : public Barry::Error
00158 {
00159         uint8_t m_format;
00160 
00161 public:
00162         BadPackedFormat(uint8_t format)
00163                 : Barry::Error("Bad packed format - internal exception")
00164                 , m_format(format)
00165                 {}
00166 
00167         uint8_t format() const { return m_format; }
00168 };
00169 
00170 //
00171 // BadPacket
00172 //
00173 /// Thrown by the socket class if a packet command's response indicates
00174 /// an error.  Some commands may be able to recover inside the library,
00175 /// so a special exception is used, that includes the response code.
00176 ///
00177 class BXEXPORT BadPacket : public Barry::Error
00178 {
00179         uint8_t m_response;
00180 
00181 public:
00182         BadPacket(uint8_t response, const std::string &msg)
00183                 : Barry::Error(msg)
00184                 , m_response(response)
00185                 {}
00186 
00187         uint8_t response() const { return m_response; }
00188 };
00189 
00190 //
00191 // ConvertError
00192 //
00193 /// Thrown by the vformat related barrysync library classes.
00194 ///
00195 class BXEXPORT ConvertError : public Barry::Error
00196 {
00197 public:
00198         ConvertError(const std::string &msg) : Barry::Error(msg) {}
00199 };
00200 
00201 //
00202 // BackupError
00203 //
00204 /// Thrown by the Backup parser class when there is a problem with the
00205 /// low level file operation.
00206 ///
00207 class BXEXPORT BackupError : public Barry::Error
00208 {
00209 public:
00210         BackupError(const std::string &str) : Error(str) {}
00211 };
00212 
00213 //
00214 // RestoreError
00215 //
00216 /// Thrown by the Restore builder class when there is a problem with the
00217 /// low level file operation.
00218 ///
00219 class BXEXPORT RestoreError : public Barry::Error
00220 {
00221 public:
00222         RestoreError(const std::string &str) : Error(str) {}
00223 };
00224 
00225 /// @}
00226 
00227 } // namespace Barry
00228 
00229 #endif
00230