1/*
2 error.h - wraps a gpgme error
3 Copyright (C) 2003, 2007 Klarälvdalens Datakonsult AB
4
5 This file is part of GPGME++.
6
7 GPGME++ is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 GPGME++ is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with GPGME++; see the file COPYING.LIB. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23// -*- c++ -*-
24#ifndef __GPGMEPP_ERROR_H__
25#define __GPGMEPP_ERROR_H__
26
27#include <gpgme++/global.h>
28
29#include <string>
30#include <iosfwd>
31
32#include <gpg-error.h>
33
34#ifndef GPGMEPP_ERR_SOURCE_DEFAULT
35# define GPGMEPP_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_1
36#endif
37
38namespace GpgME {
39
40 class GPGMEPP_EXPORT Error {
41 public:
42 Error() : mErr( 0 ), mMessage() {}
43 explicit Error( unsigned int e ) : mErr( e ), mMessage() {}
44
45 const char * source() const;
46 const char * asString() const;
47
48 int code() const;
49 int sourceID() const;
50
51 bool isCanceled() const;
52
53 unsigned int encodedError() const {
54 return mErr;
55 }
56 int toErrno() const;
57
58 static bool hasSystemError();
59 static Error fromSystemError( unsigned int src=GPGMEPP_ERR_SOURCE_DEFAULT );
60 static void setSystemError( gpg_err_code_t err );
61 static void setErrno( int err );
62 static Error fromErrno( int err, unsigned int src=GPGMEPP_ERR_SOURCE_DEFAULT );
63 static Error fromCode( unsigned int err, unsigned int src=GPGMEPP_ERR_SOURCE_DEFAULT );
64
65 GPGMEPP_MAKE_SAFE_BOOL_OPERATOR( mErr && !isCanceled() )
66 private:
67 unsigned int mErr;
68 mutable std::string mMessage;
69 };
70
71 GPGMEPP_EXPORT std::ostream & operator<<( std::ostream & os, const Error & err );
72
73} // namespace GpgME
74
75#endif /* __GPGMEPP_ERROR_H__ */
76