1 | # http-error.js
|
2 |
|
3 | > Error class for Octokit request errors
|
4 |
|
5 | [](https://www.npmjs.com/package/@octokit/request-error)
|
6 | [](https://github.com/octokit/request-error.js/actions?query=workflow%3ATest)
|
7 |
|
8 | ## Usage
|
9 |
|
10 | <table>
|
11 | <tbody valign=top align=left>
|
12 | <tr><th>
|
13 | Browsers
|
14 | </th><td width=100%>
|
15 | Load <code>@octokit/request-error</code> directly from <a href="https://cdn.skypack.dev">cdn.skypack.dev</a>
|
16 |
|
17 | ```html
|
18 | <script type="module">
|
19 | import { RequestError } from "https://cdn.skypack.dev/@octokit/request-error";
|
20 | </script>
|
21 | ```
|
22 |
|
23 | </td></tr>
|
24 | <tr><th>
|
25 | Node
|
26 | </th><td>
|
27 |
|
28 | Install with <code>npm install @octokit/request-error</code>
|
29 |
|
30 | ```js
|
31 | const { RequestError } = require("@octokit/request-error");
|
32 | // or: import { RequestError } from "@octokit/request-error";
|
33 | ```
|
34 |
|
35 | </td></tr>
|
36 | </tbody>
|
37 | </table>
|
38 |
|
39 | ```js
|
40 | const error = new RequestError("Oops", 500, {
|
41 | request: {
|
42 | method: "POST",
|
43 | url: "https://api.github.com/foo",
|
44 | body: {
|
45 | bar: "baz",
|
46 | },
|
47 | headers: {
|
48 | authorization: "token secret123",
|
49 | },
|
50 | },
|
51 | response: {
|
52 | status: 500,
|
53 | url: "https://api.github.com/foo",
|
54 | headers: {
|
55 | "x-github-request-id": "1:2:3:4",
|
56 | },
|
57 | data: {
|
58 | foo: "bar",
|
59 | },
|
60 | },
|
61 | });
|
62 |
|
63 | error.message; // Oops
|
64 | error.status; // 500
|
65 | error.request; // { method, url, headers, body }
|
66 | error.response; // { url, status, headers, data }
|
67 | ```
|
68 |
|
69 | ### Usage with Octokit
|
70 |
|
71 | ```js
|
72 | try {
|
73 | // your code here that sends at least one Octokit request
|
74 | await octokit.request("GET /");
|
75 | } catch (error) {
|
76 | // Octokit errors always have a `error.status` property which is the http response code
|
77 | if (error.status) {
|
78 | // handle Octokit error
|
79 | } else {
|
80 | // handle all other errors
|
81 | throw error;
|
82 | }
|
83 | }
|
84 | ```
|
85 |
|
86 | ## LICENSE
|
87 |
|
88 | [MIT](LICENSE)
|