Title: [Feature Request] Flexible Error/Exception handling · Issue #5914 · NativeScript/NativeScript · GitHub
Open Graph Title: [Feature Request] Flexible Error/Exception handling · Issue #5914 · NativeScript/NativeScript
X Title: [Feature Request] Flexible Error/Exception handling · Issue #5914 · NativeScript/NativeScript
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 ...
Open Graph 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 tra...
X 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 tra...
Opengraph URL: https://github.com/NativeScript/NativeScript/issues/5914
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Feature Request] Flexible Error/Exception handling","articleBody":"## The Problem\r\nCurrently, 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. \r\n\r\nWhen 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.\r\n\r\nHowever, 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.\r\n\r\n## Proposal\r\n\r\nThe 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:\r\n\r\n1. _(development 1)_ Throw exceptions as soon as error occurs.\r\n2. _(development 2)_ Show a scary `console.log` with `ERROR: Something bad happened` but 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.\r\n3. _(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. \r\n\r\n### API Proposal\r\nExtend the `trace` module with an `error()` method. \r\n\r\nUse the method in core-framework/plugins/app code:\r\n```\r\nfunction doSomething(arg) {\r\n // Instead of \r\n if(!arg) throw new Error(\"Arg not provided in \"doSomething\");\r\n \r\n // Use trace.error()\r\n if(!arg) {\r\n trace.error(\"Arg not provided in \"doSomething\");\r\n return;\r\n }\r\n // ... implementation using arg\r\n}\r\n```\r\n\r\nDevelopers can define custom error handler logic (probably on app startup):\r\n\r\n```\r\nconst errorHandler: ErrorHandler = {\r\n handleError(err){\r\n //(development 1)\r\n throw err;\r\n\r\n //(development 2)\r\n trace.write(err, \"unhandlede-error\", type.error);\r\n\r\n //(production)\r\n reportToAnalytics(err)\r\n }\r\n}\r\n\r\ntrace.setErrorHandler(errorHandler)\r\n```\r\n\r\n### Next Steps\r\n\r\n- [x] Gather FB on this proposal and implement it in the `tns-core-modules`.\r\n- [x] Create a comprehensive guide on how to use the error API in the [contributing.md](https://github.com/NativeScript/NativeScript/blob/master/CONTRIBUTING.md). **Update**: [here](https://github.com/NativeScript/NativeScript/blob/master/HandlingErrors.md) it is.\r\n- [x] Require all PRs to follow the guide and handle errors in a similar fashion\r\n- [x] _(ongoing)_ Gradually refactor most problematic areas of the codebase to use comply with the guides and use this approach (PRs will be welcome).\r\n \r\n\r\nPing @NathanWalker @EddyVerbruggen ","author":{"url":"https://github.com/vakrilov","@type":"Person","name":"vakrilov"},"datePublished":"2018-06-05T11:47:56.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/5914/NativeScript/issues/5914"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a5249f46-85f3-04cd-ed7d-0d587253bd91 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EA0A:AA33E:13C6F4:1A028D:6A5FE7B2 |
| html-safe-nonce | 3d6816f5cac213eec8bf26b253448624fa8013559a6fbcfd9bdbe8b721c8083f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTBBOkFBMzNFOjEzQzZGNDoxQTAyOEQ6NkE1RkU3QjIiLCJ2aXNpdG9yX2lkIjoiNDMzNzkwMDY1ODQ1ODg3OTkyMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 27fdd1d6a56d5add4de36fdefa610618059f2c976a3e000f64a5d4cc8bb11f56 |
| hovercard-subject-tag | issue:329420155 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/NativeScript/NativeScript/5914/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e9880e8ee8eaa6cc206592dac999ae3bdf66d637e994258ad3a9632f86dd039e/NativeScript/NativeScript/issues/5914 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e9880e8ee8eaa6cc206592dac999ae3bdf66d637e994258ad3a9632f86dd039e/NativeScript/NativeScript/issues/5914 |
| og:image:alt | 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 tra... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vakrilov |
| hostname | github.com |
| expected-hostname | github.com |
| None | 78938542173c0d8e4ad1aefc7954896e81c10ce2620286603ef14cf593bd5a11 |
| turbo-cache-control | no-preview |
| go-import | github.com/NativeScript/NativeScript git https://github.com/NativeScript/NativeScript.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 31492490 |
| octolytics-dimension-repository_nwo | NativeScript/NativeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31492490 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/NativeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 1a427601e09953033b696253e754ebc7328096a7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width