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