Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw better errors when there are parsing issues #49

Merged
merged 3 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions epub.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class EPub extends EventEmitter {
}).bind(this));

xmlparser.on("error", (function (err) {
this.emit("error", new Error("Parsing container XML failed"));
this.emit("error", new Error("Parsing container XML failed in getRootFiles " + err));
return;
}).bind(this));

Expand Down Expand Up @@ -258,7 +258,7 @@ class EPub extends EventEmitter {
xmlparser.on("end", this.parseRootFile.bind(this));

xmlparser.on("error", (function (err) {
this.emit("error", new Error("Parsing container XML failed"));
this.emit("error", new Error("Parsing container XML failed in handleRootFile: " + err));
return;
}).bind(this));

Expand Down Expand Up @@ -522,7 +522,7 @@ class EPub extends EventEmitter {
}).bind(this));

xmlparser.on("error", (function (err) {
this.emit("error", new Error("Parsing container XML failed"));
this.emit("error", new Error("Parsing container XML failed in TOC " + err));
return;
}).bind(this));

Expand Down
Binary file added example/alice_broken.epub
Binary file not shown.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"type": "git",
"url": "http://github.com/julien-c/epub.git"
},
"files": [
"epub.js",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are the only two files actually needed at runtime for the user

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

"epub.d.ts"
],
"main": "./epub",
"typings": "./epub.d.ts",
"licenses": [
Expand Down
25 changes: 23 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ mocha.describe('EPub', () => {
`/images/`
);
});

mocha.it('basic parsing', () => {
const epub = new EPub('./example/alice.epub');

epub.on('end', ()=> {
assert.ok(epub.metadata.title)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added some basic coverage

assert.equal(epub.metadata.title, "Alice's Adventures in Wonderland")
})

epub.parse();

assert.strictEqual(
epub.imageroot,
`/images/`
Expand All @@ -27,4 +33,19 @@ mocha.describe('EPub', () => {
var res = epub.walkNavMap(branch, [], []);
assert.ok(res);
});

mocha.it('raises descriptive errors', () => {
// const epub = new EPub('./example/alice.epub')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's weird is that if this line is uncommented instead of the below, the tests still pass when they shouldn't 🤔 not sure if I'm parsing it wrong or what

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, mocha doesn't wait for callbacks to be executed, does it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move to async/await... :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, you're right. I haven't written mocha tests w/ done for a while.

But you're also right that we should use promises, so I added a tool for awaiting events and now it works like a charm!

const epub = new EPub('./example/alice_broken.epub')

epub.on('error', (err) => {
assert.ok(err.message.includes('Error: Parsing container XML failed in TOC Error: Invalid character in entity name'))
})

epub.on('end', () => {
assert.fail('should not have gotten here')
})

epub.parse()
})
});