1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsError_h__
8 #define nsError_h__
9 
10 #ifndef __cplusplus
11 #error nsError.h no longer supports C sources
12 #endif
13 
14 #include "mozilla/Attributes.h"
15 #include "mozilla/Likely.h"
16 
17 #include <stdint.h>
18 
19 /*
20  * To add error code to your module, you need to do the following:
21  *
22  * 1) Add a module offset code.  Add yours to the bottom of the list
23  *    right below this comment, adding 1.
24  *
25  * 2) In your module, define a header file which uses one of the
26  *    NE_ERROR_GENERATExxxxxx macros.  Some examples below:
27  *
28  *    #define NS_ERROR_MYMODULE_MYERROR1 NS_ERROR_GENERATE(NS_ERROR_SEVERITY_ERROR,NS_ERROR_MODULE_MYMODULE,1)
29  *    #define NS_ERROR_MYMODULE_MYERROR2 NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_MYMODULE,2)
30  *    #define NS_ERROR_MYMODULE_MYERROR3 NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_MYMODULE,3)
31  *
32  */
33 
34 
35 /**
36  * @name Standard Module Offset Code. Each Module should identify a unique number
37  *       and then all errors associated with that module become offsets from the
38  *       base associated with that module id. There are 16 bits of code bits for
39  *       each module.
40  */
41 
42 #define NS_ERROR_MODULE_XPCOM      1
43 #define NS_ERROR_MODULE_BASE       2
44 #define NS_ERROR_MODULE_GFX        3
45 #define NS_ERROR_MODULE_WIDGET     4
46 #define NS_ERROR_MODULE_CALENDAR   5
47 #define NS_ERROR_MODULE_NETWORK    6
48 #define NS_ERROR_MODULE_PLUGINS    7
49 #define NS_ERROR_MODULE_LAYOUT     8
50 #define NS_ERROR_MODULE_HTMLPARSER 9
51 #define NS_ERROR_MODULE_RDF        10
52 #define NS_ERROR_MODULE_UCONV      11
53 #define NS_ERROR_MODULE_REG        12
54 #define NS_ERROR_MODULE_FILES      13
55 #define NS_ERROR_MODULE_DOM        14
56 #define NS_ERROR_MODULE_IMGLIB     15
57 #define NS_ERROR_MODULE_MAILNEWS   16
58 #define NS_ERROR_MODULE_EDITOR     17
59 #define NS_ERROR_MODULE_XPCONNECT  18
60 #define NS_ERROR_MODULE_PROFILE    19
61 #define NS_ERROR_MODULE_LDAP       20
62 #define NS_ERROR_MODULE_SECURITY   21
63 #define NS_ERROR_MODULE_DOM_XPATH  22
64 /* 23 used to be NS_ERROR_MODULE_DOM_RANGE (see bug 711047) */
65 #define NS_ERROR_MODULE_URILOADER  24
66 #define NS_ERROR_MODULE_CONTENT    25
67 #define NS_ERROR_MODULE_PYXPCOM    26
68 #define NS_ERROR_MODULE_XSLT       27
69 #define NS_ERROR_MODULE_IPC        28
70 #define NS_ERROR_MODULE_SVG        29
71 #define NS_ERROR_MODULE_STORAGE    30
72 #define NS_ERROR_MODULE_SCHEMA     31
73 #define NS_ERROR_MODULE_DOM_FILE   32
74 #define NS_ERROR_MODULE_DOM_INDEXEDDB 33
75 #define NS_ERROR_MODULE_DOM_FILEHANDLE 34
76 #define NS_ERROR_MODULE_SIGNED_JAR 35
77 #define NS_ERROR_MODULE_DOM_FILESYSTEM 36
78 #define NS_ERROR_MODULE_DOM_BLUETOOTH 37
79 #define NS_ERROR_MODULE_SIGNED_APP 38
80 #define NS_ERROR_MODULE_DOM_ANIM 39
81 #define NS_ERROR_MODULE_DOM_PUSH 40
82 #define NS_ERROR_MODULE_DOM_MEDIA 41
83 
84 /* NS_ERROR_MODULE_GENERAL should be used by modules that do not
85  * care if return code values overlap. Callers of methods that
86  * return such codes should be aware that they are not
87  * globally unique. Implementors should be careful about blindly
88  * returning codes from other modules that might also use
89  * the generic base.
90  */
91 #define NS_ERROR_MODULE_GENERAL    51
92 
93 /**
94  * @name Severity Code.  This flag identifies the level of warning
95  */
96 
97 #define NS_ERROR_SEVERITY_SUCCESS       0
98 #define NS_ERROR_SEVERITY_ERROR         1
99 
100 /**
101  * @name Mozilla Code.  This flag separates consumers of mozilla code
102  *       from the native platform
103  */
104 
105 #define NS_ERROR_MODULE_BASE_OFFSET 0x45
106 
107 /* Helpers for defining our enum, to be undef'd later */
108 #define SUCCESS_OR_FAILURE(sev, module, code) \
109   ((uint32_t)(sev) << 31) | \
110   ((uint32_t)(module + NS_ERROR_MODULE_BASE_OFFSET) << 16) | \
111   (uint32_t)(code)
112 #define SUCCESS(code) \
113   SUCCESS_OR_FAILURE(NS_ERROR_SEVERITY_SUCCESS, MODULE, code)
114 #define FAILURE(code) \
115   SUCCESS_OR_FAILURE(NS_ERROR_SEVERITY_ERROR, MODULE, code)
116 
117 /**
118  * @name Standard return values
119  */
120 
121 /*@{*/
122 
123 enum class nsresult : uint32_t
124 {
125   #undef ERROR
126   #define ERROR(key, val) key = val
127   #include "ErrorList.h"
128   #undef ERROR
129 };
130 
131 /*
132  * enum classes don't place their initializers in the global scope, so we need
133  * constants for compatibility with old code.
134  */
135 const nsresult
136   #define ERROR(key, val) key = nsresult::key
137   #include "ErrorList.h"
138   #undef ERROR
139 ;
140 
141 #undef SUCCESS_OR_FAILURE
142 #undef SUCCESS
143 #undef FAILURE
144 
145 /**
146  * @name Standard Error Handling Macros
147  * @return 0 or 1 (false/true with bool type for C++)
148  */
149 
150 inline uint32_t
NS_FAILED_impl(nsresult aErr)151 NS_FAILED_impl(nsresult aErr)
152 {
153   return static_cast<uint32_t>(aErr) & 0x80000000;
154 }
155 #define NS_FAILED(_nsresult)    ((bool)MOZ_UNLIKELY(NS_FAILED_impl(_nsresult)))
156 #define NS_SUCCEEDED(_nsresult) ((bool)MOZ_LIKELY(!NS_FAILED_impl(_nsresult)))
157 
158 /* Check that our enum type is actually uint32_t as expected */
159 static_assert(((nsresult)0) < ((nsresult)-1),
160               "nsresult must be an unsigned type");
161 static_assert(sizeof(nsresult) == sizeof(uint32_t),
162               "nsresult must be 32 bits");
163 
164 #define MOZ_ALWAYS_SUCCEEDS(expr) MOZ_ALWAYS_TRUE(NS_SUCCEEDED(expr))
165 
166 /**
167  * @name Standard Error Generating Macros
168  */
169 
170 #define NS_ERROR_GENERATE(sev, module, code) \
171   (nsresult)(((uint32_t)(sev) << 31) | \
172              ((uint32_t)(module + NS_ERROR_MODULE_BASE_OFFSET) << 16) | \
173              ((uint32_t)(code)))
174 
175 #define NS_ERROR_GENERATE_SUCCESS(module, code) \
176   NS_ERROR_GENERATE(NS_ERROR_SEVERITY_SUCCESS, module, code)
177 
178 #define NS_ERROR_GENERATE_FAILURE(module, code) \
179   NS_ERROR_GENERATE(NS_ERROR_SEVERITY_ERROR, module, code)
180 
181  /*
182   * This will return the nsresult corresponding to the most recent NSPR failure
183   * returned by PR_GetError.
184   *
185   ***********************************************************************
186   *      Do not depend on this function. It will be going away!
187   ***********************************************************************
188   */
189 extern nsresult
190 NS_ErrorAccordingToNSPR();
191 
192 
193 /**
194  * @name Standard Macros for retrieving error bits
195  */
196 
197 inline constexpr uint16_t
NS_ERROR_GET_CODE(nsresult aErr)198 NS_ERROR_GET_CODE(nsresult aErr)
199 {
200   return uint32_t(aErr) & 0xffff;
201 }
202 inline constexpr uint16_t
NS_ERROR_GET_MODULE(nsresult aErr)203 NS_ERROR_GET_MODULE(nsresult aErr)
204 {
205   return ((uint32_t(aErr) >> 16) - NS_ERROR_MODULE_BASE_OFFSET) & 0x1fff;
206 }
207 inline bool
NS_ERROR_GET_SEVERITY(nsresult aErr)208 NS_ERROR_GET_SEVERITY(nsresult aErr)
209 {
210   return uint32_t(aErr) >> 31;
211 }
212 
213 
214 #ifdef _MSC_VER
215 #pragma warning(disable: 4251) /* 'nsCOMPtr<class nsIInputStream>' needs to have dll-interface to be used by clients of class 'nsInputStream' */
216 #pragma warning(disable: 4275) /* non dll-interface class 'nsISupports' used as base for dll-interface class 'nsIRDFNode' */
217 #endif
218 
219 #endif
220