angular-docs-cn/packages/core/test/render3
Alex Rickabaugh 32c760f5e7 fix(ivy): don't mask errors by calling lifecycle hooks after a crash (#31244)
The Angular runtime frequently calls into user code (for example, when
writing to a property binding). Since user code can throw errors, calls to
it are frequently wrapped in a try-finally block. In Ivy, the following
pattern is common:

```typescript
enterView();
try {
  callUserCode();
} finally {
  leaveView();
}
```

This has a significant problem, however: `leaveView` has a side effect: it
calls any pending lifecycle hooks that might've been scheduled during the
current round of change detection. Generally it's a bad idea to run
lifecycle hooks after the application has crashed. The application is in an
inconsistent state - directives may not be instantiated fully, queries may
not be resolved, bindings may not have been applied, etc. Invariants that
the app code relies upon may not hold. Further crashes or broken behavior
are likely.

Frequently, lifecycle hooks are used to make assertions about these
invariants. When these assertions fail, they will throw and "swallow" the
original error, making debugging of the problem much more difficult.

This commit modifies `leaveView` to understand whether the application is
currently crashing, via a parameter `safeToRunHooks`. This parameter is set
by modifying the above pattern:

```typescript
enterView();
let safeToRunHooks = false;
try {
  callUserCode();
  safeToRunHooks = true;
} finally {
  leaveView(..., safeToRunHooks);
}
```

If `callUserCode` crashes, then `safeToRunHooks` will never be set to `true`
and `leaveView` won't call any further user code. The original error will
then propagate back up the stack and be reported correctly. A test is added
to verify this behavior.

PR Close #31244
2019-06-26 08:01:14 -07:00
..
ivy refactor(ivy): fix type of factory functions to allow explicit types (#30855) 2019-06-11 14:27:17 -07:00
jit fix(core): require 'static' flag on queries in typings (#30639) 2019-05-24 16:55:00 -04:00
styling refactor(ivy): inherently call ɵɵselect(0) (#30830) 2019-06-07 08:48:31 -07:00
styling_next refactor(ivy): enable sanitization support for the new styling algorithm (#30667) 2019-05-30 01:03:39 -04:00
BUILD.bazel Revert "feat(ivy): use i18n locale data to determine the plural form of ICU expressions (#29249)" (#29918) 2019-04-15 16:55:51 -07:00
basic_perf.ts refactor(ivy): Move instructions back to ɵɵ (#30546) 2019-05-20 16:37:47 -07:00
change_detection_spec.ts refactor(ivy): update ɵɵtextBinding to not take index (#30792) 2019-06-14 12:22:11 -07:00
common_with_def.ts refactor(ivy): Move instructions back to ɵɵ (#30546) 2019-05-20 16:37:47 -07:00
component_ref_spec.ts fix(ivy): unable to project into multiple slots with default selector (#30561) 2019-05-31 09:52:32 -07:00
component_spec.ts refactor(ivy): make `bind` an internal-only function (#31131) 2019-06-20 17:20:56 -07:00
content_spec.ts fix(ivy): unable to project into multiple slots with default selector (#30561) 2019-05-31 09:52:32 -07:00
control_flow_spec.ts refactor(ivy): make `bind` an internal-only function (#31131) 2019-06-20 17:20:56 -07:00
debug_spec.ts refactor(ivy): rename debug.ts to lview_debug.ts (#30542) 2019-06-04 12:01:46 -07:00
di_spec.ts fix(ivy): don't mask errors by calling lifecycle hooks after a crash (#31244) 2019-06-26 08:01:14 -07:00
domino.d.ts feat(core): Moving Renderer3 into @angular/core (#20855) 2017-12-21 21:40:58 -08:00
exports_spec.ts refactor(ivy): make `bind` an internal-only function (#31131) 2019-06-20 17:20:56 -07:00
global_utils_spec.ts refactor(ivy): split util functions into different files (#28382) 2019-02-22 13:17:30 -08:00
i18n_spec.ts feat(ivy): support `ng-content` in runtime i18n translations (#30782) 2019-06-05 09:04:13 -07:00
imported_renderer2.ts fix(platform-browser): insert APP_ID in styles, contentAttr and hostAttr (#17745) 2019-04-11 07:59:22 -07:00
instructions_spec.ts refactor(ivy): update ɵɵtextBinding to not take index (#30792) 2019-06-14 12:22:11 -07:00
integration_spec.ts refactor(ivy): make `bind` an internal-only function (#31131) 2019-06-20 17:20:56 -07:00
jit_environment_spec.ts refactor(ivy): Move instructions back to ɵɵ (#30546) 2019-05-20 16:37:47 -07:00
lifecycle_spec.ts refactor(ivy): make `bind` an internal-only function (#31131) 2019-06-20 17:20:56 -07:00
listeners_spec.ts refactor(ivy): make `bind` an internal-only function (#31131) 2019-06-20 17:20:56 -07:00
load_domino.ts fix(core): Remove static dependency from @angular/core to @angular/compiler (#26734) 2018-10-31 14:15:06 -04:00
metadata_spec.ts build: extract interface and util sub compilation from core (#28028) 2019-01-10 16:31:44 -08:00
node_selector_matcher_spec.ts fix(ivy): classes should not mess up matching for bound dir attributes (#30002) 2019-04-22 08:48:08 -07:00
outputs_spec.ts refactor(ivy): Move instructions back to ɵɵ (#30546) 2019-05-20 16:37:47 -07:00
perfCounter_spec.ts build: extract interface and util sub compilation from core (#28028) 2019-01-10 16:31:44 -08:00
pipe_spec.ts refactor(ivy): update ɵɵtextBinding to not take index (#30792) 2019-06-14 12:22:11 -07:00
providers_spec.ts refactor(ivy): make `bind` an internal-only function (#31131) 2019-06-20 17:20:56 -07:00
pure_function_spec.ts refactor(ivy): make `bind` an internal-only function (#31131) 2019-06-20 17:20:56 -07:00
query_spec.ts refactor(ivy): rename query-related functions for clarity (#30587) 2019-05-21 13:14:15 -07:00
render_util.ts refactor(ivy): createNodeAtIndex no longer depends on L* (#30453) 2019-05-23 10:30:59 -07:00
renderer_factory_spec.ts refactor(ivy): Move instructions back to ɵɵ (#30546) 2019-05-20 16:37:47 -07:00
testing_spec.ts refactor(core): remove withBody from public testing API (#25171) 2018-07-31 15:09:32 -07:00
util_spec.ts refactor(ivy): remove duplicated flatten util (#29547) 2019-03-27 12:36:23 -07:00
view_container_ref_spec.ts refactor(ivy): Move instructions back to ɵɵ (#30546) 2019-05-20 16:37:47 -07:00
view_utils_spec.ts refactor(ivy): move instructions (#29646) 2019-04-02 15:47:02 -07:00