-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
The Problem
Currently, when an unhandled exception is thrown in NativeScript (inside app-code, core-modules or plugins) it will (in most) cases crash the application showing the error and stack trace.
When in development - in many cases this is what you'd expect. You would want to have the stack-trace of the exact location the unexpected behavior has occurred, so that you can more easily understand what happened and fix it.
However, in production, an application crash can seriously hurt you application credibility and drive away customers. In many cases you might prefer anything else (app freeze, blank screen, failed navigation etc.) to an actual crash with an error log. There are even cases, in whitch the application will recover and will work OK even if something unexpected has happened - for exampled failed animation or a typo in CSS property.
Proposal
The aim of this FR is to propose an API that provides a way to handle errors in different ways in development and in production. It should provide flexibility to configure how errors should be handled. For example:
- (development 1) Throw exceptions as soon as error occurs.
- (development 2) Show a scary
console.logwithERROR: Something bad happenedbut continue execution of the app. You will see it in you terminal, but decide if it is critical based on what happens with the app after that. - (production) Send an error report to your analytics/error-report server, but continue app execution. Maybe triggers some recover logic that will handle the app without a crash.
API Proposal
Extend the trace module with an error() method.
Use the method in core-framework/plugins/app code:
function doSomething(arg) {
// Instead of
if(!arg) throw new Error("Arg not provided in "doSomething");
// Use trace.error()
if(!arg) {
trace.error("Arg not provided in "doSomething");
return;
}
// ... implementation using arg
}
Developers can define custom error handler logic (probably on app startup):
const errorHandler: ErrorHandler = {
handleError(err){
//(development 1)
throw err;
//(development 2)
trace.write(err, "unhandlede-error", type.error);
//(production)
reportToAnalytics(err)
}
}
trace.setErrorHandler(errorHandler)
Next Steps
- Gather FB on this proposal and implement it in the
tns-core-modules. - Create a comprehensive guide on how to use the error API in the contributing.md. Update: here it is.
- Require all PRs to follow the guide and handle errors in a similar fashion
- (ongoing) Gradually refactor most problematic areas of the codebase to use comply with the guides and use this approach (PRs will be welcome).