Bug 464858 - intermittent / recurring fail of test_db_update_v1.js. bustagefix.
[wine-gecko.git] / dom / src / base / nsDOMException.cpp
blob80e63f70d916c1c6a4b98d26bd1afa99892b296a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsCOMPtr.h"
40 #include "nsCRTGlue.h"
41 #include "nsDOMClassInfo.h"
42 #include "nsDOMError.h"
43 #include "nsDOMException.h"
44 #include "nsIDOMDOMException.h"
45 #include "nsIDOMRangeException.h"
46 #include "nsIDOMFileException.h"
47 #ifdef MOZ_SVG
48 #include "nsIDOMSVGException.h"
49 #endif
50 #include "nsIDOMXPathException.h"
51 #include "nsString.h"
52 #include "prprf.h"
54 #define DOM_MSG_DEF(val, message) {(val), #val, message},
56 #define IMPL_INTERNAL_DOM_EXCEPTION_HEAD(classname, ifname) \
57 class classname : public nsBaseDOMException, \
58 public ifname \
59 { \
60 public: \
61 classname(); \
62 virtual ~classname(); \
64 NS_DECL_ISUPPORTS_INHERITED
66 #define IMPL_INTERNAL_DOM_EXCEPTION_TAIL(classname, ifname, domname, module, \
67 mapping_function) \
68 }; \
70 classname::classname() {} \
71 classname::~classname() {} \
73 NS_IMPL_ADDREF_INHERITED(classname, nsBaseDOMException) \
74 NS_IMPL_RELEASE_INHERITED(classname, nsBaseDOMException) \
75 NS_CLASSINFO_MAP_BEGIN(domname) \
76 NS_CLASSINFO_MAP_ENTRY(nsIException) \
77 NS_CLASSINFO_MAP_ENTRY(ifname) \
78 NS_CLASSINFO_MAP_END \
79 NS_INTERFACE_MAP_BEGIN(classname) \
80 NS_INTERFACE_MAP_ENTRY(ifname) \
81 NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(domname) \
82 NS_INTERFACE_MAP_END_INHERITING(nsBaseDOMException) \
84 nsresult \
85 NS_New##domname(nsresult aNSResult, nsIException* aDefaultException, \
86 nsIException** aException) \
87 { \
88 if (!(NS_ERROR_GET_MODULE(aNSResult) == module)) { \
89 NS_WARNING("Trying to create an exception for the wrong error module."); \
90 return NS_ERROR_FAILURE; \
91 } \
92 const char* name; \
93 const char* message; \
94 mapping_function(aNSResult, &name, &message); \
95 classname* inst = new classname(); \
96 NS_ENSURE_TRUE(inst, NS_ERROR_OUT_OF_MEMORY); \
97 inst->Init(aNSResult, name, message, aDefaultException); \
98 *aException = inst; \
99 NS_ADDREF(*aException); \
100 return NS_OK; \
103 static struct ResultStruct
105 nsresult mNSResult;
106 const char* mName;
107 const char* mMessage;
108 } gDOMErrorMsgMap[] = {
109 #include "domerr.msg"
110 {0, nsnull, nsnull} // sentinel to mark end of array
113 #undef DOM_MSG_DEF
115 static void
116 NSResultToNameAndMessage(nsresult aNSResult,
117 const char** aName,
118 const char** aMessage)
120 ResultStruct* result_struct = gDOMErrorMsgMap;
122 while (result_struct->mName) {
123 if (aNSResult == result_struct->mNSResult) {
124 *aName = result_struct->mName;
125 *aMessage = result_struct->mMessage;
126 return;
129 ++result_struct;
132 NS_WARNING("Huh, someone is throwing non-DOM errors using the DOM module!");
134 return;
137 IMPL_INTERNAL_DOM_EXCEPTION_HEAD(nsDOMException, nsIDOMDOMException)
138 NS_DECL_NSIDOMDOMEXCEPTION
139 IMPL_INTERNAL_DOM_EXCEPTION_TAIL(nsDOMException, nsIDOMDOMException,
140 DOMException, NS_ERROR_MODULE_DOM,
141 NSResultToNameAndMessage)
143 NS_IMETHODIMP
144 nsDOMException::GetCode(PRUint32* aCode)
146 NS_ENSURE_ARG_POINTER(aCode);
147 nsresult result;
148 GetResult(&result);
149 *aCode = NS_ERROR_GET_CODE(result);
151 return NS_OK;
154 IMPL_INTERNAL_DOM_EXCEPTION_HEAD(nsRangeException, nsIDOMRangeException)
155 NS_DECL_NSIDOMRANGEEXCEPTION
156 IMPL_INTERNAL_DOM_EXCEPTION_TAIL(nsRangeException, nsIDOMRangeException,
157 RangeException, NS_ERROR_MODULE_DOM_RANGE,
158 NSResultToNameAndMessage)
160 NS_IMETHODIMP
161 nsRangeException::GetCode(PRUint16* aCode)
163 NS_ENSURE_ARG_POINTER(aCode);
164 nsresult result;
165 GetResult(&result);
166 *aCode = NS_ERROR_GET_CODE(result);
168 return NS_OK;
171 #ifdef MOZ_SVG
172 IMPL_INTERNAL_DOM_EXCEPTION_HEAD(nsSVGException, nsIDOMSVGException)
173 NS_DECL_NSIDOMSVGEXCEPTION
174 IMPL_INTERNAL_DOM_EXCEPTION_TAIL(nsSVGException, nsIDOMSVGException,
175 SVGException, NS_ERROR_MODULE_SVG,
176 NSResultToNameAndMessage)
178 NS_IMETHODIMP
179 nsSVGException::GetCode(PRUint16* aCode)
181 NS_ENSURE_ARG_POINTER(aCode);
182 nsresult result;
183 GetResult(&result);
184 *aCode = NS_ERROR_GET_CODE(result);
186 return NS_OK;
188 #endif // MOZ_SVG
190 IMPL_INTERNAL_DOM_EXCEPTION_HEAD(nsXPathException, nsIDOMXPathException)
191 NS_DECL_NSIDOMXPATHEXCEPTION
192 IMPL_INTERNAL_DOM_EXCEPTION_TAIL(nsXPathException, nsIDOMXPathException,
193 XPathException, NS_ERROR_MODULE_DOM_XPATH,
194 NSResultToNameAndMessage)
196 NS_IMETHODIMP
197 nsXPathException::GetCode(PRUint16* aCode)
199 NS_ENSURE_ARG_POINTER(aCode);
200 nsresult result;
201 GetResult(&result);
202 *aCode = NS_ERROR_GET_CODE(result);
204 return NS_OK;
207 IMPL_INTERNAL_DOM_EXCEPTION_HEAD(nsDOMFileException, nsIDOMFileException)
208 NS_DECL_NSIDOMFILEEXCEPTION
209 IMPL_INTERNAL_DOM_EXCEPTION_TAIL(nsDOMFileException, nsIDOMFileException,
210 FileException, NS_ERROR_MODULE_DOM_FILE,
211 NSResultToNameAndMessage)
213 NS_IMETHODIMP
214 nsDOMFileException::GetCode(PRUint16* aCode)
216 NS_ENSURE_ARG_POINTER(aCode);
217 nsresult result;
218 GetResult(&result);
219 *aCode = NS_ERROR_GET_CODE(result);
221 return NS_OK;
224 nsBaseDOMException::nsBaseDOMException()
228 nsBaseDOMException::~nsBaseDOMException()
232 NS_IMPL_ISUPPORTS2(nsBaseDOMException, nsIException, nsIBaseDOMException)
234 NS_IMETHODIMP
235 nsBaseDOMException::GetMessageMoz(char **aMessage)
237 if (mMessage) {
238 *aMessage = NS_strdup(mMessage);
239 } else {
240 *aMessage = nsnull;
243 return NS_OK;
246 NS_IMETHODIMP
247 nsBaseDOMException::GetResult(PRUint32* aResult)
249 NS_ENSURE_ARG_POINTER(aResult);
251 *aResult = mResult;
253 return NS_OK;
256 NS_IMETHODIMP
257 nsBaseDOMException::GetName(char **aName)
259 NS_ENSURE_ARG_POINTER(aName);
261 if (mName) {
262 *aName = NS_strdup(mName);
263 } else {
264 *aName = nsnull;
267 return NS_OK;
270 NS_IMETHODIMP
271 nsBaseDOMException::GetFilename(char **aFilename)
273 if (mInner) {
274 return mInner->GetFilename(aFilename);
277 NS_ENSURE_ARG_POINTER(aFilename);
279 *aFilename = nsnull;
281 return NS_OK;
284 NS_IMETHODIMP
285 nsBaseDOMException::GetLineNumber(PRUint32 *aLineNumber)
287 if (mInner) {
288 return mInner->GetLineNumber(aLineNumber);
291 NS_ENSURE_ARG_POINTER(aLineNumber);
293 *aLineNumber = 0;
295 return NS_OK;
298 NS_IMETHODIMP
299 nsBaseDOMException::GetColumnNumber(PRUint32 *aColumnNumber)
301 if (mInner) {
302 return mInner->GetColumnNumber(aColumnNumber);
305 NS_ENSURE_ARG_POINTER(aColumnNumber);
307 *aColumnNumber = 0;
309 return NS_OK;
312 NS_IMETHODIMP
313 nsBaseDOMException::GetLocation(nsIStackFrame **aLocation)
315 if (mInner) {
316 return mInner->GetLocation(aLocation);
319 NS_ENSURE_ARG_POINTER(aLocation);
321 *aLocation = nsnull;
323 return NS_OK;
326 NS_IMETHODIMP
327 nsBaseDOMException::GetInner(nsIException **aInner)
329 NS_ENSURE_ARG_POINTER(aInner);
331 *aInner = nsnull;
333 return NS_OK;
336 NS_IMETHODIMP
337 nsBaseDOMException::GetData(nsISupports **aData)
339 if (mInner) {
340 return mInner->GetData(aData);
343 NS_ENSURE_ARG_POINTER(aData);
345 *aData = nsnull;
347 return NS_OK;
350 NS_IMETHODIMP
351 nsBaseDOMException::ToString(char **aReturn)
353 *aReturn = nsnull;
355 static const char defaultMsg[] = "<no message>";
356 static const char defaultLocation[] = "<unknown>";
357 static const char defaultName[] = "<unknown>";
358 static const char format[] =
359 "[Exception... \"%s\" code: \"%d\" nsresult: \"0x%x (%s)\" location: \"%s\"]";
361 nsCAutoString location;
363 if (mInner) {
364 nsXPIDLCString filename;
366 mInner->GetFilename(getter_Copies(filename));
368 if (!filename.IsEmpty()) {
369 PRUint32 line_nr = 0;
371 mInner->GetLineNumber(&line_nr);
373 char *temp = PR_smprintf("%s Line: %d", filename.get(), line_nr);
374 if (temp) {
375 location.Assign(temp);
376 PR_smprintf_free(temp);
381 if (location.IsEmpty()) {
382 location = defaultLocation;
385 const char* msg = mMessage ? mMessage : defaultMsg;
386 const char* resultName = mName ? mName : defaultName;
387 PRUint32 code = NS_ERROR_GET_CODE(mResult);
389 *aReturn = PR_smprintf(format, msg, code, mResult, resultName,
390 location.get());
392 return *aReturn ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
395 NS_IMETHODIMP
396 nsBaseDOMException::Init(nsresult aNSResult, const char* aName,
397 const char* aMessage,
398 nsIException* aDefaultException)
400 mResult = aNSResult;
401 mName = aName;
402 mMessage = aMessage;
403 mInner = aDefaultException;
404 return NS_OK;