How to capture "ReferenceError: Can't find variable" - javascript

I have the following Code:
if(AdMob) { ... }
Then I get the error:
ReferenceError: Can't find variable AdMob
How to capture this error that I don't see it in the console anymore?
EDIT:
Why AdMob is not just falsy and I'm not getting any error.

The only real way to avoid errors being logged is to put the code in a try/catch block.
try {
if(AdMob){
}
} catch(e){
}
Verify it does not result in errors on this Fiddle
Of course you could solve it as easy as defining the variable (assuming this should not be a global variable):
var AdMob;

try {
if(AdMob) { }
}
catch(err){
//Do something here
}
or you can throw custom exception
try {
if(AdMob) { }
}
catch(err){
throw new Error("Error: AdMob is not defined");
}

Use try-catch to capture the error
try {
if (AdMob) {
}
} catch (e) {
console.log(e);
}

Related

Capturing exceptions from qml components in test code

I am trying to figure out how to write "this line of code has to produce an exception" in one of my test cases for qml. What I am doing essentially is
TextField{
id: defaultLineEdit
onTextChanged: { throw "test";}
}
then in my test code:
function test_exception(){
try {
defaultLineEdit.text = "text";
}
catch(e){
console.log("caught exception");
}
}
The problem is - I have verified that the exception is thrown on text changed, but I never enter the catch branch somehow. What is happening here?

SSJS Platform.Response.Redirect throws error in Try Catch Statement

I suspect threadabortexception issue of .NET but I couldn't fix it with possible options.
In short Redirect function throws an errors and goes to the catch, no matter to set the second parameter true or false).
The code below is just an example (but I faced this a couple of times before in real-time projects).
...
try {
var TSD = TriggeredSend.Init("DE_Name");
var Status = TSD.Send(data.subscriber, data.attributes);
if (Status != "OK") {
Platform.Response.Redirect(Variable.GetValue("#error_page"));
} else {
Platform.Response.Redirect(Variable.GetValue("#thanks_page")); //<<<-- This redirect throw error
}
} catch (err) {
Platform.Response.Redirect(Variable.GetValue("#error_page")); // <---- here it comes
}
...
Resources might helps:
1# https://support.microsoft.com/en-us/help/312629/prb-threadabortexception-occurs-if-you-use-response-end-response-redir
2# https://developer.salesforce.com/docs/atlas.en-us.mc-programmatic-content.meta/mc-programmatic-content/ssjs_platformClientBrowserRedirect.htm?search_text=Redirect
Any workaround is welcome.
I know this is an older question but I think it would be good to share findings.
There is no logical explanation to me why this is happen but the Redirect always throws an error and if it is used in a try block, the catch part will be executed.
Here is a simple workaround:
...
try {
var TSD = TriggeredSend.Init("DE_Name");
var Status = TSD.Send(data.subscriber, data.attributes);
try {
if (Status != "OK") {
Platform.Response.Redirect(Variable.GetValue("#error_page"));
} else {
Platform.Response.Redirect(Variable.GetValue("#thanks_page")); //<<<-- This redirect throw error
}
} catch(e) {}
} catch (err) {
Platform.Response.Redirect(Variable.GetValue("#error_page")); // <---- here it comes
}
...

Can I get exception stacktrace in kotlin-js?

I've been writing simple web-frontend for application with kotlin-js and faced with a problem of exception handling.
As I see, there is no API to get exception stacktrace: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-throwable/index.html
Is it so?
If it is, may be anyone know some library or snippets to get stacktrace out of Throwable object?
Currently, I've got some workaround for this:
import kotlin.browser.window
fun main() {
window.onload = {
try {
throw RuntimeException()
} catch (e: Throwable) {
console.log(e)
throw e
}
}
}
Console output is:
Object {
"message_8yp7un$_0": null,
"cause_th0jdv$_0": null,
"stack": "captureStack#http://localhost:9080/js/kotlin.js:1767:27\nException#http://localhost:9080/js/kotlin.js:3244:14\nRuntimeException#http://localhost:9080/js/kotlin.js:3255:17\nRuntimeException_init#http://localhost:9080/js/kotlin.js:3261:24\nmain$lambda#http://localhost:9080/js/web-client.js:34:13\n",
"name": "RuntimeException"
}
Here, console.log(Throwable) exposes underlying JavaScript object properties, and there is stack one, but it points to JavaScript code, that is hard to use without source mapping back to kotlin.
UPD: it seems like stack is not standard exception property, but common one for modern browsers.
Since Kotlin 1.4 the stdlib contains two extensions for this exact purpose
fun Throwable.stackTraceToString()
fun Throwable.printStackTrace()
Using these we can write
window.onload = {
try {
throw RuntimeException()
} catch (e: Throwable) {
e.printStackTrace()
}
}
Just change your console.log to console.error
import kotlin.browser.window
fun main() {
window.onload = {
try {
throw RuntimeException()
} catch (e: Throwable) {
console.error(e) // change here
throw e
}
}
}
Not perfect, not standard, but it works.
It also handles the source map in Firefox, so I get proper file names and line numbers:
try {
throw IllegalStateException("ops...")
}
catch (ex : Throwable) {
val stack = ex.asDynamic().stack
if (stack is String) {
val error = js("Error()")
error.name = ex.toString().substringBefore(':')
error.message = ex.message?.substringAfter(':')
error.stack = stack
console.error(error)
} else {
console.log(ex)
}
}

How can I hide an uncaught error from my browser?

I have some simple code that does the job but gives an uncaught error.
ck.setMode('source');
ck.setMode( 'wysiwyg');
This is giving me the message:
Uncaught TypeError: Cannot read property 'on' of undefined
I know it's probably a bad practice but the error does not cause any problems and I would like to avoid it showing in the browser. Is there some way that I could enclose this code so it does not give a browser console alert?
Here's the code that encloses the above:
ngModel.$render = function () {
if (typeof ngModel.$modelValue != 'undefined') {
if (ngModel.$modelValue != null) {
ck.setData(ngModel.$modelValue);
timer = setTimeout(function () {
ck.setData(ngModel.$modelValue);
}, 1000);
timer = setTimeout(function () {
ck.setMode('source');
ck.setMode('wysiwyg');
}, 1000);
}
}
};
You may use code like this:
window.onerror = function(message, url, lineNumber) {
// maybe some handling?
return true; // prevents browser error messages
};
It prevents all error messages, so use it with care.
You can put your code block inside a try catch. So your code would become like this.
try {
ck.setMode('source');
ck.setMode( 'wysiwyg');
}
catch (error) {
// handle your error
}

creating an exception handler in HTML5/javascript

I am new to the web development world and I would like I am lost in the steps of creating an exception in a java script function
what I want to ideally do is something following the following syntax ...
function exceptionhandler (){
if (x===5)
{
//throw an exception
}
}
I found the following tutorial
http://www.sitepoint.com/exceptional-exception-handling-in-javascript/
But I don t know how to convert the above if statement into a try..catch...finally exception
thanks!
To create an error in JavaScript you have to throw something, which can be an Error, a specific type of Error, or any Object or String.
function five_is_bad(x) {
if (x===5) {
// `x` should never be 5! Throw an error!
throw new RangeError('Input was 5!');
}
return x;
}
console.log('a');
try {
console.log('b');
five_is_bad(5); // error thrown in this function so this
// line causes entry into catch
console.log('c'); // this line doesn't execute if exception in `five_is_bad`
} catch (ex) {
// this only happens if there was an exception in the `try`
console.log('in catch with', ex, '[' + ex.message + ']');
} finally {
// this happens either way
console.log('d');
}
console.log('e');
/*
a
b
in catch with RangeError {} [Input was 5!]
d
e
*/
You may be looking for something like this:
function exceptionhandler() {
try {
if (x===5) {
// do something
}
} catch(ex) {
throw new Error("Boo! " + ex.message)
}
}

Categories