Skip to content

Commit 440ddd3

Browse files
committed
Improve the error without type in async()
Without any type given to `async()`, an error is thrown with a message a bit useless. I re-used the check of `generateAsync()`: without type, the result is promise failed with an error "No output type specified".
1 parent a4c7e04 commit 440ddd3

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

lib/zipObject.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,29 @@ ZipObject.prototype = {
3737
* @return StreamHelper the stream.
3838
*/
3939
internalStream: function (type) {
40-
var outputType = type.toLowerCase();
41-
var askUnicodeString = outputType === "string" || outputType === "text";
42-
if (outputType === "binarystring" || outputType === "text") {
43-
outputType = "string";
44-
}
45-
var result = this._decompressWorker();
40+
var result = null, outputType = "string";
41+
try {
42+
if (!type) {
43+
throw new Error("No output type specified.");
44+
}
45+
outputType = type.toLowerCase();
46+
var askUnicodeString = outputType === "string" || outputType === "text";
47+
if (outputType === "binarystring" || outputType === "text") {
48+
outputType = "string";
49+
}
50+
result = this._decompressWorker();
4651

47-
var isUnicodeString = !this._dataBinary;
52+
var isUnicodeString = !this._dataBinary;
4853

49-
if (isUnicodeString && !askUnicodeString) {
50-
result = result.pipe(new utf8.Utf8EncodeWorker());
51-
}
52-
if (!isUnicodeString && askUnicodeString) {
53-
result = result.pipe(new utf8.Utf8DecodeWorker());
54+
if (isUnicodeString && !askUnicodeString) {
55+
result = result.pipe(new utf8.Utf8EncodeWorker());
56+
}
57+
if (!isUnicodeString && askUnicodeString) {
58+
result = result.pipe(new utf8.Utf8DecodeWorker());
59+
}
60+
} catch (e) {
61+
result = new GenericWorker("error");
62+
result.error(e);
5463
}
5564

5665
return new StreamHelper(result, outputType, "");

test/asserts/file.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ QUnit.module("file", function () {
141141
_actualTestFileDataGetters.testGetter(opts, "nodebuffer");
142142
_actualTestFileDataGetters.testGetter(opts, "blob");
143143
_actualTestFileDataGetters.testGetter(opts, "unknown");
144+
_actualTestFileDataGetters.testGetter(opts, null);
144145

145146
stop();
146147
opts.zip.generateAsync({type:"binarystring"})
@@ -162,6 +163,7 @@ QUnit.module("file", function () {
162163
_actualTestFileDataGetters.testGetter(reloaded, "nodebuffer");
163164
_actualTestFileDataGetters.testGetter(reloaded, "blob");
164165
_actualTestFileDataGetters.testGetter(reloaded, "unknown");
166+
_actualTestFileDataGetters.testGetter(reloaded, null);
165167

166168
opts.zip.file("file.txt", "changing the content after the call won't change the result");
167169
start();
@@ -253,6 +255,10 @@ QUnit.module("file", function () {
253255
assert_unknown : function (opts, err, buffer, testName) {
254256
equal(buffer, null, testName + "no data");
255257
ok(err.message.match("not supported by this platform"), testName + "the error message is useful");
258+
},
259+
assert_null : function (opts, err, buffer, testName) {
260+
equal(buffer, null, testName + "no data");
261+
ok(err.message.match("No output type specified"), testName + "the error message is useful");
256262
}
257263
};
258264

0 commit comments

Comments
 (0)