How to throw custom error message from jsreport



  • Hi sir I want to throw custom error message from jsreport like this,

    {
    "Error message":"you don't have permission to print template",
    StatusCode:400
    }

    How to throw this error object from jsreport??



  • You can add a script like this

    function beforeRender(req, res) {
        throw new Error('you don't have permission to print template')
    }
    
    {
        "message": "Error when evaluating custom script /scripts/failhard\nyou don't have permission to print template\n\n(sandbox.js line 2:11)\n\n  1 | function beforeRender(req, res) {\n> 2 |     throw new Error('Unable to print')\n    |           ^\n  3 | }\n\n(sandbox.js line 15:18)",
        "stack": "Error: Unable to print\n    at Object.beforeRender (sandbox.js:2:11)\n    at executionFn (/app/node_modules/@jsreport/jsreport-scripts/lib/executeScript.js:67:35)\n    at sandbox.js:15:18"
    }
    


  • I have added try catch in before render ie,
    try{

    } catch (error){
    done(error)
    }

    So when ever there is any error encountered inside before render and it's sub functions catch block works and using web socket api we pass the error message to the client.

    Here after render won't works.but if error encountered in html then before render catch block won't works so how can I acknowledge to the client that there is an error in report?

    We are running report asynchronous way so when we render a report using jsreport.render function of jsreport - browser client package we instantaneously get a url. Till now what we are doing is that , using that url we get the status of report by using timer function which runs every one second.but i think it is wrong approach because we are calling lots of get method just for getting the status of report and it makes application becomes slow can you help me how to handle error for asynchronous rendering???



  • Unfortunately, there is no way to listen to errors from inside jsreport script. You can upvote the task here
    https://github.com/jsreport/jsreport/issues/966

    What you can do is to add code to your jsreport app. Open the server.js and change it to the following.
    This should let you handle the errors and act.

    const jsreport = require('jsreport')()
    
    if (process.env.JSREPORT_CLI) {
      // export jsreport instance to make it possible to use jsreport-cli
      module.exports = jsreport
    } else {
      jsreport.init().then(() => {
        jsreport.renderErrorListeners.add('my handler', async (req, res, err) => {
          // do whatever
        })
        // running
      }).catch((e) => {
        // error during startup
        console.error(e.stack)
        process.exit(1)
      })
    }
    


  • Sorry where I need to add this code.i want to throw from template and if error encountered i want to push error message to socket url.

    Also I'm deploying jsreport to AWS via docker and there is only jsreport configuration exist in docker project.you mean i need to create server.js file in that docker project?? Can you please explain in detail??



  • Yes, the jsreport app includes the server.js file in the app root which you need to modify if you want to add a custom error handler.

    Try to install jsreport as plain nodejs app locally and test the changes in the server.js
    https://jsreport.net/on-prem

    Then you will need to create your own custom image with jsreport app which you then deploy to the AWS.
    You can find some hints in jsreport docs
    https://jsreport.net/learn/docker
    However, building custom docker images is a common task and you should find plenty of resources online for it.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.