-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add RequestError support (#2465)
- Loading branch information
1 parent
6fb5995
commit 46b5f2f
Showing
7 changed files
with
61 additions
and
5 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 |
---|---|---|
|
@@ -8,7 +8,7 @@ The `octokit` package integrates the three main Octokit libraries | |
2. **App client** (GitHub App & installations, Webhooks, OAuth) | ||
3. **Action client** (Pre-authenticated API client for single repository) | ||
|
||
## Table of contents | ||
## Table of contents <!-- omit in toc --> | ||
|
||
<!-- toc --> | ||
|
||
|
@@ -23,6 +23,7 @@ The `octokit` package integrates the three main Octokit libraries | |
- [`octokit.request()`](#octokitrequest) | ||
- [Pagination](#pagination) | ||
- [Media Type previews and formats](#media-type-previews-and-formats) | ||
- [Request error handling](#request-error-handling) | ||
- [GraphQL API queries](#graphql-api-queries) | ||
- [Schema previews](#schema-previews) | ||
- [App client](#app-client) | ||
|
@@ -549,6 +550,35 @@ console.log("topics on octocat/hello-world: %j", data.topics); | |
|
||
Learn more about [Media type formats](https://docs.github.com/en/rest/overview/media-types) and [previews](https://docs.github.com/en/[email protected]/rest/overview/api-previews) used on GitHub Enterprise Server. | ||
|
||
#### Request error handling | ||
|
||
**Standalone module:** [`@octokit/request-error`](https://github.com/octokit/request-error.js/#readme) | ||
|
||
For request error handling, import `RequestError` and use `try...catch` statement. | ||
|
||
```typescript | ||
import { RequestError } from "octokit"; | ||
``` | ||
|
||
```typescript | ||
try { | ||
// your code here that sends at least one Octokit request | ||
await octokit.request("GET /"); | ||
} catch (error) { | ||
// Octokit errors always have a `error.status` property which is the http response code nad it's instance of RequestError | ||
if (error instanceof RequestError) { | ||
// handle Octokit error | ||
// error.message; // Oops | ||
// error.status; // 500 | ||
// error.request; // { method, url, headers, body } | ||
// error.response; // { url, status, headers, data } | ||
} else { | ||
// handle all other errors | ||
throw error; | ||
} | ||
} | ||
``` | ||
|
||
### GraphQL API queries | ||
|
||
Octokit also supports GitHub's GraphQL API directly -- you can use the same queries shown in the documentation and available in the GraphQL explorer in your calls with `octokit.graphql`. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,2 +1,2 @@ | ||
export { Octokit } from "./octokit"; | ||
export { Octokit, RequestError } from "./octokit"; | ||
export { App, OAuthApp, createNodeMiddleware } from "./app"; |
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