]> git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/errors.h
Security-55471.tar.gz
[apple/security.git] / libsecurity_utilities / lib / errors.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // Error hierarchy
27 //
28 #ifndef _H_UTILITIES_ERROR
29 #define _H_UTILITIES_ERROR
30
31 #include <AvailabilityMacros.h>
32 #include <exception>
33 #include <errno.h>
34 #include <Security/SecBase.h>
35 #undef check
36
37
38 namespace Security {
39
40
41 //
42 // Common base of Security exceptions that represent error conditions.
43 // All can yield Unix or OSStatus error codes as needed, though *how*
44 // is up to the subclass implementation.
45 // CSSM_RETURN conversions are done externally in (???).
46 //
47 class CommonError : public std::exception {
48 protected:
49 CommonError();
50 CommonError(const CommonError &source);
51 public:
52 virtual ~CommonError() throw ();
53
54 virtual OSStatus osStatus() const = 0;
55 virtual int unixError() const = 0;
56 };
57
58
59 //
60 // Genuine Unix-originated errors identified by an errno value.
61 // This includes secondary sources such as pthreads.
62 //
63 class UnixError : public CommonError {
64 protected:
65 UnixError();
66 UnixError(int err);
67 public:
68 const int error;
69 virtual OSStatus osStatus() const;
70 virtual int unixError() const;
71 virtual const char *what () const throw ();
72
73 static void check(int result) { if (result == -1) throwMe(); }
74 static void throwMe(int err = errno) __attribute__((noreturn));
75
76 // @@@ This is a hack for the Network protocol state machine
77 static UnixError make(int err = errno) DEPRECATED_ATTRIBUTE;
78 };
79
80
81 //
82 // Genuine MacOS (X) errors identified by an OSStatus value.
83 // Don't even think of working with OSErr values; use OSStatus.
84 //
85 class MacOSError : public CommonError {
86 protected:
87 MacOSError(int err);
88 public:
89 const int error;
90 virtual OSStatus osStatus() const;
91 virtual int unixError() const;
92 virtual const char *what () const throw ();
93
94 static void check(OSStatus status) { if (status != errSecSuccess) throwMe(status); }
95 static void throwMe(int err) __attribute__((noreturn));
96 };
97
98
99 //
100 // CoreFoundation errors.
101 // Since CF prefers not to tell us *why* something didn't work, this
102 // is not very useful - but it's better than faking it into one of the other
103 // error spaces.
104 //
105 class CFError : public CommonError {
106 protected:
107 CFError();
108 public:
109 virtual OSStatus osStatus() const;
110 virtual int unixError() const;
111 virtual const char *what () const throw ();
112
113 template <class T>
114 static void check(const T &p) { if (!p) throwMe(); }
115
116 static void throwMe() __attribute__((noreturn));
117 };
118
119
120 // Something that gets thrown when ModuleNexus creation fails
121 class ModuleNexusError : public CommonError {
122 protected:
123 ModuleNexusError() {}
124
125 public:
126 virtual OSStatus osStatus() const;
127 virtual int unixError() const;
128 static void throwMe() __attribute__((noreturn));
129 };
130
131 } // end namespace Security
132
133
134 #endif //_H_UTILITIES_ERROR