Tutorial 7: Implementing the JSAPI to catch exceptions with an error callback function
This tutorial provides step-by-step instructions for authoring a web page to catch exceptions and test them by generating a specific error to trigger the exception handler. Write an errorHandler function to catch exceptions in a web page.
1 Using a code editor, open or create a BasicViewer.html file that contains a basic implementation of the viewer.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type"
content="text/html;charset=utf-8" />
<title>Basic Viewer Example</title>
</head>
<body onload="init( )">
<table>
<tr><td class="jsapi_example_label">Report File:<td colspan="4">
<input type="text" size="80" id="reportfile" value=
"/Applications/BIRT Sample App/Customer Dashboard.rptdesign" name="Report_name">
<tr><td><input type="button" class="btn" id="run"
value="Run Viewer" onclick="run()" disabled="true">
</table>
 
<div id="content">
<script type="text/javascript" language="JavaScript" src="http://127.0.0.1:8700/iportal/jsapi"></script>
<script type="text/javascript" language="JavaScript">
var viewer;
function init( ) {
actuate.load("viewer");
actuate.initialize( "http://127.0.0.1:8700/iportal", null, null, null, initViewer);
}
function initViewer( ) {
viewer = new actuate.Viewer("content");
document.getElementById("run").disabled = false;
}
function run(){
viewer.setReportName(document.getElementById( "reportfile" ).value);
viewer.submit();
}
<!-- write an event handler to catch exceptions -->
</script>
</div>
</body>
</html>
For more information about implementing the JSAPI viewer, see Viewing reports.
2 Navigate to the following line:
<!-- write an event handler to catch exceptions -->
Replace the line with the following code:
function errorHandler(exception){
alert("Your application encountered an exception: \n" + exception.getMessage());
}
3 Navigate to the following line:
actuate.initialize( "http://127.0.0.1:8700/iportal", null, null, null, initViewer);
Add errorHandler to the parameter list as the optional exception handler for initialize, as shown in the following code:
actuate.initialize( "http://127.0.0.1:8700/iportal", null, null, null, initViewer, errorHandler);
4 Save the file as basicexception.html.
5 In Internet Explorer, open basicexception.html.
If you receive a security warning that Internet Explorer has restricted this page from running scripts or ActiveX controls that could access your computer, right-click on the message and select Allow Blocked Content.
6 Open basicexception.html. The errorHandler function runs because no login credentials have been provided, as shown in Figure 16‑3.
Figure 22-16 Chart showing a ToolTipFigure 22-16 Chart showing a ToolTip
Figure 16‑3 Authentication message from errorHandler