Forum How do I...?

Throwing Javascript Errors to Halt PDF Generation

gtolenti
Hi,

When generating PDFs with prince I have an xhtml that fires off some javascript to dynamically create parts of the document. Is there a way to halt PDF generation through javascript when an error occurs? I've tried throwing errors in the script: throw new Error("An error has occurred"), but the document still generates.

Here is what gets printed out to the console:
prince: http://localhost:63342/reporting-services-master/reporting-services-authoring/injector.js: error: Error: UNABLE_TO_RENDER_VISUAL_COMPONENT_ERROR, unable to render visual component with id: kf.enterprise.radarChart

I am using prince 10rev7 on MacOSX for local dev but deploy to a linux environment on production.

Thanks
mikeday
Actually there is no way to halt document conversion from JavaScript. Now that you mention it, we should probably consider adding an API for this purpose. For now, you can emit a log message that gets detected by the caller, to tell it to ignore the generated PDF.
gtolenti
Thanks for the reply. For now I'm just looking for a particular string in stderr and then throwing an exception at the application level.
lynxSven
Hi,

It has been 6 years since this post.
Is the solution which Mike posted still the goto solution to handle errrors by the javascript and cancel the conversion with a error?

Thanks.
mikeday
Now you can use the new fail-safe option:
Prince.failStatus = true;

This will cause PDF generation to fail:
prince: error: not generating PDF due to fail-safe option

Some fail-safe errors can also be handled without JavaScript using these options:
  --fail-dropped-content      Fail if any content is dropped.
  --fail-missing-resources    Fail if any resources cannot be loaded.
  --fail-missing-glyphs       Fail if glyphs cannot be found for any characters.
lynxSven
Thanks mike,
Works good for me sofar.

Just one clarification. Can I hand the JS error message to the Prince console?
The message
prince: error: not generating PDF due to fail-safe option 
could have many causes.

Is there a way to handover the error message?
Like let's say:
...
catch(error){

Prince.failStatus= true;
Prince.failmessage=error
}
//or

Prince.fail(true,failmessage)
mikeday
You can call Log.warning(msg) or console.warn(msg) before setting the failure status.
lynxSven
We have a Restservices which generates a PDF and returns it.
If the Prince.FailStatus is true the rest service responses with "not generating PDF due to fail-safe option"

The Restservice can't see the logs on the JS-console. It would be better if we could edit the generic "not generating PDF due to fail-safe option" message wich is called by the client.
Would make Errorhandling and investigating why an error occurred so much easier

Edited by lynxSven

mikeday
How does it see the fail-safe error message if it cannot see the logs?
lynxSven
Prince starts with the Failsafe option. The Java wrapper knows that Prince failed the conversion. He returns a 500 with the message to the calling client "not generating PDF due to fail-safe option"
Then a ticket pops up and states there was an conversion error with this as a response.
Then I have to look through all the calls and on wich machine it occured and than I have to search for the error message.
It takes a lot of time for me and my co-workers.

When I could edit the failsafe message, the ticket creator could say: "hey the service crashed with errormessage xy" and I would not have to dig through lots of logs on several machines.
mikeday
The Java wrapper can see the Prince output log messages so perhaps it could remember the last JavaScript log message and send that with the 500 error? (Or send the entire output log, if that's more convenient).