File indexing completed on 2024-09-22 04:52:55
0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net> 0002 0003 This file is part of the Trojita Qt IMAP e-mail client, 0004 http://trojita.flaska.net/ 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public License as 0008 published by the Free Software Foundation; either version 2 of 0009 the License or (at your option) version 3 or any later version 0010 accepted by the membership of KDE e.V. (or its successor approved 0011 by the membership of KDE e.V.), which shall act as a proxy 0012 defined in Section 14 of version 3 of the license. 0013 0014 This program is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 GNU General Public License for more details. 0018 0019 You should have received a copy of the GNU General Public License 0020 along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 #ifndef IMAP_EXCEPTIONS_H 0023 #define IMAP_EXCEPTIONS_H 0024 #include <exception> 0025 #include <string> 0026 #include <QByteArray> 0027 0028 /** 0029 * @file 0030 * @short Common IMAP-related exceptions 0031 * 0032 * All IMAP-related exceptions inherit from Imap::Exception which inherits from 0033 * std::exception. 0034 * 0035 * @author Jan Kundrát <jkt@flaska.net> 0036 */ 0037 0038 /** @short Namespace for IMAP interaction */ 0039 namespace Imap 0040 { 0041 0042 namespace Responses 0043 { 0044 class AbstractResponse; 0045 } 0046 0047 /** @short General exception class */ 0048 class ImapException : public std::exception 0049 { 0050 protected: 0051 /** The error message */ 0052 std::string m_msg; 0053 /** Line with data that caused this error */ 0054 QByteArray m_line; 0055 /** Offset in line for error source */ 0056 int m_offset; 0057 /** Class name of the exception */ 0058 std::string m_exceptionClass; 0059 public: 0060 ImapException(): m_offset(-1), m_exceptionClass("ImapException") {} 0061 ImapException(const std::string &msg) : m_msg(msg), m_offset(-1), m_exceptionClass("ImapException") {}; 0062 ImapException(const std::string &msg, const QByteArray &line, const int offset): 0063 m_msg(msg), m_line(line), m_offset(offset), m_exceptionClass("ImapException") {}; 0064 virtual const char *what() const throw(); 0065 virtual ~ImapException() throw() {}; 0066 std::string msg() const { return m_msg; } 0067 QByteArray line() const { return m_line; } 0068 int offset() const { return m_offset; } 0069 std::string exceptionClass() const { return m_exceptionClass; } 0070 }; 0071 0072 #define ECBODY(CLASSNAME, PARENT) class CLASSNAME: public PARENT { \ 0073 public: CLASSNAME(const std::string &msg): PARENT(msg) { m_exceptionClass = #CLASSNAME; }\ 0074 CLASSNAME(const QByteArray &line, const int offset): PARENT(#CLASSNAME, line, offset) { m_exceptionClass = #CLASSNAME; }\ 0075 CLASSNAME(const std::string &msg, const QByteArray &line, const int offset): PARENT(msg, line, offset) { m_exceptionClass = #CLASSNAME; }\ 0076 }; 0077 0078 /** @short A generic parser exception */ 0079 ECBODY(ParserException, ImapException) 0080 0081 /** @short Invalid argument was passed to some function */ 0082 ECBODY(InvalidArgument, ParserException) 0083 0084 /** @short General parse error */ 0085 ECBODY(ParseError, ParserException) 0086 0087 /** @short Parse error: unknown identifier */ 0088 ECBODY(UnknownIdentifier, ParseError) 0089 0090 /** @short Parse error: unrecognized kind of response */ 0091 ECBODY(UnrecognizedResponseKind, UnknownIdentifier) 0092 0093 /** @short Parse error: this is known, but not expected here */ 0094 ECBODY(UnexpectedHere, ParseError) 0095 0096 /** @short Parse error: No usable data */ 0097 ECBODY(NoData, ParseError) 0098 0099 /** @short Parse error: Too much data */ 0100 ECBODY(TooMuchData, ParseError) 0101 0102 /** @short Command Continuation Request received, but we have no idea how to handle it here */ 0103 ECBODY(ContinuationRequest, ParserException) 0104 0105 /** @short Invalid Response Code */ 0106 ECBODY(InvalidResponseCode, ParseError) 0107 0108 /** @short This is not an IMAP4 server */ 0109 ECBODY(NotAnImapServerError, ParseError) 0110 0111 #undef ECBODY 0112 0113 /** @short Parent for all exceptions thrown by Imap::Mailbox-related classes */ 0114 class MailboxException: public ImapException 0115 { 0116 public: 0117 MailboxException(const char *const msg, const Imap::Responses::AbstractResponse &response); 0118 explicit MailboxException(const char *const msg); 0119 const char *what() const throw() override { return m_msg.c_str(); } 0120 virtual ~MailboxException() throw() {} 0121 0122 }; 0123 0124 #define ECBODY(CLASSNAME, PARENT) class CLASSNAME: public PARENT { \ 0125 public: CLASSNAME(const char *const msg, const Imap::Responses::AbstractResponse &response): PARENT(msg, response) { m_exceptionClass=#CLASSNAME; } \ 0126 CLASSNAME(const char *const msg): PARENT(msg) { m_exceptionClass=#CLASSNAME; } \ 0127 }; 0128 0129 /** @short Server sent us something that isn't expected right now */ 0130 ECBODY(UnexpectedResponseReceived, MailboxException) 0131 0132 /** @short Internal error in Imap::Mailbox code -- there must be bug in its code */ 0133 ECBODY(CantHappen, MailboxException) 0134 0135 /** @short Server sent us information about message we don't know */ 0136 ECBODY(UnknownMessageIndex, MailboxException) 0137 0138 #undef ECBODY 0139 0140 } 0141 #endif /* IMAP_EXCEPTIONS_H */