Skip to content

Commit

Permalink
feat: add RequestError support (#2465)
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszPorowski authored Jun 20, 2023
1 parent 6fb5995 commit 46b5f2f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 5 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand All @@ -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)
Expand Down Expand Up @@ -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`.
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"@octokit/plugin-rest-endpoint-methods": "^7.1.1",
"@octokit/plugin-retry": "^4.1.3",
"@octokit/plugin-throttling": "^5.2.2",
"@octokit/types": "^9.2.2"
"@octokit/types": "^9.2.2",
"@octokit/request-error": "^v3.0.3"
},
"devDependencies": {
"@octokit/tsconfig": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
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";
2 changes: 2 additions & 0 deletions src/octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { throttling } from "@octokit/plugin-throttling";

import { VERSION } from "./version";

export { RequestError } from "@octokit/request-error";

export const Octokit = OctokitCore.plugin(
restEndpointMethods,
paginateRest,
Expand Down
14 changes: 13 additions & 1 deletion test/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Octokit, App, OAuthApp } from "../src";
import { Octokit, App, OAuthApp, RequestError } from "../src";

describe("Smoke tests", () => {
it("Octokit is a function", () => {
Expand Down Expand Up @@ -53,4 +53,16 @@ describe("Smoke tests", () => {

expect(app.octokit.request).toBeInstanceOf(Function);
});

it("RequestError inherits from Error", () => {
const error = new RequestError("test", 123, {
request: {
method: "GET",
url: "https://api.github.com/",
headers: {},
},
});

expect(error).toBeInstanceOf(RequestError);
});
});
12 changes: 11 additions & 1 deletion test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// THIS CODE IS NOT EXECUTED. IT IS JUST FOR TYPECHECKING
// ************************************************************

import { App, OAuthApp, Octokit } from "../src";
import { App, OAuthApp, Octokit, RequestError } from "../src";

function expect<T>(what: T) {}

Expand Down Expand Up @@ -30,4 +30,14 @@ export async function OctokitTest() {
}
);
expect<number>(issues[0].id);

const error = new RequestError("test", 123, {
request: {
method: "GET",
url: "https://api.github.com/",
headers: {},
},
});

expect<RequestError>(error);
}

0 comments on commit 46b5f2f

Please sign in to comment.