Commit 0265b75f authored by Robert Knight's avatar Robert Knight

Fail loudly if VitalSource page info fields are missing

If for any reason VitalSource's `<mosaic-book>` API does not return the page
info fields that the Hypothesis client is expecting, fail loudly so we find out
about the issue, rather than silently creating annotations with
missing/incorrect chapter location information.

I don't currently know of any situation where this is expected to happen, so
this is protection against future changes or differences in behaviors with
certain books that we have not observed yet.
parent 38e4d947
...@@ -405,6 +405,34 @@ describe('annotator/integrations/vitalsource', () => { ...@@ -405,6 +405,34 @@ describe('annotator/integrations/vitalsource', () => {
}); });
}); });
['absoluteURL', 'cfi', 'index', 'page'].forEach(field => {
it(`throws if page info field "${field}" is missing`, async () => {
fakeBookElement.selectPDFBook();
const pageInfo = { ...(await fakeBookElement.getCurrentPage()) };
delete pageInfo[field];
fakeBookElement.getCurrentPage = async () => pageInfo;
const integration = createIntegration();
integration.contentContainer();
assert.calledWith(fakeHTMLIntegration.contentContainer);
const range = new Range();
let error;
try {
await integration.describe(range);
} catch (err) {
error = err;
}
assert.instanceOf(error, Error);
assert.equal(
error.message,
`Chapter metadata field "${field}" is missing`
);
});
});
describe('#getMetadata', () => { describe('#getMetadata', () => {
it('returns book metadata', async () => { it('returns book metadata', async () => {
const integration = createIntegration(); const integration = createIntegration();
......
...@@ -430,6 +430,18 @@ export class VitalSourceContentIntegration ...@@ -430,6 +430,18 @@ export class VitalSourceContentIntegration
includeTitle ? this._bookElement.getTOC() : undefined, includeTitle ? this._bookElement.getTOC() : undefined,
]); ]);
// If changes in VitalSource ever mean that critical chapter/page metadata
// fields are missing, fail loudly. Otherwise we might create annotations
// that cannot be re-anchored in future.
const expectedFields = ['absoluteURL', 'cfi', 'index', 'page'];
for (const field of expectedFields) {
// nb. We intentionally allow properties anywhere on the prototype chain,
// rather than requiring `hasOwnProperty`.
if (!(field in pageInfo)) {
throw new Error(`Chapter metadata field "${field}" is missing`);
}
}
let title; let title;
if (toc) { if (toc) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment