Kindly do write If any one know how to catch uncaughtErrorEvent and Exception?
Hello @katturajam
For NativeScript you can handle uncaughtErrorEvent in your app liike this (in your app.js)
var application = require("application");
application.on(application.uncaughtErrorEvent, function (args) {
if (args.android) {
// For Android applications, args.android is an NativeScriptError.
console.log("NativeScriptError: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is NativeScriptError.
console.log("NativeScriptError: " + args.ios);
}
});
For more information reffer to this documentaion article: https://docs.nativescript.org/core-concepts/application-management
Please provide additional information on the exact problem you are trying to solve. Is there a code related to your issue that you try to handle in NativeScript!? Your question is too abstract and if you need basic information please reffer to online sources on that matter - for example: http://eloquentjavascript.net/1st_edition/chapter5.html
Above example is not working for me. I am using {N}@2.0.1. Can you explain more detail?
Hey @katturajam do you have a specific situation you are trying to handle - if so please provide your code in order to help you out.
application.uncaughtErrorEvent will trigger whenever your app is crashing and the cause is not handled.
For example:
if you try to initialize an android button with null context like this in your main-page.js it will throw an error
in _main-page.js_
var btn = new android.widget.Button(null);
What you can do to catch this error is to use uncaughtErrorEvent
in _app.js_
"use strict";
var application = require("application");
application.on(application.uncaughtErrorEvent, function (args) {
if (application.android) {
// For Android applications, args.android is an NativeScriptError.
console.log(" *** NativeScriptError *** : " + args.android);
console.log(" *** StackTrace *** : " + args.android.stackTrace);
console.log(" *** nativeException *** : " + args.android.nativeException);
}
else if (application.ios) {
// For iOS applications, args.ios is NativeScriptError.
console.log("NativeScriptError: " + args.ios);
}
});
application.start({ moduleName: "main-page" });
From the log you can see the following information:
NativeScriptError : Error: The application crashed because of an uncaught exception.
from the stackTrace you can see the cause of this error : Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on null reference object
You also have a pointer where the error has occured:
Frame: function:'navigatingTo', file:'/data/data/org.nativescript.cameraUpload/files/app/main-page.js', line: 11, column: 15
Dear friend,
Its working good for me. Thanks for your help.
On Jun 15, 2016 2:52 PM, "Nick Iliev" [email protected] wrote:
Hey @katturajam https://github.com/katturajam do you have a specific
situation you are trying to handle - if so please provide your code in
order to help you out._application.uncaughtErrorEvent_ will trigger whenever your app is
crashing and the cause is not handled.For example:
if you try to initialize an android button with null context liek this in
your main-page.js it will throw an errorin _main-page.js_
var btn = new android.widget.Button(null);
What you can do to catch this error is to use _uncaughtErrorEvent_
in _app.js_
"use strict";
var application = require("application");application.on(application.uncaughtErrorEvent, function (args) {
if (args.android) {
// For Android applications, args.android is an NativeScriptError.
console.log(" ** NativeScriptError ** : " + args.android);
console.log(" ** StackTrace ** : " + args.android.stackTrace);
console.log(" ** nativeException ** : " + args.android.nativeException);
}
else if (args.ios) {
// For iOS applications, args.ios is NativeScriptError.
console.log("NativeScriptError: " + args.ios);
}
});
application.start({ moduleName: "main-page" });From the log you can see the following information:
NativeScriptError : Error: The application crashed because of an uncaught
exception.from the stackTrace you can see the cause of this error : Attempt to
invoke virtual method 'android.content.res.Resources
android.content.Context.getResources()' on null reference objectYou also have a pointer where the error has oocured:
Frame: function:'navigatingTo',
file:'/data/data/org.nativescript.cameraUpload/files/app/main-page.js',
line: 11, column: 15—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/NativeScript/NativeScript/issues/2309#issuecomment-226134131,
or mute the thread
https://github.com/notifications/unsubscribe/ANinJ23QVewWdZL2D-ngcBpWdbvm9-aeks5qL8RPgaJpZM4I1Ppw
.
@NickIliev Should the uncaughtErrorEvent
work in the simulator?
I'm struggling to handle the uncaughtErrorEvent
in the iOS simulator, but I THINK it works fine when running on an actual device. Wondering if this is by design or if I'm doing something wrong in the simulator...
@toddanglin by design it should work on both simulator and real device. Keep in mind that it will return an error that is not try/catch
wrapped. I have created this very basic applicaiton that will trigger the event in iOS on both simulator and device.
handling the event in app.ts
import { on, start, uncaughtErrorEvent, android, ios } from "application";
on(uncaughtErrorEvent, (args) => {
if (android) {
// For Android applications, args.android is an NativeScriptError.
console.log(" *** NativeScriptError *** : " + args.android);
console.log(" *** StackTrace *** : " + args.android.stackTrace);
console.log(" *** nativeException *** : " + args.android.nativeException);
} else if (ios) {
// For iOS applications, args.ios is NativeScriptError.
console.log(" ||||| NativeScriptError in iOS ||||| " + args.ios);
}
});
And triggering an error with this line
If you have a case where the error is thrown only on a device then perhaps it is related to some specific device requirements. You can send me a sample application or snippet that demonstrates the issue so I can test it on my side.
Thanks, @NickIliev!
I can confirm that this does work on the iOS simulator. After some more digging, it seems that the problem is specific to the plugin I'm working on: https://github.com/toddanglin/nativescript-trace-raven
It's a custom TraceWriter that writes logs to Sentry.io. For some reason, when this TraceWriter is loaded, the uncaughtErrorEvent
gets swallowed and does not fire. Not sure why...
Either way, it's clear now the problem is somewhere in the plugin implementation.
Will this swallow exceptions without crashing the app?
I am also interested in what @RoyiNamir asked. I am encountering a problem with a plugin (admob), that crashes some times, and it would be very helpful If the exception was not crashing the app, since the app could still keep working without this plugin.
@Spahar I'll tell you why it crashes. There is a setTimeout which sometimes, it can't find page to put an ad on.
Go to the plugin code and change the code there ( in the right section) to :
setTimeout(function () {
if (frame.topmost() !== undefined) {
// frame.topmost().currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter);
frame.topmost().currentPage &&
frame.topmost().currentPage.android &&
frame.topmost().currentPage.android.getParent()&&
frame.topmost().currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter);
} else {
//application.android.foregroundActivity.getWindow().getDecorView().addView(adViewLayout, relativeLayoutParamsOuter);
application &&
application.android &&
application.android.foregroundActivity &&
application.android.foregroundActivity.getWindow()&&
application.android.foregroundActivity.getWindow().getDecorView()&&
application.android.foregroundActivity.getWindow().getDecorView().addView(adViewLayout, relativeLayoutParamsOuter);
}
}, 0);
To me it happens , when I create banner in a setTimeout of 3 seconds ,while quickly minimize the app , and then exception was thrown becuase when I minimize it , ,after 3 sec , it didn't ind an activity to put the banner on. Hence these^ checkings.
Thanks a lot @RoyiNamir, it seems to be working fine with this change. The only downside is that if I have to remove modules and reimport I have to change it again. But it is a small price to pay. ;P
@NickIliev does this still work as you have described here? I have a button
<Button text="Log In" (tap)="login()"></Button>
Where the login function is defined in the component:
declare var android: any;
// ...
export class AuthPageComponent implements OnInit {
// ...
login() {
console.log('attempting to throw error');
var btn = new android.widget.Button(null);
throw new Error('test');
}
And in my main app.component
file I am registering the uncaughtErrorEvent
:
import { on as applicationOn, uncaughtErrorEvent, UnhandledErrorEventData } from 'application';
// ...
export class AppComponent {
constructor(
private http: HttpClient,
private api: ApiService
) {
applicationOn(uncaughtErrorEvent, (errData: UnhandledErrorEventData) => {
console.log('caught error', errData);
});
}
}
But, in my console, I never see caught error
. I see
ERROR Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
android.view.ViewConfiguration.get(ViewConfiguration.java:432)
android.view.View.<init>(View.java:4573)
android.view.View.<init>(View.java:4706)
android.widget.TextView.<init>(TextView.java:826)
android.widget.Button.<init>(Button.java:166)
android.widget.Button.<init>(Button.java:141)
android.widget.Button.<init>(Button.java:117)
android.widget.Button.<init>(Button.java:96)
com.tns.Runtime.callJSMethodNative(Native Method)
com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1116)
com.tns.Runtime.callJSMethodImpl(Runtime.java:996)
com.tns.Runtime.callJSMethod(Runtime.java:983)
com.tns.Runtime.callJSMethod(Runtime.java:967)
com.tns.Runtime.callJSMethod(Runtime.java:959)
com.tns.gen.java.lang.Object_button_19_32_ClickListenerImpl.onClick(Object_button_19_32_ClickListenerImpl.java:17)
android.view.View.performClick(View.java:6294)
android.view.View$PerformClick.run(View.java:24770)
android.os.Handler.handleCallback(Handler.java:790)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:164)
android.app.ActivityThread.main(ActivityThread.java:6494)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Is there something that I'm missing? Or has something changed since this issue was discussed?
@Spahar I'll tell you why it crashes. There is a setTimeout which sometimes, it can't find page to put an ad on.
Go to the plugin code and change the code there ( in the right section) to :
setTimeout(function () { if (frame.topmost() !== undefined) { // frame.topmost().currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter); frame.topmost().currentPage && frame.topmost().currentPage.android && frame.topmost().currentPage.android.getParent()&& frame.topmost().currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter); } else { //application.android.foregroundActivity.getWindow().getDecorView().addView(adViewLayout, relativeLayoutParamsOuter); application && application.android && application.android.foregroundActivity && application.android.foregroundActivity.getWindow()&& application.android.foregroundActivity.getWindow().getDecorView()&& application.android.foregroundActivity.getWindow().getDecorView().addView(adViewLayout, relativeLayoutParamsOuter); } }, 0);
To me it happens , when I create banner in a setTimeout of 3 seconds ,while quickly minimize the app , and then exception was thrown becuase when I minimize it , ,after 3 sec , it didn't ind an activity to put the banner on. Hence these^ checkings.
Hello @RoyiNamir , I'm facing the same problem with Admob on Android platform. Where do you write your workaround ? in which plugin ?
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Hey @katturajam do you have a specific situation you are trying to handle - if so please provide your code in order to help you out.
application.uncaughtErrorEvent will trigger whenever your app is crashing and the cause is not handled.
For example:
if you try to initialize an android button with null context like this in your main-page.js it will throw an error
in _main-page.js_
What you can do to catch this error is to use uncaughtErrorEvent
in _app.js_
From the log you can see the following information:
NativeScriptError :
Error: The application crashed because of an uncaught exception.
from the stackTrace you can see the cause of this error :
Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on null reference object
You also have a pointer where the error has occured:
Frame: function:'navigatingTo', file:'/data/data/org.nativescript.cameraUpload/files/app/main-page.js', line: 11, column: 15