onError handler doesn't work
See original GitHub issueI’m trying to print a PDF from the server. If a server error occurs, I want to display this error in the UI, for this, I try to use the onError
handler, but it does not work, my function is not called.
printJS({
printable: "docs/myFile.pdf",
type: 'pdf',
showModal: true,
onError: err => console.log("Erorr") // it doesn't work
});
What am I doing wrong?
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (7 by maintainers)
Top Results From Across the Web
window.onerror does not work - Stack Overflow
I have some tricky AJAX code on a form, and sometimes it will fail (don't ask why, I can't get around it). When...
Read more >Window: error event - Web APIs - MDN Web Docs - Mozilla
Note: Due to historical reasons, onerror on window is the only event handler property that receives more than one argument.
Read more >Working with window.onerror - Errorception
In the DOM Level 1 event model that window.onerror uses, there can only be one event handler assigned at a time. That's because...
Read more >onerror Event - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP,...
Read more >JavaScript Error Handling - JSNLog
To override JSNLog's handler, simply set window.onerror to your own handler. If your code runs before jsnlog.js loads, jsnlog.js will see that window.onerror...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey guys, this change is now available with the latest version. Run
npm update print-js
to get it.Let me know of any issues. Thank you.
Hi @crabbly @yaroslav-perec
I may have a fix for you as I’ve had the same issue. Basically in pdf.js if you check that the specified URL exists first and encapsulate the rest of the method in a try clause, you can then set params.onError to trigger. Here is some code:
function UrlExists(url) { var http = new XMLHttpRequest(); http.open('HEAD', url, false); http.send(); if (!(http.status >= 200 && http.status < 300 || http.status === 304)) { throw new Error(http.statusText); } }
`exports.default = { print: function print(params, printFrame) { // Format pdf url params.printable = /^(blob|http)/i.test(params.printable) ? params.printable : window.location.origin + (params.printable.charAt(0) !== ‘/’ ? ‘/’ + params.printable : params.printable); try { UrlExists(params.printable);
} };`
Also, I found an issue with the modal appearing after the error message appears (this is at the point where the script checks for the printable type) - this also needs to be encapsulated in a try clause:
// Check printable type switch (params.type) { case 'pdf': // Check browser support for pdf and if not supported we will just open the pdf file instead if (_browser2.default.isFirefox() || _browser2.default.isEdge() || _browser2.default.isIE()) { try { console.info('PrintJS currently doesn\'t support PDF printing in Firefox, Internet Explorer and Edge.'); var win = window.open(params.fallbackPrintable, '_blank'); win.focus(); if (params.onPdfOpen) params.onPdfOpen(); } catch (e) { params.onError(e); } finally { // Make sure there is no loading modal opened if (params.showModal) _modal2.default.close(); if (params.onLoadingEnd) params.onLoadingEnd(); } } else { try { _pdf2.default.print(params, printFrame); } catch (e) { params.onError(e); } finally { // Make sure there is no loading modal opened if (params.showModal) _modal2.default.close(); if (params.onLoadingEnd) params.onLoadingEnd(); } } break; case 'image': _image2.default.print(params, printFrame); break; case 'html': _html2.default.print(params, printFrame); break; case 'json': _json2.default.print(params, printFrame); break; }
I hope this helps with your next version and to anyone else wanting to solve this issue!
Cheers