Small fixups to Nicolas Vivien's bjavaloader patch
[barry.git] / src / error.h
blob2b88ce87cfc1240686f00b2bf391816958cf4760
1 ///
2 /// \file error.h
3 /// Common exception classes for the Barry library
4 ///
6 /*
7 Copyright (C) 2005-2009, 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 <stdexcept>
28 namespace Barry {
30 /// \addtogroup exceptions
31 /// @{
34 // Error class
36 /// The base class for any future derived exceptions.
37 /// Can be thrown on any protocol error.
38 ///
39 class BXEXPORT Error : public std::runtime_error
41 public:
42 Error(const std::string &str) : std::runtime_error(str) {}
47 // BadPassword
49 /// A bad or unknown password when talking to the device.
50 /// Can be thrown in the following instances:
51 ///
52 /// - no password provided and the device requests one
53 /// - device rejected the available password
54 /// - too few remaining tries left... Barry will refuse to keep
55 /// trying passwords if there are fewer than
56 /// BARRY_MIN_PASSWORD_TRIES tries remaining. In this case,
57 /// out_of_tries() will return true.
58 ///
59 ///
60 class BXEXPORT BadPassword : public Barry::Error
62 int m_remaining_tries;
63 bool m_out_of_tries;
65 public:
66 BadPassword(const std::string &str, int remaining_tries,
67 bool out_of_tries)
68 : Barry::Error(str),
69 m_remaining_tries(remaining_tries),
70 m_out_of_tries(out_of_tries)
72 int remaining_tries() const { return m_remaining_tries; }
73 bool out_of_tries() const { return m_out_of_tries; }
77 // BadData
79 /// Thrown by record classes if their data is invalid and cannot be
80 /// uploaded to the Blackberry.
81 ///
82 class BXEXPORT BadData : public Barry::Error
84 public:
85 BadData(const std::string &str)
86 : Barry::Error(str)
91 // BadSize
93 /// Unexpected packet size, or not enough data.
94 ///
95 class BXEXPORT BadSize : public Barry::Error
97 unsigned int m_packet_size,
98 m_data_buf_size,
99 m_required_size;
101 BXLOCAL static std::string GetMsg(unsigned int p, unsigned int d, unsigned int r);
103 public:
104 BadSize(unsigned int packet_size,
105 unsigned int data_buf_size,
106 unsigned int required_size)
107 : Barry::Error(GetMsg(packet_size, data_buf_size, required_size)),
108 m_packet_size(packet_size),
109 m_data_buf_size(data_buf_size),
110 m_required_size(required_size)
112 unsigned int packet_size() const { return m_packet_size; }
113 unsigned int data_buf_size() const { return m_data_buf_size; }
114 unsigned int required_size() const { return m_required_size; }
118 // ErrnoError
120 /// System error that provides an errno error code.
122 class BXEXPORT ErrnoError : public Barry::Error
124 int m_errno;
126 static std::string GetMsg(const std::string &msg, int err);
128 public:
129 ErrnoError(const std::string &msg, int err)
130 : Barry::Error(GetMsg(msg, err))
131 , m_errno(err)
134 int error_code() const { return m_errno; }
138 // BadPackedFormat
140 /// Thrown by record classes that don't recognize a given packed format code.
141 /// This exception is mostly handled internally, but is published here
142 /// just in case it escapes.
144 class BXEXPORT BadPackedFormat : public Barry::Error
146 uint8_t m_format;
148 public:
149 BadPackedFormat(uint8_t format)
150 : Barry::Error("Bad packed format - internal exception")
151 , m_format(format)
154 uint8_t format() const { return m_format; }
157 /// @}
159 } // namespace Barry
161 #endif