question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onError handler doesn't work

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
crabblycommented, Apr 3, 2019

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.

1reaction
harpsicord86commented, Dec 12, 2018

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);

      // If showing a loading modal or using a hook function, we will preload the pdf file
      if (params.showModal || params.onLoadingStart) {
          // Get the file through a http request
          var req = new window.XMLHttpRequest();
          req.responseType = 'arraybuffer';

          req.addEventListener('load', function () {
              // Pass response data to a blob and create a local object url
              var localPdf = new window.Blob([req.response], { type: 'application/pdf' });
              localPdf = window.URL.createObjectURL(localPdf);

              // Pass the url to the printable parameter (replacing the original pdf file url)
              // This will prevent a second request to the file (server) once the iframe loads
              params.printable = localPdf;

              send(params, printFrame);
          });

          req.open('GET', params.printable, true);
          req.send();
      } else {
          send(params, printFrame);
      }
  } catch (e) {
      params.onError(e);
  }

} };`

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found