Skip to content

Commit d0af86c

Browse files
committed
Improve the "unsupported format" error message
The message was misleading and some users misunderstood it: JSZip doesn't reject a file because of what the content actually is (json file, mp4 file, etc). It rejects a file because it is not a supported JavaScript object (usually a plain object containing the buffer, not the buffer directly). The new message and the FAQ entry should help clear the misunderstanding.
1 parent a8e6831 commit d0af86c

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

documentation/faq.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,26 @@ decode the binary content as a text and corrupt it. See
1616
That happens if you try to handle to much data with the synchronous API. If
1717
possible, try the asynchronous API, see
1818
[this page]({{site.baseurl}}/documentation/limitations.html) for more informations.
19+
20+
### Can't read the data of [...]. Is it in a supported JavaScript type ?
21+
22+
Or the old message:
23+
24+
> The data of [...] is in an unsupported format
25+
26+
The method [`file(name, data [,options])`]({{site.baseurl}}/documentation/api_jszip/file_data.html)
27+
accepts string and binary inputs for `data`.
28+
29+
If you use an unsupported type, an object for example, you will get this error:
30+
31+
```js
32+
// WRONG
33+
var data = {
34+
content: new ArrayBuffer(...)
35+
};
36+
zip.file("my.data", data); // won't work, data is an object
37+
38+
// CORRECT
39+
var data = new ArrayBuffer(...);
40+
zip.file("my.data", data); // will work, JSZip accepts ArrayBuffer
41+
```

lib/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ exports.prepareContent = function(name, inputData, isBinary, isOptimizedBinarySt
454454

455455
if (!dataType) {
456456
return external.Promise.reject(
457-
new Error("The data of '" + name + "' is in an unsupported format !")
457+
new Error("Can't read the data of '" + name + "'. Is it " +
458+
"in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?")
458459
);
459460
}
460461
// special case : it's way easier to work with Uint8Array than with ArrayBuffer

test/asserts/file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ QUnit.module("file", function () {
345345
ok(false, "An unsupported object was added, but no exception thrown");
346346
}, function (e) {
347347
start();
348-
ok(e.message.match("unsupported format"), "the error message is useful");
348+
ok(e.message.match("Is it in a supported JavaScript type"), "the error message is useful");
349349
});
350350
});
351351

0 commit comments

Comments
 (0)