Error handling from javascript
-
Hi,
I have a report which gets its data from a webservice.
I use the beforeRender to get da data. Something like this:
function beforeRender(req, res, done) { require('request')({ url: 'https://xyz', json: true, encoding: 'utf8', method: 'POST', headers: { 'Authorization': 'Basic xyz', 'Accept': 'application/json' }, body: { "params": [{ "name": "idin", "param_type": "IN", "value": req.data.idin }] } }, function(err, response, body) { req.data.rows = body; done(); }); }
What's the proper way for error handling?
Let's say, there is a password error and the webservice returns 403 access forbidden. What is best way to send this error to jsreport, so jsreport returns also some kind of error if the report gets called from the jsreport API?
Thanks for a jumpstart on this.
Reto E.
-
Try something like this. You likely need to check also status code but I believe you get the idea.
}, function(err, response, body) { if (err) { return done(err) } req.data.rows = body; done(); });
-
Thx for your reply.
How can I process this error message in jsreport studio?
If done(err) is returned, I want jsreport to fail the report generation and display the error message. The API request of the report should respond with a 500 server error or something like this.
-
If done(err) is returned, I want jsreport to fail the report generation and display the error message. The API request of the report should respond with a 500 server error or something like this.
This is exactly what will happen.