-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from providerx/documentation
Finished error handling documentation
- Loading branch information
Showing
19 changed files
with
139 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
export type ProviderReference = { | ||
import { throwError } from "rxjs" | ||
|
||
export class ProviderReference { | ||
executionError?: any | ||
|
||
error(error: any) { | ||
this.executionError = error | ||
return throwError(error) | ||
} | ||
} | ||
|
||
export class AutoDisposeProviderReference extends ProviderReference { | ||
maintainState: boolean | ||
constructor(maintainState?: boolean) { | ||
super() | ||
this.maintainState = maintainState ?? true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
title: Error Handling | ||
--- | ||
|
||
For more information about `ObservableProvider`, [read this](/docs/) | ||
|
||
## Error Handling | ||
By default, ProviderX will find errors which occur in our `Observable` and send them to our consumers | ||
in our frontend UI | ||
|
||
However... | ||
|
||
In many situations we may want to handle errors and run code based on them | ||
Let's use the following example: | ||
|
||
```typescript | ||
import { from } from 'rxjs' | ||
import { catchError } from 'rxjs/operators' | ||
|
||
const provider = ObservableProvider.autoDispose(() => { | ||
const fetchUser = async () => { | ||
const response = await fetch( | ||
'https://jsonplaceholder.typicode.com/users/1' | ||
) | ||
const json = await response.json() | ||
return json | ||
} | ||
const observable = from(fetchUser()).pipe( | ||
catchError((error: any) => { | ||
ref.maintainState = false | ||
return ref.error(error) | ||
}), | ||
) | ||
return observable | ||
}) | ||
``` | ||
Points to Note: | ||
- We catch the error using `catchError` | ||
- We set `ref.maintainState` to `false` because we do not want the state of the provider to be kept | ||
even after it has no listeners and we want the provider value to be recomputed once again | ||
- We return `ref.error(error)` which tells ProviderX we have an error | ||
|
||
:::danger Return your errors! | ||
|
||
```typescript | ||
catchError((error: any) => { | ||
ref.maintainState = false | ||
return ref.error(error) | ||
}), | ||
``` | ||
|
||
***If*** you use `catchError`, make sure to return `ref.error(error)` otherwise | ||
ProviderX will not know that an error has been received. | ||
|
||
::: |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.