Skip to content

Instantly share code, notes, and snippets.

@egomez99
Created March 22, 2013 18:11
Show Gist options
  • Save egomez99/5223484 to your computer and use it in GitHub Desktop.
Save egomez99/5223484 to your computer and use it in GitHub Desktop.
App sample: Requires Crittercism module (v1.0.1) it was made to test try{}catch{} blocks and handledExceptions can report what are file name and line of code to narrow down an app crash e.g. crittercism.logHandledException(err); It has leaveBreadCrumb to follow the app flow taken prior getting the crash, e.g. crittercism.leaveBreadcrumb("it fail…
// An Example app to show usage of the Crittercism APIs
// this does not currently contain best practices for
// Crittercism in conjunction with Titanium
//
// Updated 02-13-2013
Ti.API.info("Ti App: importing Crittercism...");
var crittercism = require('com.crittercism.ti');
Ti.API.info("module is => " + crittercism + "\n");
var didCrash;
if (Titanium.Platform.osname == 'android')
{
Ti.API.info("Ti App: initializing Crittercism Android...");
didCrash = crittercism.init("514b7c1f8b2e33515a000002");
}
else if (Titanium.Platform.osname == 'iphone')
{
Ti.API.info("Ti App: initializing Crittercism iOS...");
crittercism.init("5140f181558d6a1d5000002c");
didCrash = crittercism.didCrashOnLastAppLoad();
}
Ti.API.info("Ti App: Crittercism initialized\n");
var crashed = (didCrash)?"Yes":"No";
Ti.API.info("Ti App: didCrashOnLastLoad: " + crashed + "\n");
Ti.API.info("Ti App: Crittercism UUID: " + crittercism.getUUID() + "\n");
// Android Only
Ti.API.info("Ti App: Crittercism Notification Title: " + crittercism.getNotificationTitle() + "\n");
var win = Titanium.UI.createWindow({
title : 'Crittercism Test',
backgroundColor : '#fff'
});
crittercism.leaveBreadcrumb("Creating window");
var topLabel = Titanium.UI.createLabel({
top:10,
color : '#999',
text : 'Crittercism Test App!',
font : {
fontSize : 16,
fontFamily : 'Helvetica Neue'
},
width : 'auto'
});
win.add(topLabel);
var setMetaData = Titanium.UI.createButton({
top:35,
width:301,
height:30,
title:'Set Metadata'
});
setMetaData.addEventListener('click', function()
{
Ti.API.info("setMetaData // Setting Username");
crittercism.leaveBreadcrumb("setMetaData // Setting Username");
crittercism.setUsername("TheCritter");
Ti.API.info("setMetaData // Setting Email");
crittercism.leaveBreadcrumb("setMetaData // Setting Email");
crittercism.setEmail("support@crittercism.com");
Ti.API.info("setMetaData // Setting Arbitrary Single Set Metadata");
crittercism.setMetadata("gameLevel", 6);
crittercism.setMetadata("playerID", 9491824);
});
win.add(setMetaData);
var crashButton = Titanium.UI.createButton({
top:70,
width:301,
height:30,
title:'Crash it on app.js'
});
crashButton.addEventListener('click', function()
{
crittercism.leaveBreadcrumb("Clicking the crash button");
doSomething();
});
win.add(crashButton);
var openModalWindowButton = Titanium.UI.createButton({
top:300,
width:301,
height:30,
title:'Open Modal Window'
});
Ti.include("myModalWindow.js");
openModalWindowButton.addEventListener('click', function(){
try {
crittercism.leaveBreadcrumb("Attempting To OPEN myModalWindow.js...");
UI.openModalWindow().open();
} catch (err){
crittercism.leaveBreadcrumb("Failed to open myModalWindow.js");
crittercism.logHandledException(err);
}
});
win.add(openModalWindowButton);
var handledButton = Titanium.UI.createButton({
top:105,
width:301,
height:30,
title:'Send Handled Exception'
});
handledButton.addEventListener('click', function()
{
try {
crittercism.leaveBreadcrumb("Attempting some awesome task...");
doSomething();
} catch (err){
crittercism.leaveBreadcrumb("Oh no, it failed! Log it...");
crittercism.logHandledException(err);
}
});
win.add(handledButton);
var doSomething = function doSomething () {
crittercism.leaveBreadcrumb("doSomething // Entered");
foo();
}
function foo () {
crittercism.leaveBreadcrumb("Foo // Entered");
bar();
}
function bar () {
crittercism.leaveBreadcrumb("Bar // Entered");
something();
}
var something = function() {
crittercism.leaveBreadcrumb("Something // Entered");
// create an array with an invalid size
//Test case 1: Handled exception is reported with offending line of code
var a = new Array(0x100000000);
var array = new Array();
win.add(array[0]); // this gets caught because the object is undefined and not a proxy
// throw a custom exception
var er = new Error("My Awesome Uncaught Error!");
throw er;
/*Handled report (iOS only):
Supported orientations has no common orientation with the application,
and shouldAutorotate is returning YES at app.js (line 173)
var winTest = Ti.UI.createWindow({
backgroundColor : 'red',
orientationModes : [Titanium.UI.UPSIDE_PORTRAIT],
modal : true
});
var label = Ti.UI.createLabel({
text : 'hello'
});
winTest.add(label);
winTest.open();
*/
}
var status = false;
var optOutToggle = Titanium.UI.createButton({
top:140,
width:301,
height:30,
title:'Toggle OptOut Status: No'
});
optOutToggle.addEventListener('click', function()
{
// Set the status
crittercism.setOptOutStatus(status = !status);
// change the status in the button title for visibility
var stringStatus = crittercism.getOptOutStatus() ? "Yes" : "No";
optOutToggle.title = 'Toggle OptOut Status: ' + stringStatus;
});
win.add(optOutToggle);
// iOS Only
var asyncBreadcrumbToggle = Titanium.UI.createButton({
top:175,
width:301,
height:30,
title:'Toggle Async BreadCrumbs: No'
});
asyncBreadcrumbToggle.addEventListener('click', function()
{
// Set the status
crittercism.setAsyncBreadcrumbMode(status = !status);
// change the status in the button title for visibility
var stringStatus = status? "Yes" : "No";
asyncBreadcrumbToggle.title = 'Toggle Async BreadCrumbs: ' + stringStatus;
});
win.add(asyncBreadcrumbToggle);
win.open();
var crittercism = require('com.crittercism.ti');
var UI = {};
(function() {
UI = {
openModalWindow : function() {
crittercism.leaveBreadcrumb("Creating ModalWindow");
var win = Ti.UI.createWindow({
modal : false,
backgroundColor : 'red',
title : 'My Modal Window',
layout : 'vertical'
});
win.add(Ti.UI.createLabel({
top : 5,
color : "white",
text : "Click red area to Close!"
}));
var crashButton = Titanium.UI.createButton({
top : 70,
width : 301,
height : 30,
title : 'Crash it from myModalWindow.js'
});
crashButton.addEventListener('click', function() {
crittercism.leaveBreadcrumb("Clicking the crash button - myModalWindow.js");
UI.doSomethingHere();
});
win.add(crashButton);
win.addEventListener('click', function() {
win.close();
crittercism.leaveBreadcrumb("Closing ModalWindow - myModalWindow.js");
});
return win;
}, //openModalWindow : function()
doSomethingHere : function() {
//Test case 1:
//Without a try-catch block Crittercism will just report
//the native stack trace (without JS refs): https://gist.github.com/egomez99/5171384
//UI.HandledException();//executing the method
//Test case 2:
//Handled exceptions display JS reference and no stack trace is available:
//"Supported orientations has no common orientation with the application,
//and shouldAutorotate is returning YES at myModalWindow.js (line 85)"
try {
crittercism.leaveBreadcrumb("Attempting to open UPSIDE PORTRAIT...");
UI.HandledException();
//executing a handled exception
} catch (err) {
crittercism.leaveBreadcrumb("Oh no, it failed! Log it...");
crittercism.logHandledException(err);
//log the handled exception
}//end of try-catch block
},
HandledException : function() {
//Test case 1: Handled exception is reported with offending line of code
//For Android Platform
var a = new Array(0x100000000);
var array = new Array();
win.add(array[0]);
// this gets caught because the object is undefined and not a proxy
// throw a custom exception
var er = new Error("My Awesome Uncaught Error!");
throw er;
}
/*
Test case for iOS platform:
Getting an [ERROR] :  Script Error =
Supported orientations has no common orientation with the application,
and shouldAutorotate is returning YES at myModalWindow.js (line 79).
(TIMOB-11151): The application has crashed with an uncaught exception 'UIApplicationInvalidInterfaceOrientation'
var winTest = Ti.UI.createWindow({
backgroundColor : 'red',
orientationModes : [Titanium.UI.UPSIDE_PORTRAIT],
modal : true
});
var label = Ti.UI.createLabel({
text : 'hello'
});
winTest.add(label);
winTest.open();
}
*/
};
//UI
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment