2025-03-14 13:13:56 -07:00
# request-error.js
2020-04-28 16:46:47 +02:00
> Error class for Octokit request errors
[](https://www.npmjs.com/package/@octokit/request -error)
2020-09-18 15:40:23 +01:00
[](https://github.com/octokit/request-error.js/actions?query=workflow%3ATest)
2020-04-28 16:46:47 +02:00
## Usage
<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
2025-03-14 13:13:56 -07:00
Load <code>@octokit/request -error</code> directly from <a href="https://esm.sh">esm.sh</a>
2020-04-28 16:46:47 +02:00
``` html
< script type = "module" >
2025-03-14 13:13:56 -07:00
import { RequestError } from "https://esm.sh/@octokit/request-error" ;
2020-04-28 16:46:47 +02:00
< / script >
```
</td></tr>
<tr><th>
Node
</th><td>
Install with <code>npm install @octokit/request -error</code>
``` js
2025-03-14 13:13:56 -07:00
import { RequestError } from "@octokit/request-error" ;
2020-04-28 16:46:47 +02:00
```
</td></tr>
</tbody>
</table>
2025-03-14 13:13:56 -07:00
> [!IMPORTANT]
> As we use [conditional exports](https://nodejs.org/api/packages.html#conditional-exports), you will need to adapt your `tsconfig.json` by setting `"moduleResolution": "node16", "module": "node16"`.
>
> See the TypeScript docs on [package.json "exports"](https://www.typescriptlang.org/docs/handbook/modules/reference.html#packagejson-exports).<br>
> See this [helpful guide on transitioning to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) from [@sindresorhus](https://github.com/sindresorhus)
2020-04-28 16:46:47 +02:00
``` js
const error = new RequestError ( "Oops" , 500 , {
request : {
method : "POST" ,
url : "https://api.github.com/foo" ,
body : {
2020-09-18 15:40:23 +01:00
bar : "baz" ,
2020-04-28 16:46:47 +02:00
} ,
headers : {
2020-09-18 15:40:23 +01:00
authorization : "token secret123" ,
} ,
} ,
2025-03-14 13:13:56 -07:00
response : {
status : 500 ,
url : "https://api.github.com/foo" ,
headers : {
"x-github-request-id" : "1:2:3:4" ,
} ,
data : {
foo : "bar" ,
} ,
} ,
2020-04-28 16:46:47 +02:00
} ) ;
error . message ; // Oops
error . status ; // 500
2025-03-14 13:13:56 -07:00
error . request ; // { method, url, headers, body }
2023-07-13 09:09:17 +00:00
error . response ; // { url, status, headers, data }
```
2025-03-14 13:13:56 -07:00
### Usage with Octokit
``` js
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
if ( error . status ) {
// handle Octokit error
} else {
// handle all other errors
throw error ;
}
}
```
2020-04-28 16:46:47 +02:00
## LICENSE
[MIT ](LICENSE )