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-2009, 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 
00028 namespace Barry {
00029 
00030 /// \addtogroup exceptions
00031 /// @{
00032 
00033 //
00034 // Error class
00035 //
00036 /// The base class for any future derived exceptions.
00037 /// Can be thrown on any protocol error.
00038 ///
00039 class BXEXPORT Error : public std::runtime_error
00040 {
00041 public:
00042         Error(const std::string &str) : std::runtime_error(str) {}
00043 };
00044 
00045 
00046 //
00047 // BadPassword
00048 //
00049 /// A bad or unknown password when talking to the device.
00050 /// Can be thrown in the following instances:
00051 ///
00052 ///     - no password provided and the device requests one
00053 ///     - device rejected the available password
00054 ///     - too few remaining tries left... Barry will refuse to keep
00055 ///             trying passwords if there are fewer than
00056 ///             BARRY_MIN_PASSWORD_TRIES tries remaining.  In this case,
00057 ///             out_of_tries() will return true.
00058 ///             
00059 ///
00060 class BXEXPORT BadPassword : public Barry::Error
00061 {
00062         int m_remaining_tries;
00063         bool m_out_of_tries;
00064 
00065 public:
00066         BadPassword(const std::string &str, int remaining_tries,
00067                 bool out_of_tries)
00068                 : Barry::Error(str),
00069                 m_remaining_tries(remaining_tries),
00070                 m_out_of_tries(out_of_tries)
00071                 {}
00072         int remaining_tries() const { return m_remaining_tries; }
00073         bool out_of_tries() const { return m_out_of_tries; }
00074 };
00075 
00076 //
00077 // BadData
00078 //
00079 /// Thrown by record classes if their data is invalid and cannot be
00080 /// uploaded to the Blackberry.
00081 ///
00082 class BXEXPORT BadData : public Barry::Error
00083 {
00084 public:
00085         BadData(const std::string &str)
00086                 : Barry::Error(str)
00087                 {}
00088 };
00089 
00090 //
00091 // BadSize
00092 //
00093 /// Unexpected packet size, or not enough data.
00094 ///
00095 class BXEXPORT BadSize : public Barry::Error
00096 {
00097         unsigned int m_packet_size,
00098                 m_data_buf_size,
00099                 m_required_size;
00100 
00101         BXLOCAL static std::string GetMsg(unsigned int p, unsigned int d, unsigned int r);
00102 
00103 public:
00104         BadSize(unsigned int packet_size,
00105                 unsigned int data_buf_size,
00106                 unsigned int required_size)
00107                 : Barry::Error(GetMsg(packet_size, data_buf_size, required_size)),
00108                 m_packet_size(packet_size),
00109                 m_data_buf_size(data_buf_size),
00110                 m_required_size(required_size)
00111                 {}
00112         unsigned int packet_size() const { return m_packet_size; }
00113         unsigned int data_buf_size() const { return m_data_buf_size; }
00114         unsigned int required_size() const { return m_required_size; }
00115 };
00116 
00117 //
00118 // ErrnoError
00119 //
00120 /// System error that provides an errno error code.
00121 ///
00122 class BXEXPORT ErrnoError : public Barry::Error
00123 {
00124         int m_errno;
00125 
00126         static std::string GetMsg(const std::string &msg, int err);
00127 
00128 public:
00129         ErrnoError(const std::string &msg, int err)
00130                 : Barry::Error(GetMsg(msg, err))
00131                 , m_errno(err)
00132                 {}
00133 
00134         int error_code() const { return m_errno; }
00135 };
00136 
00137 //
00138 // BadPackedFormat
00139 //
00140 /// Thrown by record classes that don't recognize a given packed format code.
00141 /// This exception is mostly handled internally, but is published here
00142 /// just in case it escapes.
00143 ///
00144 class BXEXPORT BadPackedFormat : public Barry::Error
00145 {
00146         uint8_t m_format;
00147 
00148 public:
00149         BadPackedFormat(uint8_t format)
00150                 : Barry::Error("Bad packed format - internal exception")
00151                 , m_format(format)
00152                 {}
00153 
00154         uint8_t format() const { return m_format; }
00155 };
00156 
00157 /// @}
00158 
00159 } // namespace Barry
00160 
00161 #endif
00162 

Generated on Mon Jan 12 10:51:12 2009 for Barry by  doxygen 1.5.7.1