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
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.
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 ***** */
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"
48 #include "nsIDOMSVGException.h"
50 #include "nsIDOMXPathException.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, \
62 virtual ~classname(); \
64 NS_DECL_ISUPPORTS_INHERITED
66 #define IMPL_INTERNAL_DOM_EXCEPTION_TAIL(classname, ifname, domname, module, \
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) \
85 NS_New##domname(nsresult aNSResult, nsIException* aDefaultException, \
86 nsIException** aException) \
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; \
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); \
99 NS_ADDREF(*aException); \
103 static struct ResultStruct
107 const char* mMessage
;
108 } gDOMErrorMsgMap
[] = {
109 #include "domerr.msg"
110 {0, nsnull
, nsnull
} // sentinel to mark end of array
116 NSResultToNameAndMessage(nsresult aNSResult
,
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
;
132 NS_WARNING("Huh, someone is throwing non-DOM errors using the DOM module!");
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
)
144 nsDOMException::GetCode(PRUint32
* aCode
)
146 NS_ENSURE_ARG_POINTER(aCode
);
149 *aCode
= NS_ERROR_GET_CODE(result
);
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
)
161 nsRangeException::GetCode(PRUint16
* aCode
)
163 NS_ENSURE_ARG_POINTER(aCode
);
166 *aCode
= NS_ERROR_GET_CODE(result
);
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
)
179 nsSVGException::GetCode(PRUint16
* aCode
)
181 NS_ENSURE_ARG_POINTER(aCode
);
184 *aCode
= NS_ERROR_GET_CODE(result
);
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
)
197 nsXPathException::GetCode(PRUint16
* aCode
)
199 NS_ENSURE_ARG_POINTER(aCode
);
202 *aCode
= NS_ERROR_GET_CODE(result
);
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
)
214 nsDOMFileException::GetCode(PRUint16
* aCode
)
216 NS_ENSURE_ARG_POINTER(aCode
);
219 *aCode
= NS_ERROR_GET_CODE(result
);
224 nsBaseDOMException::nsBaseDOMException()
228 nsBaseDOMException::~nsBaseDOMException()
232 NS_IMPL_ISUPPORTS2(nsBaseDOMException
, nsIException
, nsIBaseDOMException
)
235 nsBaseDOMException::GetMessageMoz(char **aMessage
)
238 *aMessage
= NS_strdup(mMessage
);
247 nsBaseDOMException::GetResult(PRUint32
* aResult
)
249 NS_ENSURE_ARG_POINTER(aResult
);
257 nsBaseDOMException::GetName(char **aName
)
259 NS_ENSURE_ARG_POINTER(aName
);
262 *aName
= NS_strdup(mName
);
271 nsBaseDOMException::GetFilename(char **aFilename
)
274 return mInner
->GetFilename(aFilename
);
277 NS_ENSURE_ARG_POINTER(aFilename
);
285 nsBaseDOMException::GetLineNumber(PRUint32
*aLineNumber
)
288 return mInner
->GetLineNumber(aLineNumber
);
291 NS_ENSURE_ARG_POINTER(aLineNumber
);
299 nsBaseDOMException::GetColumnNumber(PRUint32
*aColumnNumber
)
302 return mInner
->GetColumnNumber(aColumnNumber
);
305 NS_ENSURE_ARG_POINTER(aColumnNumber
);
313 nsBaseDOMException::GetLocation(nsIStackFrame
**aLocation
)
316 return mInner
->GetLocation(aLocation
);
319 NS_ENSURE_ARG_POINTER(aLocation
);
327 nsBaseDOMException::GetInner(nsIException
**aInner
)
329 NS_ENSURE_ARG_POINTER(aInner
);
337 nsBaseDOMException::GetData(nsISupports
**aData
)
340 return mInner
->GetData(aData
);
343 NS_ENSURE_ARG_POINTER(aData
);
351 nsBaseDOMException::ToString(char **aReturn
)
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
;
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
);
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
,
392 return *aReturn
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
396 nsBaseDOMException::Init(nsresult aNSResult
, const char* aName
,
397 const char* aMessage
,
398 nsIException
* aDefaultException
)
403 mInner
= aDefaultException
;