Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added exception handling to Session and Result examples.

  • Loading branch information...
commit 59e84caecded36b3f1062fc1d4bc8583bdf21b2e 1 parent 68910c9
@mheadd mheadd authored
Showing with 60 additions and 43 deletions.
  1. +35 −27 samples/use-result.js
  2. +25 −16 samples/use-session.js
View
62 samples/use-result.js
@@ -20,33 +20,41 @@ var server = http.createServer(function (request, response) {
// Add a listener for the EOF event on the incoming stream.
request.addListener('end', function() {
- // Create a new instance of the TropoWebAPI object.
- var tropo = new TropoWebAPI();
-
- // The path for the first step in the application flow (ask the caller for input).
- if(pathname == '/') {
-
- var say = new Say("Please enter a number one through 5.");
- var choices = new Choices("1,2,3,4,5");
- tropo.ask(choices, 3, false, null, "foo", null, true, say, 5, null);
- tropo.on("continue", null, '/selection', true);
- response.writeHead(200, {'Content-Type': 'application/json'});
- response.end(TropoJSON(tropo));
-
- }
-
- // The second step in the application flow - input is submitted via Result JSON delivered from Tropo.
- if(pathname == '/selection') {
-
- // Create a new instance of the Result object and give it the JSON delivered from Tropo.
- var result = Result(json);
- tropo.say("Your selection was, " + result.interpretation + ". Goodbye.");
- tropo.hangup();
- response.writeHead(200, {'Content-Type': 'application/json'});
- response.end(TropoJSON(tropo));
-
- }
-
+ try {
+
+ // Create a new instance of the TropoWebAPI object.
+ var tropo = new TropoWebAPI();
+
+ // The path for the first step in the application flow (ask the caller for input).
+ if(pathname == '/') {
+
+ var say = new Say("Please enter a number one through 5.");
+ var choices = new Choices("1,2,3,4,5");
+ tropo.ask(choices, 3, false, null, "foo", null, true, say, 5, null);
+ tropo.on("continue", null, '/selection', true);
+ response.writeHead(200, {'Content-Type': 'application/json'});
+ response.end(TropoJSON(tropo));
+
+ }
+
+ // The second step in the application flow - input is submitted via Result JSON delivered from Tropo.
+ if(pathname == '/selection') {
+
+ // Create a new instance of the Result object and give it the JSON delivered from Tropo.
+ var result = Result(json);
+ tropo.say("Your selection was, " + result.interpretation + ". Goodbye.");
+ tropo.hangup();
+ response.writeHead(200, {'Content-Type': 'application/json'});
+ response.end(TropoJSON(tropo));
+
+ }
+ }
+
+ catch(e) {
+ response.writeHead(501, {'Content-Type': "text/plain"});
+ //sys.log(e.message);
+ response.end(e.message + "");
+ }
})
}).listen(8000); // Listen on port 8000 for requests.
View
41 samples/use-session.js
@@ -3,12 +3,12 @@
* the Tropo Session object.
*/
-
+var sys = require('sys');
var http = require('http');
var tropo = require('../lib/tropo-webapi');
var server = http.createServer(function (request, response) {
-
+
// Add a listener for the data event (incoming data from the HTTP request)
request.addListener('data', function(data){
json = data.toString();
@@ -16,23 +16,32 @@ var server = http.createServer(function (request, response) {
// Add a listener for the EOF event on the incoming stream.
request.addListener('end', function() {
-
- // Create a new instance of the Session object and give it the JSON delivered from Tropo.
- var session = Session(json);
- var accountId = session.accountId;
- // Create a new instance of the TropoWebAPI object.
- var tropo = new TropoWebAPI();
+ try {
- // Build the response with the userType property of the session object.
- tropo.say("Your user type is, " + session.userType);
- tropo.say('Goodbye.');
- tropo.hangup();
+ // Create a new instance of the Session object and give it the JSON delivered from Tropo.
+ var session = Session(json);
+
+ // Create a new instance of the TropoWebAPI object.
+ var tropo = new TropoWebAPI();
+
+ // Build the response with the userType property of the session object.
+ tropo.say("Your user type is, " + session.userType);
+ tropo.say('Goodbye.');
+ tropo.hangup();
+
+ // Render out the JSON for Tropo to consume.
+ response.writeHead(200, {'Content-Type': 'application/json'});
+ response.end(TropoJSON(tropo));
+
+ }
+
+ catch(e) {
+ response.writeHead(501, {'Content-Type': "text/plain"});
+ //sys.log(e.message);
+ response.end(e.message + "");
+ }
- // Render out the JSON for Tropo to consume.
- response.writeHead(200, {'Content-Type': 'application/json'});
- response.end(TropoJSON(tropo));
-
})
}).listen(8000); // Listen on port 8000 for requests.
Please sign in to comment.
Something went wrong with that request. Please try again.