Testing URL schemes with try/catch - javascript

I'm trying to test URL schemes via javascript try/catch blocks but my catch blocks are not being executed. Safari just skips them entirely and I don't understand why?!
function open_twitter() {
if (!IS_IOS){ //made unequal IOS so that I could test it on my Macbook
try {
window.location = "twitter://"; //fails and should go to the catch block
} catch (e) { //skipped
try { //skipped
window.location = TWITTERRIFIC_URL_SCHEME; //skipped
} catch (e) { //skipped
null; //skipped
} //skipped
} //skipped
} else {
window.open(TWITTER_URL, "_blank");
}
}
Does anyone have an idea why this is happening?
I want the code to test the first URL scheme. If that isn't successful than it should (in theory) go to the catch block and run the code inside which is in my case a try block again with the next URL scheme to test and so on and so forth.
But this doesn't happen?! I'm really confused...

Try catch doesn't work exactly like what you wrote, You'll need to provide your catch with an exception, Else it doesn't know something bad happened.
See these docs : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
try/catch should be used as a simple test with your custom exception. For example,
try
{
if(false==false)
{
throw "myException"; // generates an exception
}
catch (e) {
// statements to handle any exceptions
logMyErrors(e); // pass exception object to error handler
}
As you can see, The try should test for something and return an exception if some condition is met.

Related

why is otherwise callback / catch block not getting executed?

I am working with tableau JS API and using the getParametersAsync() method which return all the parameters on a workbook and there seems to be a exception when doing so.
The exception I am getting "Duplicate key 'OperatorTypeParameter'", however my otherwise callback does not get executed, I also tried to add a try / catch but the execution flow does not come in the outer catch as well as the inner catch block.
try {
workbook.getParametersAsync().then(function(parameter) {}).otherwise(function(err) {
try {
console.log(err);
} catch(e) {
console.log(e)
}
});
} catch(e) {
console.log(e)
}
Not sure if it's a bug in the API. Any help in this would be really great.
Thanks.

Is there an opposite of catch?

By running the following, you can run code if an error is thrown.
try {
// test
} catch (e) {
// error output
}
Is there a similar way to run code only if no error is thrown?
Sure there is, see the comment inline.
try {
// test
// No error is thrown if program control reaches here
} catch {
// error output
}
Consider using an additional try block in the "No error is thrown" part if you don't want the outer catch to handle any other errors.

Protractor: catch AssertionError

I'm using Protractor with Chai as Promised in order to create a javascript-based testing tool and I'm getting the error
AssertionError: expected 'http://localhost:8888/test/homepage.php' to equal 'http://localhost:8888/test/my_homepage.php'
while I'm checking the url with this step definition:
this.Then(/^The url of the page should be "([^"]*)"$/, function(myUrl, callback){
expect(browser.getCurrentUrl()).to.eventually.equal(myUrl);
callback();
});
I would like to catch this error in order to use a different callback function, how can I do that? I've tried to use a try-catch block but it doesn't seem to work. I can't even understand if AssertionErrors are generated by Protractor, can you gently give me an explaination about this?
Thank you in advance
I couldn't find anything which could catch errors from expect and do something else. If #alecxe's suggestion from comments work, that should be your answer otherwise why not just do
browser.getCurrentUrl().then(function(url) {
if(url === myUrl) {
callback();
} else {
callback('something went wrong');
}
});
or would this not work?
try {
expect(browser.getCurrentUrl()).to.eventually.equal(myUrl);
callback();
} catch(e) {
callback('something went wrong '));
}

How do I capture and resume global errors in javascript?

I am writing a testing suite for javascript and need a way to capture any JS errors and continue processing the rest of the page.
I can't use window.onerror since return true stops the browser from proceeding. I tried using a try {} catch block but the function is run in a window setTimeout() for various reasons and that seems to mess up the try catch block.
For example, I do something like this:
function test(msg, fn) {
$('#output').text(msg);
try {
fn.apply(this);
} catch(e) {
document.write('error'+e);
};
}
and then call it like this
test('trying this', function() { setTimeout('afunction()',100); });
but if afunction() fails the error is not being caught.
Does anyone have any idea on a solution or global error handler that allows me to resume?
There is window.onerror, which you can attach a handler function to. If that function returns true, the error is suppressed: http://jsfiddle.net/pimvdb/qAv9J/2/.
window.onerror = function(e) {
console.log(e);
return true;
};

How javascript try...catch statement works

I am trying to test in browsermob if certain input field work or not. I am attempting to use a try...catch statement which I have never used before. I know that the form is:
try {
//some code
} catch (){
//some error code
};
What exactly is supposed to be put in the parenthesis after the catch statement?
When I try to use the statement it runs everything through the catch statement no matter if it is not an error. What am I doing wrong?
See the “try...catch statement” guide on MDN.
In short, try/catch is used to handle exceptions (which are "thrown" using the throw statement). The syntax for try/catch is:
try {
// Code
} catch (varName) { // Optional
// If exception thrown in try block,
// execute this block
} finally { // Optional
// Execute this block after
// try or after catch clause
// (i.e. this is *always* called)
}
varName is available to the scope of the catch block only. It refers to the exception object which was thrown (which could be any type of object, e.g. a String, but is usually an Error object).
The try catch statement is used to detected for exceptions/errors that are raised inside the try-block. In the catch block you can then react on this exceptional behavior and try to resolve it or get to a safe state.
You got the statement almost right:
try {
// code that may fail with error/exception
} catch (e) { // e represents the exception/error object
// react
}
Consider the following examples:
try {
var x = parseInt("xxx");
if(isNaN(x)){
throw new Error("Not a number");
}
} catch (e) { // e represents the exception/error object
alert(e);
}
try {
// some code
if(!condition){
throw new Error("Something went wrong!");
}
} catch (e) { // e represents the exception/error object
alert(e);
}
the stuff inside try {...} is what you want to execute. The stuff in catch() { ... } is what you want to execute if you get any javascript errors from anything executed in the try {...}
catch {...} only executes if there is a javascript error in the try {...} block. You can find out what the error is by doing for example this:
try {
// do something
} catch (err) {
alert(err);
}
According to ECMAScript specifications,
try {
// Code
} catch (varName) { // optional if 'finally' block is present.
if (condition) { // eg. (varName instanceof URIError)
// Condition (Type) specific error handling
}
else {
// Generic error handling
}
} finally { // Optional if 'catch' block is present.
// Execute this block after
// try or after catch clause
// (i.e. this is *always* called)
}
the code that is likely to throw an exception goes into try { }, The code to be run when an exception is thrown, comes into catch() { }. In catch() you can specify which exceptions you want to catch, and in which automatic variables to put it.
finally { } is always run, regardless whether exception was thrown or not.

Categories

Resources