Skip to content

Commit 9f9b30a

Browse files
docs: mention 3 strats for with(Entity)Resource error handling (#271)
1 parent 10e1a4e commit 9f9b30a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

docs/docs/with-entity-resources.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ This exposes per-resource members with the resource name as a prefix:
8686
- **Resource members**: `todosValue()`, `todosStatus()`, `todosError()`, `todosIsLoading()`; `projectsValue()`, ...
8787
- **Entity members**: `todosIds()`, `todosEntityMap()`, `todosEntities()`; `projectsIds()`, `projectsEntityMap()`, `projectsEntities()`
8888

89+
## Error Handling
90+
91+
Starting in NgRx Toolkit v20.6.0, error state handling for `withResource` and `withEntityResources` was enhanced.
92+
93+
In the event of an error in a resource declared in `withEntityResource`, the resource's value will be `undefined`.
94+
95+
For further details, check out the error handling section of [`withResource`](./with-resource.md#error-handling).
96+
8997
## Component Usage
9098

9199
```typescript

docs/docs/with-resource.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,34 @@ With named resources, each resource gets prefixed properties:
8383
- **Single resource:** use when your store works with just one data source.
8484
- **Named resources:** use when your store is larger and manages multiple entities or async operations.
8585

86+
## Error Handling
87+
88+
The error throwing behavior of the native `resource` causes a deadlock in the error case.
89+
That's why `withResource` (as of NgRx Toolkit v20.6.0) comes with a different error handling strategy, which doesn't throw.
90+
91+
To prevent resource failures from blocking the store, the Toolkit provides some strategies to handle errors.
92+
93+
```ts
94+
withResource(
95+
() => ({
96+
id: resource({
97+
loader: () => Promise.resolve(1),
98+
defaultValue: 0,
99+
}),
100+
}),
101+
// Other values: 'native' and 'previous value'
102+
{ errorHandling: 'undefined value' }, // default if not specified
103+
),
104+
```
105+
106+
Options:
107+
108+
1. `'undefined value'` (default). In the event of an error, the resource's value will be `undefined`
109+
1. `'previous value'`. The resource's previous value will be returned.
110+
1. `'native'`. No special handling is provided, inline with default error behavior.
111+
112+
Under the hood, `'previous value'` and `'undefined value'` proxy the value. For a detailed explanation for why this is done, check out the [JSDoc for the error handling strategy](https://github.com/angular-architects/ngrx-toolkit/blob/main/libs/ngrx-toolkit/src/lib/with-resource.ts#L402).
113+
86114
## Component Usage
87115

88116
```typescript

0 commit comments

Comments
 (0)