View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.dom;
16  
17  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
18  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
19  
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.htmlunit.javascript.HtmlUnitScriptable;
24  import org.htmlunit.javascript.JavaScriptEngine;
25  import org.htmlunit.javascript.configuration.JsxClass;
26  import org.htmlunit.javascript.configuration.JsxConstant;
27  import org.htmlunit.javascript.configuration.JsxConstructor;
28  import org.htmlunit.javascript.configuration.JsxGetter;
29  
30  /**
31   * Exception for DOM manipulations.
32   *
33   * @see <a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-17189187">
34   * DOM-Level-2-Core</a>
35   * @author Marc Guillemot
36   * @author Frank Danek
37   * @author Ahmed Ashour
38   * @author Ronald Brill
39   */
40  @JsxClass
41  public class DOMException extends HtmlUnitScriptable {
42  
43      /** If the specified range of text does not fit into a DOMString. */
44      @JsxConstant
45      public static final int DOMSTRING_SIZE_ERR = org.w3c.dom.DOMException.DOMSTRING_SIZE_ERR;
46  
47      /** If any node is inserted somewhere it doesn't belong. */
48      @JsxConstant
49      public static final int HIERARCHY_REQUEST_ERR = org.w3c.dom.DOMException.HIERARCHY_REQUEST_ERR;
50  
51      /** If index or size is negative, or greater than the allowed value. */
52      @JsxConstant
53      public static final int INDEX_SIZE_ERR = org.w3c.dom.DOMException.INDEX_SIZE_ERR;
54  
55      /** If an attempt is made to add an attribute that is already in use elsewhere. */
56      @JsxConstant
57      public static final int INUSE_ATTRIBUTE_ERR = org.w3c.dom.DOMException.INUSE_ATTRIBUTE_ERR;
58  
59      /** If a parameter or an operation is not supported by the underlying object. */
60      @JsxConstant
61      public static final int INVALID_ACCESS_ERR = org.w3c.dom.DOMException.INVALID_ACCESS_ERR;
62  
63      /** If an invalid or illegal character is specified, such as in a name. */
64      @JsxConstant
65      public static final int INVALID_CHARACTER_ERR = org.w3c.dom.DOMException.INVALID_CHARACTER_ERR;
66  
67      /** If an attempt is made to modify the type of the underlying object. */
68      @JsxConstant
69      public static final int INVALID_MODIFICATION_ERR = org.w3c.dom.DOMException.INVALID_MODIFICATION_ERR;
70  
71      /** If an attempt is made to use an object that is not, or is no longer, usable. */
72      @JsxConstant
73      public static final int INVALID_STATE_ERR = org.w3c.dom.DOMException.INVALID_STATE_ERR;
74  
75      /** If an attempt is made to create or change an object in a way which is incorrect with regard to namespaces. */
76      @JsxConstant
77      public static final int NAMESPACE_ERR = org.w3c.dom.DOMException.NAMESPACE_ERR;
78  
79      /** If data is specified for a node which does not support data. */
80      @JsxConstant
81      public static final int NO_DATA_ALLOWED_ERR = org.w3c.dom.DOMException.NO_DATA_ALLOWED_ERR;
82  
83      /** If an attempt is made to modify an object where modifications are not allowed. */
84      @JsxConstant
85      public static final int NO_MODIFICATION_ALLOWED_ERR = org.w3c.dom.DOMException.NO_MODIFICATION_ALLOWED_ERR;
86  
87      /** If an attempt is made to reference a node in a context where it does not exist. */
88      @JsxConstant
89      public static final int NOT_FOUND_ERR = org.w3c.dom.DOMException.NOT_FOUND_ERR;
90  
91      /** If the implementation does not support the requested type of object or operation. */
92      @JsxConstant
93      public static final int NOT_SUPPORTED_ERR = org.w3c.dom.DOMException.NOT_SUPPORTED_ERR;
94  
95      /** If an invalid or illegal string is specified. */
96      @JsxConstant
97      public static final int SYNTAX_ERR = org.w3c.dom.DOMException.SYNTAX_ERR;
98  
99      /** If a node is used in a different document than the one that created it (that doesn't support it). */
100     @JsxConstant
101     public static final int WRONG_DOCUMENT_ERR = org.w3c.dom.DOMException.WRONG_DOCUMENT_ERR;
102 
103     /** If a call to a method would make the {@code Node} invalid with respect to "partial validity". */
104     @JsxConstant
105     public static final int VALIDATION_ERR = org.w3c.dom.DOMException.VALIDATION_ERR;
106 
107     /** If the type of an object is incompatible with the expected type of the parameter. */
108     @JsxConstant
109     public static final int TYPE_MISMATCH_ERR = org.w3c.dom.DOMException.TYPE_MISMATCH_ERR;
110 
111     /** Security error. */
112     @JsxConstant
113     public static final int SECURITY_ERR = 18;
114 
115     /** Network error. */
116     @JsxConstant
117     public static final int NETWORK_ERR = 19;
118 
119     /** Abort error. */
120     @JsxConstant
121     public static final int ABORT_ERR = 20;
122 
123     /** URL mismatch error. */
124     @JsxConstant
125     public static final int URL_MISMATCH_ERR = 21;
126 
127     /** Quota exceeded error. */
128     @JsxConstant
129     public static final int QUOTA_EXCEEDED_ERR = 22;
130 
131     /** Timeout error. */
132     @JsxConstant
133     public static final int TIMEOUT_ERR = 23;
134 
135     /** Invalid node type error. */
136     @JsxConstant
137     public static final int INVALID_NODE_TYPE_ERR = 24;
138 
139     /** Data clone error. */
140     @JsxConstant
141     public static final int DATA_CLONE_ERR = 25;
142 
143     private static final List<String> COMMON_ERROR_NAMES = Arrays.asList(new String[] {
144         "IndexSizeError",
145         null,
146         "HierarchyRequestError",
147         "WrongDocumentError",
148         "InvalidCharacterError",
149         null,
150         "NoModificationAllowedError",
151         "NotFoundError",
152         "NotSupportedError",
153         "InUseAttributeError",
154         "InvalidStateError",
155         "SyntaxError",
156         "InvalidModificationError",
157         "NamespaceError",
158         "InvalidAccessError",
159         null,
160         "TypeMismatchError",
161         "SecurityError",
162         "NetworkError",
163         "AbortError",
164         "URLMismatchError",
165         "QuotaExceededError",
166         "TimeoutError",
167         "InvalidNodeTypeError",
168         "DataCloneError"});
169 
170     private int code_;
171     private String name_;
172     private String message_;
173     private int lineNumber_;
174     private String fileName_;
175 
176     /**
177      * Default constructor used to build the prototype.
178      */
179     public DOMException() {
180     }
181 
182     /**
183      * JavaScript constructor.
184      * @param message a description of the exception. If not present, the empty string '' is used
185      * @param error If the specified name is a standard error name,
186      *        then getting the code property of the DOMException object will return the
187      *        code number corresponding to the specified name.
188      *        If not present, the string 'Error' is used.
189      */
190     @JsxConstructor
191     public void jsConstructor(final String message, final Object error) {
192         message_ = message;
193 
194         if (error == null) {
195             code_ = 0;
196             name_ = null;
197             return;
198         }
199 
200         if (JavaScriptEngine.isUndefined(error)) {
201             code_ = 0;
202             name_ = "Error";
203             return;
204         }
205 
206         name_ = JavaScriptEngine.toString(error);
207 
208         final int idx = COMMON_ERROR_NAMES.indexOf(name_);
209         if (idx != -1) {
210             code_ = idx + 1;
211             return;
212         }
213 
214         code_ = 0;
215     }
216 
217     /**
218      * Constructor.
219      * @param message the exception message
220      * @param error the error code (on of the constants from the class)
221      */
222     public DOMException(final String message, final int error) {
223         message_ = message;
224         code_ = error;
225         name_ = COMMON_ERROR_NAMES.get(error - 1);
226     }
227 
228     /**
229      * Gets the exception code.
230      * @return the exception code
231      */
232     @JsxGetter
233     public Object getCode() {
234         if (code_ == -1) {
235             return JavaScriptEngine.UNDEFINED;
236         }
237         return code_;
238     }
239 
240     /**
241      * Gets the exception name.
242      * @return the exception name
243      */
244     @JsxGetter
245     public String getName() {
246         return name_;
247     }
248 
249     /**
250      * Gets the exception message.
251      * @return the exception message
252      */
253     @JsxGetter
254     public Object getMessage() {
255         if (message_ == null) {
256             return JavaScriptEngine.UNDEFINED;
257         }
258         return message_;
259     }
260 
261     /**
262      * Gets the line at which the exception occurred.
263      * @return the line of the exception
264      */
265     @JsxGetter({FF, FF_ESR})
266     public Object getLineNumber() {
267         if (lineNumber_ == -1) {
268             return JavaScriptEngine.UNDEFINED;
269         }
270         return lineNumber_;
271     }
272 
273     /**
274      * Gets the name of the in which the exception occurred.
275      * @return the name of the source file
276      */
277     @JsxGetter({FF, FF_ESR})
278     public Object getFilename() {
279         if (fileName_ == null) {
280             return JavaScriptEngine.UNDEFINED;
281         }
282         return fileName_;
283     }
284 
285     /**
286      * Sets the location in JavaScript source where this exception occurred.
287      * @param fileName the name of the source file
288      * @param lineNumber the line number
289      */
290     public void setLocation(final String fileName, final int lineNumber) {
291         fileName_ = fileName;
292         lineNumber_ = lineNumber;
293     }
294 }