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/. */
7 #ifndef mozilla_dom_DOMException_h__
8 #define mozilla_dom_DOMException_h__
10 // We intentionally shadow non-virtual methods, but gcc gets confused.
12 # pragma GCC diagnostic push
13 # pragma GCC diagnostic ignored "-Woverloaded-virtual"
20 #include "nsCycleCollectionParticipant.h"
22 #include "nsWrapperCache.h"
23 #include "nsIException.h"
25 #include "mozilla/dom/BindingDeclarations.h"
27 class nsIGlobalObject
;
30 nsresult
NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult
,
33 uint16_t* aCode
= nullptr);
42 #define MOZILLA_EXCEPTION_IID \
44 0x55eda557, 0xeba0, 0x4fe3, { \
45 0xae, 0x2e, 0xf3, 0x94, 0x49, 0x23, 0x62, 0xd6 \
49 class Exception
: public nsIException
, public nsWrapperCache
{
51 NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_EXCEPTION_IID
)
53 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Exception
)
55 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
58 const nsCString
& GetMessageMoz() const { return mMessage
; }
59 nsresult
GetResult() const { return mResult
; }
60 // DOMException wants different ToString behavior, so allow it to override.
61 virtual void ToString(JSContext
* aCx
, nsACString
& aReturn
);
63 // Cruft used by XPConnect for exceptions originating in JS implemented
65 bool StealJSVal(JS::Value
* aVp
);
66 void StowJSVal(JS::Value
& aVp
);
69 virtual JSObject
* WrapObject(JSContext
* cx
,
70 JS::Handle
<JSObject
*> aGivenProto
) override
;
72 nsISupports
* GetParentObject() const { return nullptr; }
74 void GetMessageMoz(nsString
& retval
);
76 uint32_t Result() const;
78 void GetName(nsAString
& retval
);
80 virtual void GetErrorMessage(nsAString
& aRetVal
) {
81 // Since GetName is non non-virtual and deals with different
82 // member variables in Exception vs. DOMException, have a virtual
83 // method to ensure the right error message creation.
86 CreateErrorMessage(name
, aRetVal
);
89 void GetFilename(JSContext
* aCx
, nsACString
& aFilename
);
91 uint32_t SourceId(JSContext
* aCx
) const;
93 uint32_t LineNumber(JSContext
* aCx
) const;
95 uint32_t ColumnNumber() const;
97 already_AddRefed
<nsIStackFrame
> GetLocation() const;
99 nsISupports
* GetData() const;
101 void GetStack(JSContext
* aCx
, nsAString
& aStack
) const;
103 void Stringify(JSContext
* aCx
, nsString
& retval
);
105 Exception(const nsACString
& aMessage
, nsresult aResult
,
106 const nsACString
& aName
, nsIStackFrame
* aLocation
,
108 Exception(nsCString
&& aMessage
, nsresult aResult
, nsCString
&& aName
);
111 virtual ~Exception();
113 void CreateErrorMessage(const nsAString
& aName
, nsAString
& aRetVal
) {
114 // Create similar error message as what ErrorReport::init does in jsexn.cpp.
115 if (!aName
.IsEmpty() && !mMessage
.IsEmpty()) {
116 aRetVal
.Assign(aName
);
117 aRetVal
.AppendLiteral(": ");
118 AppendUTF8toUTF16(mMessage
, aRetVal
);
119 } else if (!aName
.IsEmpty()) {
120 aRetVal
.Assign(aName
);
121 } else if (!mMessage
.IsEmpty()) {
122 CopyUTF8toUTF16(mMessage
, aRetVal
);
131 nsCOMPtr
<nsIStackFrame
> mLocation
;
132 nsCOMPtr
<nsISupports
> mData
;
135 JS::Heap
<JS::Value
> mThrownJSVal
;
138 NS_DEFINE_STATIC_IID_ACCESSOR(Exception
, MOZILLA_EXCEPTION_IID
)
140 class DOMException
: public Exception
{
142 DOMException(nsresult aRv
, const nsACString
& aMessage
,
143 const nsACString
& aName
, uint16_t aCode
,
144 nsIStackFrame
* aLocation
= nullptr);
145 DOMException(nsresult aRv
, nsCString
&& aMessage
, nsCString
&& aName
,
148 NS_INLINE_DECL_REFCOUNTING_INHERITED(DOMException
, Exception
)
150 // nsWrapperCache overrides
151 virtual JSObject
* WrapObject(JSContext
* aCx
,
152 JS::Handle
<JSObject
*> aGivenProto
) override
;
154 static already_AddRefed
<DOMException
> Constructor(
155 GlobalObject
& /* unused */, const nsAString
& aMessage
,
156 const Optional
<nsAString
>& aName
);
158 uint16_t Code() const { return mCode
; }
160 // Intentionally shadow the Exception version.
161 void GetName(nsString
& retval
);
163 // Exception overrides
164 void ToString(JSContext
* aCx
, nsACString
& aReturn
) override
;
166 virtual void GetErrorMessage(nsAString
& aRetVal
) override
{
167 // See the comment in Exception::GetErrorMessage.
170 CreateErrorMessage(name
, aRetVal
);
173 static already_AddRefed
<DOMException
> Create(nsresult aRv
);
175 static already_AddRefed
<DOMException
> Create(nsresult aRv
,
176 const nsACString
& aMessage
);
178 static already_AddRefed
<DOMException
> ReadStructuredClone(
179 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
180 JSStructuredCloneReader
* aReader
);
181 bool WriteStructuredClone(JSContext
* aCx
,
182 JSStructuredCloneWriter
* aWriter
) const;
185 virtual ~DOMException() = default;
191 } // namespace mozilla
194 # pragma GCC diagnostic pop