Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
fixed: if a PDF endpoint encounters an authentication error during PD…
Browse files Browse the repository at this point in the history
…F generation it does not provide an error response, https://github.com/OpenClinica/enketo-express-oc/issues/688
  • Loading branch information
MartijnR committed Jul 25, 2023
1 parent 8ca51a5 commit 181346d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion app/lib/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,27 @@ async function get(
let pdf;

try {
await page
// To use an eventhandler here and catch a specific error,
// we have to return a Promise (in this case one that never resolves).
const detect401 = new Promise((resolve, reject) => {
page.on('requestfinished', (request) => {
if (request.response().status() === 401) {
const e = new Error('Authentication required');
e.status = 401;
reject(e);
}
});
});
const goToPage = page
.goto(urlObj.href, { waitUntil: 'networkidle0', timeout })
.catch((e) => {
e.status = /timeout/i.test(e.message) ? 408 : 400;
throw e;
});

// Either a 401 error is thrown or goto succeeds (or encounters a real loading error)
await Promise.race([detect401, goToPage]);

/*
* This works around an issue with puppeteer not printing canvas
* images that were loaded from a file.
Expand Down

0 comments on commit 181346d

Please sign in to comment.