-
Notifications
You must be signed in to change notification settings - Fork 44
docs: mention 3 strats for with(Entity)Resource error handling
#271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
4a484c0
00e2d14
f8152f4
9ece275
452c787
88ff856
964a75f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,35 @@ This exposes per-resource members with the resource name as a prefix: | |
| - **Resource members**: `todosValue()`, `todosStatus()`, `todosError()`, `todosIsLoading()`; `projectsValue()`, ... | ||
| - **Entity members**: `todosIds()`, `todosEntityMap()`, `todosEntities()`; `projectsIds()`, `projectsEntityMap()`, `projectsEntities()` | ||
|
|
||
| ## Error Handling | ||
|
|
||
| Starting in NgRx Toolkit v20.6.0, error handling now has more resilient options. | ||
|
|
||
| The behavior of Angular's resources' error handling and the NgRx SignalStore's `getState/patchState` required `withEntityResource` to approach error handling | ||
|
||
| with a particular strategy unique to the intersection of resources and the Signal Store. | ||
| To prevent resource failures from blocking the store, the Toolkit provides some strategies to handle errors. | ||
|
|
||
| ```ts | ||
| withEntityResource( | ||
| () => ({ | ||
| id: resource({ | ||
| loader: () => Promise.resolve(1), | ||
| defaultValue: 0, | ||
| }), | ||
| }), | ||
| // Other values: 'native' and 'previous value' | ||
| { errorHandling: 'undefined value' }, // default if not specified | ||
| ), | ||
| ``` | ||
|
|
||
| Options: | ||
|
|
||
| 1. `'undfined value'` (default). In the event of an error, the resource's value will be `undefined` | ||
| 1. `'previous value'`. Provided the resource had a previous value, that previous value will be returned. If not, an error is thrown. | ||
| 1. `'native'`. No special handling is provided, inline with default error behavior. | ||
|
|
||
| Under the hood, `'previous value'` and `'undefined value'` proxy the value. For a detailed explanation for why this is done and what a more longterm solution may be with some framework enhancements, 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). | ||
|
|
||
| ## Component Usage | ||
|
|
||
| ```typescript | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -83,6 +83,35 @@ With named resources, each resource gets prefixed properties: | |||||
| - **Single resource:** use when your store works with just one data source. | ||||||
| - **Named resources:** use when your store is larger and manages multiple entities or async operations. | ||||||
|
|
||||||
| ## Error Handling | ||||||
|
|
||||||
| Starting in NgRx Toolkit v20.6.0, error handling now has more resilient options. | ||||||
|
|
||||||
| The behavior of Angular's resources' error handling and the NgRx SignalStore's `getState/patchState` required `withResource` to approach error handling | ||||||
|
||||||
| with a particular strategy unique to the intersection of resources and the Signal Store. | ||||||
| To prevent resource failures from blocking the store, the Toolkit provides some strategies to handle errors. | ||||||
|
|
||||||
| ```ts | ||||||
| withResource( | ||||||
| () => ({ | ||||||
| id: resource({ | ||||||
| loader: () => Promise.resolve(1), | ||||||
| defaultValue: 0, | ||||||
| }), | ||||||
| }), | ||||||
| // Other values: 'native' and 'previous value' | ||||||
| { errorHandling: 'undefined value' }, // default if not specified | ||||||
| ), | ||||||
| ``` | ||||||
|
|
||||||
| Options: | ||||||
|
|
||||||
| 1. `'undfined value'` (default). In the event of an error, the resource's value will be `undefined` | ||||||
|
||||||
| 1. `'undfined value'` (default). In the event of an error, the resource's value will be `undefined` | |
| 1. `'undefined value'` (default). In the event of an error, the resource's value will be `undefined` |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't mention the condition that it only returns if a previous value exists. It just returns the previous value.
A resource always has a value in its initial state. The implementation needs to consider a "non-realsitic" scenario because of TypeScript.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The complete
withEntityResourceis based on theundefined valuestrategy. I think we just need to here a quick note on that and link towithResourcefor further details.Users cannot change the error handling here, since
withEntityResourceuses the resource internally. It is an implementation detail so to say.