Skip to content

Commit 4f9c94d

Browse files
committed
more on error handling
1 parent 57da32c commit 4f9c94d

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/main/java/org/htmlunit/javascript/host/canvas/ImageData.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.htmlunit.javascript.configuration.JsxClass;
2626
import org.htmlunit.javascript.configuration.JsxConstructor;
2727
import org.htmlunit.javascript.configuration.JsxGetter;
28+
import org.htmlunit.javascript.host.dom.DOMException;
2829
import org.htmlunit.platform.canvas.rendering.RenderingBackend;
2930

3031
/**
@@ -61,7 +62,7 @@ public ImageData() {
6162
public static ImageData jsConstructor(final Context cx, final Scriptable scope,
6263
final Object[] args, final Function ctorObj, final boolean inNewExpr) {
6364
if (args.length < 2) {
64-
throw JavaScriptEngine.reportRuntimeError("ImageData ctor - too less arguments");
65+
throw JavaScriptEngine.typeError("ImageData ctor - too less arguments");
6566
}
6667

6768
NativeUint8ClampedArray data = null;
@@ -70,23 +71,35 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
7071
if (args[0] instanceof NativeUint8ClampedArray) {
7172
data = (NativeUint8ClampedArray) args[0];
7273
if (data.getArrayLength() % 4 != 0) {
73-
throw JavaScriptEngine.reportRuntimeError("ImageData ctor - data length mod 4 not zero");
74+
throw JavaScriptEngine.asJavaScriptException(
75+
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
76+
new DOMException(
77+
"ImageData ctor - data length mod 4 not zero",
78+
DOMException.INVALID_STATE_ERR));
7479
}
7580

7681
width = (int) JavaScriptEngine.toInteger(args[1]);
7782
if (args.length < 3) {
7883
height = data.getArrayLength() / 4 / width;
7984

8085
if (data.getArrayLength() != 4 * width * height) {
81-
throw JavaScriptEngine.reportRuntimeError("ImageData ctor - width not correct");
86+
throw JavaScriptEngine.asJavaScriptException(
87+
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
88+
new DOMException(
89+
"ImageData ctor - width not correct",
90+
DOMException.INDEX_SIZE_ERR));
8291
}
8392
}
8493
else {
8594
height = (int) JavaScriptEngine.toInteger(args[2]);
8695
}
8796

8897
if (data.getArrayLength() != 4 * width * height) {
89-
throw JavaScriptEngine.reportRuntimeError("ImageData ctor - width/height not correct");
98+
throw JavaScriptEngine.asJavaScriptException(
99+
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
100+
new DOMException(
101+
"ImageData ctor - width/height not correct",
102+
DOMException.INDEX_SIZE_ERR));
90103
}
91104
}
92105
else {
@@ -95,10 +108,18 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
95108
}
96109

97110
if (width < 0) {
98-
throw JavaScriptEngine.reportRuntimeError("ImageData ctor - width negative");
111+
throw JavaScriptEngine.asJavaScriptException(
112+
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
113+
new DOMException(
114+
"ImageData ctor - width negative",
115+
DOMException.INDEX_SIZE_ERR));
99116
}
100117
if (height < 0) {
101-
throw JavaScriptEngine.reportRuntimeError("ImageData ctor - height negative");
118+
throw JavaScriptEngine.asJavaScriptException(
119+
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
120+
new DOMException(
121+
"ImageData ctor - height negative",
122+
DOMException.INDEX_SIZE_ERR));
102123
}
103124

104125
final ImageData result = new ImageData(null, 0, 0, width, height);

src/test/java/org/htmlunit/javascript/host/canvas/ImageDataTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public void ctorArrayWidthHeight() throws Exception {
122122
* @throws Exception if an error occurs
123123
*/
124124
@Test
125-
@Alerts({"exception", "exception", "exception", "exception", "exception", "exception", "exception"})
125+
@Alerts({"TypeError", "IndexSizeError", "IndexSizeError", "IndexSizeError",
126+
"IndexSizeError", "InvalidStateError", "IndexSizeError"})
126127
public void ctorArrayInvalid() throws Exception {
127128
final String html =
128129
"<html><head><script>\n"
@@ -131,34 +132,34 @@ public void ctorArrayInvalid() throws Exception {
131132

132133
+ " try {\n"
133134
+ " var imageData = new ImageData();\n"
134-
+ " } catch (e) { log('exception');}\n"
135+
+ " } catch (e) { log(e.name);}\n"
135136

136137
+ " try {\n"
137138
+ " var imageData = new ImageData(-2, 1);\n"
138-
+ " } catch (e) { log('exception');}\n"
139+
+ " } catch (e) { log(e.name);}\n"
139140

140141
+ " try {\n"
141142
+ " var imageData = new ImageData(2, -1);\n"
142-
+ " } catch (e) { log('exception');}\n"
143+
+ " } catch (e) { log(e.name);}\n"
143144

144145
+ " try {\n"
145146
+ " var imageData = new ImageData(-2, -1);\n"
146-
+ " } catch (e) { log('exception');}\n"
147+
+ " } catch (e) { log(e.name);}\n"
147148

148149
+ " var arr = new Uint8ClampedArray(8);\n"
149150
+ " try {\n"
150151
+ " var imageData = new ImageData(arr, 3);\n"
151-
+ " } catch (e) { log('exception');}\n"
152+
+ " } catch (e) { log(e.name);}\n"
152153

153154
+ " arr = new Uint8ClampedArray(11);\n"
154155
+ " try {\n"
155156
+ " var imageData = new ImageData(arr, 2);\n"
156-
+ " } catch (e) { log('exception');}\n"
157+
+ " } catch (e) { log(e.name);}\n"
157158

158159
+ " arr = new Uint8ClampedArray(8);\n"
159160
+ " try {\n"
160161
+ " var imageData = new ImageData(arr, 2, 2);\n"
161-
+ " } catch (e) { log('exception');}\n"
162+
+ " } catch (e) { log(e.name);}\n"
162163

163164
+ "}\n"
164165
+ "</script>\n"

0 commit comments

Comments
 (0)