-
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.
- Loading branch information
1 parent
2ac5907
commit 9b38a94
Showing
19 changed files
with
561 additions
and
591 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
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,8 +1,8 @@ | ||
## Features | ||
|
||
- **Small** - Less than 3KB | ||
- **Composable** - Safely reuse previous configurations | ||
- **Intuitive** - Clean and easy to use API | ||
- **Type safe** - Strongly typed, written in TypeScript | ||
- **Isomorphic** - Compatible with modern browsers, Node.js and Deno | ||
- **Well Tested** - Fully covered by unit tests | ||
- **Small** - Less than 3KB | ||
- **Composable** - Safely reuse previous configurations | ||
- **Intuitive** - Clean and easy to use API | ||
- **Type safe** - Strongly typed, written in TypeScript | ||
- **Isomorphic** - Compatible with modern browsers, Node.js and Deno | ||
- **Well Tested** - Fully covered by unit tests |
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 +1,77 @@ | ||
# Error Handling | ||
|
||
Sicen [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) doest not reject on http error status, manually checking and handling every request error code can be very tedious: | ||
|
||
```typescript | ||
|
||
fetch("anything") | ||
.then(response => { | ||
if(!response.ok) { | ||
if(response.status === 404) throw new Error("Not found"); | ||
else if(response.status === 401) throw new Error("Unauthorized"); | ||
else if(response.status === 418) throw new Error("I'm a teapot !"); | ||
/* ... */ | ||
else throw new Error("Other error") | ||
} else { | ||
/* ... */ | ||
} | ||
}) | ||
.then(data => /* ... */) | ||
.catch(error => { /* ... */ }) | ||
``` | ||
|
||
Fetchtastic throws an [HttpError](/reference/http-error) when the response is not successful and contains helper methods to handle common codes. | ||
|
||
We call those helper methods [Error Catchers](/reference/fetchtastic#error-catchers): | ||
|
||
```typescript | ||
const api = new Fetchtastic('https://jsonplaceholder.typicode.com/posts'); | ||
|
||
await api | ||
.badRequest(error => /* 400 bad-request */) | ||
.unauthorized(error => /* 401 unauthorized */) | ||
.forbidden(error => /* 403 forbidden */) | ||
.notFound(error => /* 404 not-found */) | ||
.timeout(error => /* 408 request-timeout */) | ||
.serverError(error => /* 500 internal-server-error */) | ||
.onError(418, error => /* I'm a Teapot */) | ||
.resolve() | ||
.then(doSomethingWithResponse) | ||
.catch((error: Error) => { | ||
/* Uncaught errors */ | ||
|
||
if (error instanceof HttpError) { | ||
console.log(error.url) // https://jsonplaceholder.typicode.com/posts | ||
console.log(error.status) // 429 | ||
console.log(error.message) // Too Many Requests | ||
} | ||
|
||
}); | ||
|
||
``` | ||
|
||
The original config is passed along the error and can be used in order to perform an additional request, and modify the initial response: | ||
|
||
```typescript | ||
let authToken = ''; | ||
|
||
const api = new Fetchtastic('https://my.backend-api.com'); | ||
|
||
function getResource(callback) { | ||
api | ||
.get('/resource') | ||
.headers({ Authorization: authToken }) | ||
.unauthorized(async (error, config) => { | ||
// Renew credentials | ||
const credentials = await api.get('/renew-token').json(); | ||
authToken = credentials.token; | ||
// Replay the original request with new credentials | ||
return config.headers({ Authorization: authToken }).resolve(); | ||
}) | ||
.json() | ||
.then(callback); | ||
} | ||
``` | ||
|
||
In the above example, you can notice that the execution flow is preserved as expected, | ||
`.then` will be called with the result of the original request or the replayed one. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"core": "Core", | ||
"resolver": "Resolver", | ||
"error": "Error", | ||
"fetchtastic": "Fetchtastic", | ||
"http-error": "HttpError", | ||
"types": "Types", | ||
"utility": "Utility" | ||
"utilities": "Utilities" | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.