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 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(
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         super();
181     }
182 
183     /**
184      * Constructor.
185      * @param message the exception message
186      * @param error the error code (on of the constants from the class)
187      */
188     public DOMException(final String message, final int error) {
189         message_ = message;
190         code_ = error;
191         name_ = COMMON_ERROR_NAMES.get(error - 1);
192     }
193 
194     /**
195      * JavaScript constructor.
196      * @param message a description of the exception. If not present, the empty string '' is used
197      * @param error If the specified name is a standard error name,
198      *        then getting the code property of the DOMException object will return the
199      *        code number corresponding to the specified name.
200      *        If not present, the string 'Error' is used.
201      */
202     @JsxConstructor
203     public void jsConstructor(final String message, final Object error) {
204         message_ = message;
205 
206         if (error == null) {
207             code_ = 0;
208             name_ = null;
209             return;
210         }
211 
212         if (JavaScriptEngine.isUndefined(error)) {
213             code_ = 0;
214             name_ = "Error";
215             return;
216         }
217 
218         name_ = JavaScriptEngine.toString(error);
219 
220         final int idx = COMMON_ERROR_NAMES.indexOf(name_);
221         if (idx != -1) {
222             code_ = idx + 1;
223             return;
224         }
225 
226         code_ = 0;
227     }
228 
229     /**
230      * Gets the exception code.
231      * @return the exception code
232      */
233     @JsxGetter
234     public Object getCode() {
235         if (code_ == -1) {
236             return JavaScriptEngine.UNDEFINED;
237         }
238         return code_;
239     }
240 
241     /**
242      * Gets the exception name.
243      * @return the exception name
244      */
245     @JsxGetter
246     public String getName() {
247         return name_;
248     }
249 
250     /**
251      * Gets the exception message.
252      * @return the exception message
253      */
254     @JsxGetter
255     public Object getMessage() {
256         if (message_ == null) {
257             return JavaScriptEngine.UNDEFINED;
258         }
259         return message_;
260     }
261 
262     /**
263      * Gets the line at which the exception occurred.
264      * @return the line of the exception
265      */
266     @JsxGetter({FF, FF_ESR})
267     public Object getLineNumber() {
268         if (lineNumber_ == -1) {
269             return JavaScriptEngine.UNDEFINED;
270         }
271         return lineNumber_;
272     }
273 
274     /**
275      * Gets the name of the in which the exception occurred.
276      * @return the name of the source file
277      */
278     @JsxGetter({FF, FF_ESR})
279     public Object getFilename() {
280         if (fileName_ == null) {
281             return JavaScriptEngine.UNDEFINED;
282         }
283         return fileName_;
284     }
285 
286     /**
287      * Sets the location in JavaScript source where this exception occurred.
288      * @param fileName the name of the source file
289      * @param lineNumber the line number
290      */
291     public void setLocation(final String fileName, final int lineNumber) {
292         fileName_ = fileName;
293         lineNumber_ = lineNumber;
294     }
295 }