Want an exception upon BIRT JavaScript errors - javascript

I have a report in BIRT that has non-trivial JavaScript (scripted data source). The JavaScript is all a bit wobbly, and suspect to regress. For that reason and others I have written a JUnit test that populates the data, runs the report (createRunAndRenderTask and run that task) and do some validation on the resulting report.
Obviously this test will fail when the BIRT engine throws any exceptions. However, upon JavaScript errors in the report, no exceptions are thrown. And that does not feel good. Can I change this somehow to have the BIRT engine throw exceptions upon JavaScript errors?
I tried this by having a host of JavaScript errors during development of the report. Think of typos in the scripted data source. They are spit out in the console, but no exceptions.
E.g.:
<method name="open"><![CDATA[count = 0;
this should break]]></method>
This shows in the console:
... Fail to execute script in function __bm_OPEN(). Source:
------
" + count = 0;
this should break + "
-----
A BIRT exception occurred. See next exception for more information.
ReferenceError: "this should break" is not defined. (/report/data-sets/script-data-set[#id="9"]/method[#name="open"]#3)
Thank you for your suggestions!

I ended up doing this and pretty okay with it:
IRunAndRenderTask task = ...
...
task.setErrorHandlingOption(IEngineTask.CANCEL_ON_ERROR);
...
task.run();
evaluateStatus(task, reportName);
task.close();
And:
private void evaluateStatus(IRunAndRenderTask task, String reportName) {
if (task.getStatus() == IEngineTask.STATUS_CANCELLED) {
String message = "report failed: " + reportName;
List<Throwable> errors = task.getErrors();
if (!errors.isEmpty()) {
throw new RuntimeException(message, errors.get(errors.size() - 1));
}
throw new RuntimeException(message);
}
}

Depending on javascript errors, the BIRT engine will catch them and still try to display the report.
I think you could override this by wrapping your javascript code (Rhino script) in a try...catch expression, and explicitely throw a BirtException if something wrong happens:
try{
//your javascript stuff
var test=null;
test.toString();
}catch(e){
var exception=new org.eclipse.birt.core.exception.BirtException("Custom exception:"+e);
throw exception;
}

Related

"Script error" reported in window.onerror without cross-site scripting

Almost every JS error I get using window.onerror is the unhelpful "Script error" message.
I have read everything I can find on this but it is always said it is caused by cross-site scripting, but there's none in my code.
Why do these unhelpful error messages appear and how can I replace them with the full messages that appear in the console?
As a test example, I created a minimal HTML/JS code pair. This is the HTML page, testpage.html:
<html>
<head></head>
<body>
<script src="/js/testpage.min.js"></script>
</body>
</html>
and JS script:
window.onerror = function(msg, url, lineNo, columnNo, error) {
// code goes here to generate log entry on the server
return true;
}
// generate illustrative sample error
var jsonData = "";
var data = JSON.parse(jsonData);
Running this, msg = "Script error" and other parameters are either null or zero. The Google DevTools console displays the full error:
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at testpage.min.js:7
Any help towards being able to log the full error messages will be most welcome. I'm at a complete loss.
EDIT: Recast to make problem clearer.

Catch window.open() error

I open a file download from a remote API on my webpage via window.open(). The API (a Flask server) has error handling and returns the error message if there's an internal server error, like this:
#app.errorhandler(502) //all other errors are handled the same way, including 500, etc.
#crossdomain(origin='*')
def bad_gateway_error(error):
return "Bad Gateway Error - Please make sure you're using a properly formatted file! Details: " + str(error), 200
I want to display this error on my site instead of redirecting to the error page. I'm trying to catch it via:
try {
window.open("https://API/receivedoc?timestamp="+timestamp,"_self")
} catch(e) {
filerootdiv.querySelector('.output').innerHTML = String(e);
}
This however does nothing (tested in Chrome). How could I catch the error when I'm using window.open? I guess it might be because in the error handling I return a 200 message so that the string I return actually gets returned instead of just crashing the server (this needs to stay this way as it's working just fine with all the other errors when I'm not trying to return a file). The issue is that I can't tell if the API request would return a file or a string before doing a window.open().
UPDATE
I've tried implementing:
let new_window = window.open("https://flaskmin.run.aws-usw02-pr.ice.predix.io/receivedoc?timestamp="+timestamp,"_self")
newWindow.onerror = function() {
filerootdiv.querySelector('.output').innerHTML = "Error!";
However this still only opens a new window with the error. I guess it's because of the error handling on the server side (I cannot change this). Can I somehow probe the content of new_window before redirecting to it, and just not open it if it's just a string containing the word 'error'?

Selenium-C#: SendKeys timing out & JS Executor throwing error

Trying to paste the data(which is quiet big) in the "Textarea1" control, below are the two methods I tried using, but first method tries pasting the data but throws timeout error sometime, and second one throws JS error. please help
public StringBuilder PasteDataIn_Tarea1
{
set
{
//Method1
Textarea1.Clear();
Textarea1.SendKeys(value.ToString());
//Method2
IWebDriver driver;
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
StringBuilder javascript = new StringBuilder();
javascript.Append(string.Format("$('#Textarea1').text('{0}')", value));//value has my data which is quiet big, "Textarea1" is where I need to paste my data
js.ExecuteScript(javascript.ToString()); // Js exector which should paste my data
}
}
Method2 throws below exception:
An exception of type 'System.InvalidOperationException' occurred in
WebDriver.dll but was not handled in user code
Additional information: JavaScript error (UnexpectedJavaScriptError)
I prefer going to Method2 as the first one takes time to paste the data in the textarea.
Try resetting the browser timeout:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--disable-extensions");
ChromeDriverService svc = ChromeDriverService.CreateDefaultService();
IWebDriver driver = new ChromeDriver(svc, options,TimeSpan.FromMinutes(5));
The example above takes the command timeout to 5 minutes. I had a similar problem and this is what worked for me.

error params are null in window.onerror in google chrome

I have html page which causes js error and an global handler for it.
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
<script type="text/javascript">
$(function () {
window.onerror = function (errorMsg, url, lineNumber) {
alert('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber);
}
throw new Error("this is error");
});
</script>
</head>
<body>
<div>Test page</div>
</body>
</html>
I receive alert: "Error: Script error. Script: Line: 0". As you can see error info is missing. How can I get this info here like I can see errors in developer tools. Any extensions for chrome are not suitable. I must catch errors with their info in javascript.
When a syntax(?) error occurs in a script, loaded from a different origin, the details of the syntax error are not reported to prevent leaking information (see bug 363897). Instead the error reported is simply "Script error." This behavior can be overriden in some browsers using the crossorigin attribute on and having the server send the appropriate CORS HTTP response headers. A workaround is to isolate "Script error." and handle it knowing that the error detail is only viewable in the browser console and not accessible via JavaScript.
I also ran into this. See notes section here: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
This implies the development setup might need some adjustment to avoid this policy issue. I'm merely guessing here, so don't take it for granted.
I am just adding answer for the sake of those who came here looking.
Source: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
Please go this URL, as you can see there are five parameters to
window.onerror = function(message, source, lineno, colno, error) { ... }
the last one is the one you need, it has complete record of what you see in console in case of error.
message: error message (string). Available as event (sic!) in HTML onerror="" handler.
source: URL of the script where the error was raised (string)
lineno: Line number where error was raised (number)
colno: Column number for the line where the error occurred (number)
error: Error Object (object)
Then Error Object has two properties
message
stack
stack is the one which will contain all the information you required.
use console.log(errorMsg, url, lineNumber)instead of alert to get more readable log. You can see the log in browser developer console. Also you can check the use of try..catch for better error handling.
use jquery script with anonymous attirbute, and the script server response header with [access-control-allow-origin:*]. Your error throw code is runned with jquery callback.
errorMsg is NOT the message "this is error"; it is Script error., which is precisely what you're seeing. You simply don't get the error message that way. onerror is unsuited to replace a try-catch block.

Microsoft JScript runtime error: Object expected Exception

I am getting below exception in runtime, could anybody suggest what is wrong?
Microsoft JScript runtime error: Object expected
Exception is coming in very first line:
aspxAddHoverItems('tabMaster',[[['dxtcTabHover_Glass'],[''],['T0','T1','T2','T3'],['','T'],[[''],[''],[''],['']],['Img']],[['dxtcActiveTabHover_Glass'],[''],['AT0','AT1','AT2','AT3'],['','T'],[[''],[''],[''],['']],['Img']]]);
var dxo = new ASPxClientTabControl('tabMaster');
window['tabMaster'] = dxo;
dxo.uniqueID = 'ctl00$tabMaster';
dxo.RegisterServerEventAssigned(['TabClick']);
dxo.emptyHeight = true;
dxo.emptyWidth = true;
dxo.tabCount=4;
dxo.InlineInitialize();
aspxAddHoverItems is simply unrecognized as the error suggests. Be sure to include all necessary references and assemblies in your project.
This may be just what you need: How to: Manually Register DevExpress Extensions to Start Using Them in an MVC Web Application
Some additional reading: Google Search: aspxAddHoverItems is not defined

Categories